Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Something like this should do the trick</p> <pre><code>public class EllipseDrawer { private static PointF GetEllipsePointFromX(float x, float a, float b) { //(x/a)^2 + (y/b)^2 = 1 //(y/b)^2 = 1 - (x/a)^2 //y/b = -sqrt(1 - (x/a)^2) --Neg root for upper portion of the plane //y = b*-sqrt(1 - (x/a)^2) return new PointF(x, b * -(float)Math.Sqrt(1 - (x * x / a / a))); } public static void Ellipse(bool[,] pixels, Rectangle area) { DrawEllipse(pixels, area, false); } public static void FillEllipse(bool[,] pixels, Rectangle area) { DrawEllipse(pixels, area, true); } private static void DrawEllipse(bool[,] pixels, Rectangle area, bool fill) { // Get the size of the matrix var matrixWidth = pixels.GetLength(0); var matrixHeight = pixels.GetLength(1); var offsetY = area.Top; var offsetX = area.Left; // Figure out how big the ellipse is var ellipseWidth = (float)area.Width; var ellipseHeight = (float)area.Height; // Figure out the radiuses of the ellipses var radiusX = ellipseWidth / 2; var radiusY = ellipseHeight / 2; //Keep track of the previous y position var prevY = 0; var firstRun = true; // Loop through the points in the matrix for (var x = 0; x &lt;= radiusX; ++x) { var xPos = x + offsetX; var rxPos = (int)ellipseWidth - x - 1 + offsetX; if (xPos &lt; 0 || rxPos &lt; xPos || xPos &gt;= matrixWidth) { continue; } var pointOnEllipseBoundCorrespondingToXMatrixPosition = GetEllipsePointFromX(x - radiusX, radiusX, radiusY); var y = (int) Math.Floor(pointOnEllipseBoundCorrespondingToXMatrixPosition.Y + (int)radiusY); var yPos = y + offsetY; var ryPos = (int)ellipseHeight - y - 1 + offsetY; if (yPos &gt;= 0) { if (xPos &gt; -1 &amp;&amp; xPos &lt; matrixWidth &amp;&amp; yPos &gt; -1 &amp;&amp; yPos &lt; matrixHeight) { pixels[xPos, yPos] = true; } if(xPos &gt; -1 &amp;&amp; xPos &lt; matrixWidth &amp;&amp; ryPos &gt; -1 &amp;&amp; ryPos &lt; matrixHeight) { pixels[xPos, ryPos] = true; } if (rxPos &gt; -1 &amp;&amp; rxPos &lt; matrixWidth) { if (yPos &gt; -1 &amp;&amp; yPos &lt; matrixHeight) { pixels[rxPos, yPos] = true; } if (ryPos &gt; -1 &amp;&amp; ryPos &lt; matrixHeight) { pixels[rxPos, ryPos] = true; } } } //While there's a &gt;1 jump in y, fill in the gap (assumes that this is not the first time we've tracked y, x != 0) for (var j = prevY - 1; !firstRun &amp;&amp; j &gt; y - 1 &amp;&amp; y &gt; 0; --j) { var jPos = j + offsetY; var rjPos = (int)ellipseHeight - j - 1 + offsetY; if(jPos == rjPos - 1) { continue; } if(jPos &gt; -1 &amp;&amp; jPos &lt; matrixHeight) { pixels[xPos, jPos] = true; } if(rjPos &gt; -1 &amp;&amp; rjPos &lt; matrixHeight) { pixels[xPos, rjPos] = true; } if (rxPos &gt; -1 &amp;&amp; rxPos &lt; matrixWidth) { if(jPos &gt; -1 &amp;&amp; jPos &lt; matrixHeight) { pixels[rxPos, jPos] = true; } if(rjPos &gt; -1 &amp;&amp; rjPos &lt; matrixHeight) { pixels[rxPos, rjPos] = true; } } } firstRun = false; prevY = y; var countTarget = radiusY - y; for (var count = 0; fill &amp;&amp; count &lt; countTarget; ++count) { ++yPos; --ryPos; // Set all four points in the matrix we just learned about // also, make the indication that for the rest of this row, we need to fill the body of the ellipse if(yPos &gt; -1 &amp;&amp; yPos &lt; matrixHeight) { pixels[xPos, yPos] = true; } if(ryPos &gt; -1 &amp;&amp; ryPos &lt; matrixHeight) { pixels[xPos, ryPos] = true; } if (rxPos &gt; -1 &amp;&amp; rxPos &lt; matrixWidth) { if(yPos &gt; -1 &amp;&amp; yPos &lt; matrixHeight) { pixels[rxPos, yPos] = true; } if(ryPos &gt; -1 &amp;&amp; ryPos &lt; matrixHeight) { pixels[rxPos, ryPos] = true; } } } } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload