Assignment 3 using the Bresenham algorithm to draw a circle in OpenGL

Source: Internet
Author: User

A Mission objectives

Using OpenGL, the Bresenham algorithm is used to draw the circle.

Two Task requirements

    1. Use integers to determine the position of a point.
    2. Mark the center of the circle. (I don't quite understand the meaning of the show, so I drew a point at the center of the circle to show it.) )
    3. Use at least 16 points to represent a circle.

Three Using the Platform

Windows 8.1

Visual Studio 2012

Four Implementation brief

Similar to the Bresenham linear algorithm, the midpoint drawing circle algorithm is used.

A function that defines a circle

The position of the point (x, y) can be determined according to the F (x, y) symbol:

So assuming that the point Pi (xi, Yi) is the current point, the next point is either Point E (xi + 1, yi), or point SE (xi + 1, yi–1). Take two points M (xi + 1, YI–1/2), bring in F (x, Y) and select the next given point according to the F (M) symbol.

Just like Bresenham to draw a straight line algorithm,

Set a decision variable to

and iterate over the decision variable di+1 of the next midpoint m, and di+1 is determined by the Di positive and negative:

and (demand points are integers)

The first one-eighth circles are generated clockwise, and then all the circles are generated according to symmetry.

optimization : The multiply 2 in di+1=di+2xi+3 is represented by a bitwise operation: di+1=di+ (xi<<1) +3, di+1=di+2 (xi-yi) +5 is represented as: di+1=di+ ((xi-yi) <<1) +5

Five Experience

Started to understand the difference between the Bresenham algorithm and the midpoint midpoint algorithm. Finally find the answer: Midpoint algorithm is the extension of Bresenham algorithm. The two have different angles of reasoning (although all use iterations), but the result is the same. The first idea of the Bresenham algorithm is to draw lines and circles, while the midpoint algorithm is more generic.

For ease of comparison, use the most basic line of drawing (0<k<1) to see the idea of two algorithms:

Bresenham algorithm: calculates the distance between the next point and F (x+1) based on the current point and Line Y=f (x) (0<k<1), possibly, possibly. The decision variables for the iteration are Dupper-dlower, judging by the variable symbol and which point to choose more recently, and updating the decision variables to iterate over the next selection.

Midpoint algorithm: an implicit equation f (x, y) =c0*x+c1*y+c2=0 is used. For the current point, it's next midpoint m (, the midpoint is taken into the equation, by judging the f () symbol to determine which point the line is closer to, and using the same iterative method for the next judgment. In addition to drawing lines and circles, you can also draw ellipses, hyperbolic curves, and so on.

There is a little more experience is that this problem must be seen in foreign forums to find a good explanation.

Code:

#include <gl/glut.h>#include<math.h>voidMyinit (void) {Glclearcolor (1.0f,1.0f,1.0f,0.0f); glcolor3f (1.0,0.0,0.0);    Glmatrixmode (gl_projection);    Glloadidentity (); Gluortho2d (0.0,500.0,0.0,500.0); Glmatrixmode (Gl_modelview);}voidPlotcirclepixel (intCxintCyintXinty)    {Glbegin (gl_points); Glvertex2i (CX+ x, CY +y); Glvertex2i (CX-X, CY +y); Glvertex2i (CX+ x, Cy-y); Glvertex2i (CX-X, Cy-y); Glvertex2i (CX+ Y, cy +x); Glvertex2i (CX-Y, Cy +x); Glvertex2i (CX+ Y, Cy-x); Glvertex2i (CX-Y, Cy-x); Glend ();}//@param CX x coordinate of center//@param cy y coordinate of center//@param r radius of CirclevoidBresenhamcircle (intCxintCyintR) {    intx =0, y = r;//Initial Position    intD =1-R; //draw a point at the center of the circleGlbegin (gl_points);    Glvertex2i (CX, CY);    Glend ();  while(x <=y) {plotcirclepixel (CX, CY, X, y); if(d <=0) {D+ = (x <<1) +3; } Else{d+ = ((x-y) <<1) +5; Y--; } x++; }}voidDisplayvoid) {glclear (gl_color_buffer_bit); Bresenhamcircle ( -, -, Max); Glflush ();}intMainintargcChar*argv[]) {Glutinit (&argc, argv); Glutinitdisplaymode (Glut_rgb|glut_single); Glutinitwindowposition ( -, -); Glutinitwindowsize ( -, -); Glutcreatewindow ("Bresenham Circle");    Myinit ();    Glutdisplayfunc (display);        Glutmainloop (); return 0;}

Assignment 3 uses the Bresenham algorithm to draw a circle in OpenGL

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.