Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8177365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:29:24+00:00 2026-06-06T23:29:24+00:00

Does anyone know of any code to render an Ellipse to an array in

  • 0

Does anyone know of any code to render an Ellipse to an array in C#? I had a look about, I couldn’t find anything that answered my problem.

Given the following array:

bool[,] pixels = new bool[100, 100];

I’m looking for functions to render both a hollow and filled ellipse within a rectangular area. e.g:

public void Ellipse(bool[,] pixels, Rectangle area)
{
    // fill pixels[x,y] = true here for the ellipse within area.
}

public void FillEllipse(bool[,] pixels, Rectangle area)
{
    // fill pixels[x,y] = true here for the ellipse within area.
}

Ellipse(pixels, new Rectangle(20, 20, 60, 60));
FillEllipse(pixels, new Rectangle(40, 40, 20, 20));

Any help would be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T23:29:25+00:00Added an answer on June 6, 2026 at 11:29 pm

    Something like this should do the trick

    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 <= radiusX; ++x)
            {
                var xPos = x + offsetX;
                var rxPos = (int)ellipseWidth - x - 1 + offsetX;
    
                if (xPos < 0 || rxPos < xPos || xPos >= 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 >= 0)
                {
                    if (xPos > -1 && xPos < matrixWidth && yPos > -1 && yPos < matrixHeight)
                    {
                        pixels[xPos, yPos] = true;
                    }
    
                    if(xPos > -1 && xPos < matrixWidth && ryPos > -1 && ryPos < matrixHeight)
                    {
                        pixels[xPos, ryPos] = true;
                    }
    
                    if (rxPos > -1 && rxPos < matrixWidth)
                    {
                        if (yPos > -1 && yPos < matrixHeight)
                        {
                            pixels[rxPos, yPos] = true;
                        }
    
                        if (ryPos > -1 && ryPos < matrixHeight)
                        {
                            pixels[rxPos, ryPos] = true;
                        }
                    }
                }
    
                //While there's a >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 && j > y - 1 && y > 0; --j)
                {
                    var jPos = j + offsetY;
                    var rjPos = (int)ellipseHeight - j - 1 + offsetY;
    
                    if(jPos == rjPos - 1)
                    {
                        continue;
                    }
    
                    if(jPos > -1 && jPos < matrixHeight)
                    {
                        pixels[xPos, jPos] = true;
                    }
    
                    if(rjPos > -1 && rjPos < matrixHeight)
                    {
                        pixels[xPos, rjPos] = true;
                    }
    
                    if (rxPos > -1 && rxPos < matrixWidth)
                    {
                        if(jPos > -1 && jPos < matrixHeight)
                        {
                            pixels[rxPos, jPos] = true;
                        }
    
                        if(rjPos > -1 && rjPos < matrixHeight)
                        {
                            pixels[rxPos, rjPos] = true;
                        }
                    }
                }
    
                firstRun = false;
                prevY = y;
                var countTarget = radiusY - y;
    
                for (var count = 0; fill && count < 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 > -1 && yPos < matrixHeight)
                    {
                        pixels[xPos, yPos] = true;
                    }
    
                    if(ryPos > -1 && ryPos < matrixHeight)
                    {
                        pixels[xPos, ryPos] = true;
                    }
    
                    if (rxPos > -1 && rxPos < matrixWidth)
                    {
                        if(yPos > -1 && yPos < matrixHeight)
                        {
                            pixels[rxPos, yPos] = true;
                        }
    
                        if(ryPos > -1 && ryPos < matrixHeight)
                        {
                            pixels[rxPos, ryPos] = true;
                        }
                    }
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know of any code or tools that can strip literal values out
Does anyone know any site or book that presents problems like python challenge ,
Does anyone know any more details about google's web-crawler (aka GoogleBot)? I was curious
does anyone know any sort of app that lets users visit a page on
Does anyone know of any websites, or (preferably) downloadable packages that you can use
Does anyone of you know any code reviewing tool designed for JSF like PMD/Checkstyle
Does anyone know of any sample code for restoring a window's position and size
does any one know if, for example, I'm writing in c++ the following code:
Does anyone know any good resources with tasks or problems to get practice in
Does anyone know any open source project or adapter for MySQL-AdWhirl? We try to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.