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 7839343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:25:03+00:00 2026-06-02T15:25:03+00:00

On SO, found the following simple algorithm for drawing filled circles: for(int y=-radius; y<=radius;

  • 0

On SO, found the following simple algorithm for drawing filled circles:

for(int y=-radius; y<=radius; y++)
    for(int x=-radius; x<=radius; x++)
        if(x*x+y*y <= radius*radius)
            setpixel(origin.x+x, origin.y+y);

Is there an equally simple algorithm for drawing filled ellipses?

  • 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-02T15:25:05+00:00Added an answer on June 2, 2026 at 3:25 pm

    Simpler, with no double and no division (but be careful of integer overflow):

    for(int y=-height; y<=height; y++) {
        for(int x=-width; x<=width; x++) {
            if(x*x*height*height+y*y*width*width <= height*height*width*width)
                setpixel(origin.x+x, origin.y+y);
        }
    }
    

    We can take advantage of two facts to optimize this significantly:

    • Ellipses have vertical and horizontal symmetry;
    • As you progress away from an axis, the contour of the ellipse slopes more and more.

    The first fact saves three-quarters of the work (almost); the second fact tremendously reduces the number of tests (we test only along the edge of the ellipse, and even there we don’t have to test every point).

    int hh = height * height;
    int ww = width * width;
    int hhww = hh * ww;
    int x0 = width;
    int dx = 0;
    
    // do the horizontal diameter
    for (int x = -width; x <= width; x++)
        setpixel(origin.x + x, origin.y);
    
    // now do both halves at the same time, away from the diameter
    for (int y = 1; y <= height; y++)
    {
        int x1 = x0 - (dx - 1);  // try slopes of dx - 1 or more
        for ( ; x1 > 0; x1--)
            if (x1*x1*hh + y*y*ww <= hhww)
                break;
        dx = x0 - x1;  // current approximation of the slope
        x0 = x1;
    
        for (int x = -x0; x <= x0; x++)
        {
            setpixel(origin.x + x, origin.y - y);
            setpixel(origin.x + x, origin.y + y);
        }
    }
    

    This works because each scan line is shorter than the previous one, by at least as much
    as that one was shorter than the one before it. Because of rounding to integer pixel coordinates, that’s not perfectly accurate — the line can be shorter by one pixel less that that.

    In other words, starting from the longest scan line (the horizontal diameter), the amount by which each line is shorter than the previous one, denoted dx in the code, decreases by at most one, stays the same, or increases. The first inner for finds the exact amount by which the current scan line is shorter than the previous one, starting at dx - 1 and up, until we land just inside the ellipse.

                           |         x1 dx x0
                           |######    |<-->|
     current scan line --> |###########    |<>|previous dx
    previous scan line --> |################  |
    two scan lines ago --> |###################
                           |##################### 
                           |###################### 
                           |######################
                           +------------------------
    

    To compare the number of inside-ellipse tests, each asterisk is one pair of coordinates tested in the naive version:

     *********************************************
     *********************************************
     *********************************************
     *********************************************
     *********************************************
     *********************************************
     *********************************************
     *********************************************
     *********************************************
     *********************************************
     *********************************************
     *********************************************
     *********************************************
     *********************************************
     *********************************************
     *********************************************
     *********************************************
    

    … and in the improved version:

                            *
                                 **
                                      ****
                                           ***
                                              ***
                                                ***
                                                 **
                                                 **
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote the following very simple python code to find circles in an image:
Reading through documentation, I found following: 1.9.1 1.8.4 1.8.2 A version of 1.8.2 select
On the MSDN, I have found following: public event EventHandler<MyEventArgs> SampleEvent; public void DemoEvent(string
I found the following table structures while I was watching ruby on rails tutorial.
I found the following code somewhere, but I am not understanding the code properly.
I found the following article : here about installing fonts on a Windows computer
I found the following syntax as a VB.NET property and I'm trying to convert
I found the following code in my team's project: Public Shared Function isRemoteDisconnectMessage(ByRef m
I found the following claim in the documentation for Net::OpenSSH : Note that using
I found the following statement: Map is an object that stores key/volume pairs. Given

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.