Simple algorithm for drawing filled ellipse in C/C++

前端 未结 4 1160
滥情空心
滥情空心 2021-01-31 05:42

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++)
        i         


        
4条回答
  •  生来不讨喜
    2021-01-31 06:18

    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:

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

提交回复
热议问题