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

前端 未结 4 1162
滥情空心
滥情空心 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:39

    An ellipse (about the origin) is a circle that has been linearly stretched along the x or y axes. So you can modify your loop like this:

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

    You can see that if width == height == radius, then this is equivalent to your code for drawing a circle.

提交回复
热议问题