Like the raster line algorithm, each step is sampled at the interval unit and determines the nearest pixel position from the specified circle. To reduce the amount of computation, the circle can be eight points, according to the Bresenham line algorithm. We first give the point position function:
It is known that: (1), located within the circle boundary, (2), on the Circle boundary, (3), is outside the circle boundary.
In the first quadrant, assuming that the pixel points are drawn, the next step is to determine whether the position is drawn or closer to the circle.
The circle function equation of the decision parameter is worth in the midpoint of these two pixels,
Or
Which is or is, depending on the symbol.
Algorithm process:
1. Enter the circle Radius and center, and get the first point on the circumference:
2. Calculate the initial value of the decision parameter:
3. At each location, from the beginning, complete the following tests: If, the center of the circle at the next point of the round is, and
Otherwise, the next point of the Circle is, and
where and.
4. Determine the symmetry points in the other seven eight-minute circles.
5. Move each calculated pixel position to the circle path at the center of the circle and draw the coordinate values:
6. Repeat step 3 through step 5 until.
1 classSCREENPT {2 Private:3 glint x, y;4 5 Public:6 screenpt () {7x = y =0;8 }9 voidsetcoords (Glint xcoordvalue, glint ycoordvalue) {Tenx =Xcoordvalue; Oney =Ycoordvalue; A } -Glint Getx ()Const { - returnx; the } -Glint Gety ()Const { - returny; - } + voidIncrementx () { -++x; + } A voidDecrementy () { at--y; - } - }; - - voidSetPixel (Glint Xcoord, glint Ycoord) - { in Glbegin (gl_points); - glvertex2i (Xcoord, Ycoord); to glend (); + } - the voidcircleplotpoints (Glint xc, Glint YC, SCREENPT circpt) * { $SetPixel (XC + circpt.getx (), YC +circpt.gety ());Panax NotoginsengSetPixel (Xc-circpt.getx (), YC +circpt.gety ()); -SetPixel (XC + circpt.getx (), YC-circpt.gety ()); theSetPixel (Xc-circpt.getx (), YC-circpt.gety ()); +SetPixel (XC + circpt.gety (), YC +circpt.getx ()); ASetPixel (Xc-circpt.gety (), YC +circpt.getx ()); theSetPixel (XC + circpt.gety (), YC-circpt.getx ()); +SetPixel (Xc-circpt.gety (), YC-circpt.getx ()); - } $ $ voidCirclemidpoint (Glint xc, glint YC, glint radius) - { - SCREENPT circpt; the -Glint p =1-radius;//Initial value for midpoint parameterWuyi theCircpt.setcoords (0, radius);//Set coords for top point of circle. - //Plot the initial point in each circle Quadrant Wu circleplotpoints (XC, YC, CIRCPT); - //Calculate Next Point and Polt in each octant About while(Circpt.getx () <circpt.gety ()) { $ Circpt.incrementx (); - if(P <0) { -p + =2* CIRCPT.GETX () +1; - } A Else { + Circpt.decrementy (); thep + =2* (Circpt.getx ()-circpt.gety ()) +1; - } $ circleplotpoints (XC, YC, CIRCPT); the } the}
Midpoint Drawing Circle Algorithm