Computer Graphics (ii) OUTPUT elements _6_opengl curve function _2_ Midpoint circle algorithm

Source: Internet
Author: User
Tags gety

Midpoint Drawing Circle Algorithm
like the raster line algorithm, we sampled each step at a unit interval and determined the nearest pixel position from the specified circle. For givingSet radius r and Screen center (XC,yC), you can first use the algorithm to calculate the pixel position of the circle at the coordinates origin (0, 0), and then add the XC to X and yc to Y. This moves each computed position (x, Y) to its appropriate screen position. In the first quadrant,arc segments from x = 0 to x = y, the slope of the curve changes from 0 to-1.0. Therefore, the unit can be taken in the positive x direction on the eight-minute circlestep and use decision parameters to determine which of the two possible Y positions in each step is closer to the circle position. Then, the otherthe position of the seven eight-minute circle can be obtained by symmetry.
to apply the midpoint circle algorithm, we define a circle function:

any point on the circle boundary with a radius of R (x, y) satisfies the equation fcirc (y) = 0. If the point is inside the circle, the circle function is negative;The circle function is positive when the point is outside the circle. In summary, the relative position of any point (x, y) can be determined by the symbol of the circle function:



The circle function in the (3.30) formula tests the midpoint of two pixels close to the circumference at each sampling step. Therefore, the midpoint algorithmthe middle circle function is a decision parameter and can be similar to the line drawing algorithm to set the increment operation for this function.
Figure 3.19 shows the midpoint between the two candidate pixels on the sampling position x k+1. Suppose just in ( xK, yK) Draws a pixel, the nextstep needs to determine the pixel location(xK+1,yK)
or (xk+1,yk-1) closer to the circle. Our decision parameters are the circular function equation (3.29)at the midpoint of the two pixels, the value is obtained:
 
if pk < 0, then the midpoint is within the circle, and the pixels on the yk of the scan line are close to the perimeter. Otherwise, the midpoint is outside the circle oron the circumference boundary, we select the pixels of the scan line yk-1 .

Subsequent decision parameters can be obtained using an incremental operation. We evaluate the round function at the sampling position xk+1= xk+2 and canget the loop expression for the next decision parameter:

Or


Among them, yk+1 is YK or yk-1, depending on the PK symbol.

In order to get the pk+1 increment may be 2xk+1+1 (if PK is negative) or 2xk+1+1-2yk+1. And 2yk+1 can be evaluated in an incremental way, i.e.




At the starting position (0, R), the values for these two items are 0 and 2r, respectively. Each successive value of the 2xk+1 item can be obtained by adding 2 to the previous value or by 2 minus the previous value of 2yk+1.

The initial decision parameters are obtained when the circle function is evaluated at the starting position (x0,y0) = (0,r):

Or



If you specify the radius r as an integer, you can P0 for simple rounding:


because all the increments are integers.

assuming that the round parameters are specified in integer screen coordinates, as in the Bresenham line algorithm, the midpoint method uses integers plus subtract to calculate the pixel position along the circumference. We can summarize the steps of the midpoint circle algorithm as follows:

steps for midpoint drawing of a circle algorithm
1. Enter the circle radius r and Center (XC,yC) and get the first point on the circumference (the center point at the origin):

2. Calculate the initial value of the decision parameter:


3. In each x k position, starting with k = 0, complete the following tests: if PK < 0, the center of the circle at (0, 0) The next point of the (Xk+1,yk) and


Otherwise, the next point of the Circle is (xk+1,yk-1), and


Among them 2xk+1= 2xk+2 and 2yk+1= 2yk-2.

4. Determine the symmetry points in the other seven eight-minute circles.

5. Move each calculated pixel position to the center of the Circle (xc,yC), and draw the coordinate values:


6. Repeat step 3 through step 5 until x >= y.


example 3.2 drawing using the midpoint drawing circle algorithm

given the circle radius r = 10, we will demonstrate the midpoint-draw circle algorithm to determine the image in the first quadrant from x = 0 to x = y along the eight-minute circle The location of the vegetarian. The initial value of the decision parameter is


for the center at the coordinate Origin circle, the initial point (x0,y0) = (0,10), the initial increment entry for the decision parameter is calculated:

The subsequent decision parameter values calculated using the midpoint Draw circle algorithm and the position along the circle path are


The procedure for implementing a midpoint circle algorithm is given in the following section. The RADIUS value and the center coordinate are passed to the procedureCirclemidpoint. It then calculates a pixel position on the first eight-point circle and passes it to the procedure circleplotpoints. The process throughthe SetPixel function is called over and over repeatedly to set the color of the circle in the frame cache, and all pixels in its symmetrical position, setpixel byOpenGL draw point function implementation.


#include <gl/glut.h>class screenpt{private:glint x, y;     Public:/*default constructor:initializes coordinate position to (0,0) */SCREENPT () {x = y = 0;        } void Setcoords (Glint xcoordvalue,glint ycorrdvalue) {x = Xcoordvalue;    y = ycorrdvalue; } Glint Getx () const{return x;}   Glint gety () const{return y;   }void Incrementx () {x + +;   } void Incrementy () {y--;   }};void setpixel (Glint xcoord,glint Ycoord) {glbegin (gl_points);   Glvertex2i (Xcoord,ycoord); Glend ();}   void Circlemidpoint (Glint xc,glint yc,glint radius) {SCREENPT circpt; Glint p = 1-radius;//initial value for midpoint parameter Circpt.setcoords (0,radius),//set coords for top point of Cir   CLE void Circleplotpoints (GLINT,GLINT,SCREENPT);   /*plot the initial point in each circle quadrant*/circleplotpoints (XC,YC,CIRCPT); /*calculate next point and plot in each octant*/while (Circpt.getx () < Circpt.gety ()) {circpt.incrementx ();         if (P < 0) p + = 2 * CIRCPT.GETX () + 1;            else{Circpt.decrementy ();    p + = 2 * (Circpt.getx ()-circpt.gety ()) + 1;  } circleplotpoints (XC, XY, CIRCPT);   }}void circleplotpoints (Glint xc,glint yc,screenpt circpt) {setpixel (XC + circpt.getx (), YC + circpt.gety ());   SetPixel (Xc-circpt.getx (), YC + circpt.gety ());   SetPixel (XC + circpt.getx (), yc-circpt.gety ());   SetPixel (Xc-circpt.getx (), yc-circpt.gety ());   SetPixel (XC + circpt.gety (), YC + circpt.getx ());   SetPixel (Xc-circpt.gety (), YC + circpt.getx ());   SetPixel (XC + circpt.gety (), Yc-circpt.getx ()); SetPixel (Xc-circpt.gety (), Yc-circpt.getx ());}


Computer Graphics (ii) OUTPUT elements _6_opengl curve function _2_ Midpoint circle algorithm

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.