Having an int I and number if iterations and a function DrawPoint(x,y) how to draw a circle?
So we have a function DrawPoint(x,y) that draws a point, we have to draw some number of points that would look like a circle. How to create such for(i=0; i<numb开发者_StackOverflow社区erOfIterations; i++)
to draw a circle?
// (cx, cy) is the center of the circle
// r is the circle radius
// the smaller the granularity, the better the circle will look
// to draw only numberOfIterations points, granularity
// should be 2*pi / numberOfIterations
for(i=0; i<2*pi; i+=granularity)
DrawPoint(cx + r*sin(i), cy + r*cos(i));
One of the better algorithms for getting a decent circle is the Bresenham's circle algorithm, also called the Midpoint circle algorith.
The problem with the straight forward, basic circle routines is that they tend to have alias effects and not look right as a result, this algorithm gives a better approximation, though does not strictly fit your for(;;)
loop requirement, though it is still an iterative loop.
精彩评论