"Graphics" experiment Seven: Midpoint Bresenham algorithm Draw ellipse

Source: Internet
Author: User
Tags set background

Development Environment:

Vc++6.0,opengl

Experimental content:

Use the midpoint Bresenham algorithm to draw an ellipse.

Experimental Results:

Code:
1#include <gl/glut.h>2 3 #defineWIDTH 5004 #defineHEIGHT 5005 #defineOFFSET 15//Offset , offset to Origin6 #defineA 67 #defineB 58 9 voidInit ()//Other InitializationTen { OneGlclearcolor (1.0f,1.0f,1.0f,1.0f);//Set background color, completely opaque AGLCOLOR3F (1.0f,0.0f,0.0f);//Set Brush Color -  - Glmatrixmode (gl_projection); the glloadidentity (); -Gluortho2d (0.0,30.0,0.0,30.0); - Glmatrixmode (gl_modelview); - } +  - voidMidbresenhamellipse (intAintb//midpoint Bresenham algorithm to draw ellipse + { A     intx, y; at     floatd1,d2; -x =0; y =b; -d1=b*b+a*a* (-b+0.25); -Glpointsize (5);//Set Brush size -  - Glbegin (gl_points); inGlvertex2i (offset+x,offset+y); -Glvertex2i (offset-x,offset-y); toGlvertex2i (offset-x,offset+y); +Glvertex2i (offset+x,offset-y); - glend (); the  *      while(B*b* (x+1) < a*a* (y0.5)){ $         if(d1<=0){Panax Notoginsengd1+=b*b* (2*x+3); -X + +; the         } +         Else{ Ad1+=b*b* (2*x+3) +a*a* (-2*y+2); theX + +; +y--; -         } $ Glbegin (gl_points); $Glvertex2i (offset+x,offset+y); -Glvertex2i (offset-x,offset-y); -Glvertex2i (offset-x,offset+y); theGlvertex2i (offset+x,offset-y); - glend ();Wuyi}//While the upper half part thed2=b*b* (x+0.5) * (x+0.5) +a*a* (y1) * (Y1)-a*a*b*b; -      while(y>0){ Wu         if(d2<=0){ -d2+=b*b* (2*x+2) +a*a* (-2*y+3); Aboutx++,y--; $         } -         Else{ -d2+=a*a* (-2*y+3); -y--; A         } + Glbegin (gl_points); theGlvertex2i (offset+x,offset+y); -Glvertex2i (offset-x,offset-y); $Glvertex2i (offset-x,offset+y); theGlvertex2i (offset+x,offset-y); the glend (); the     } the } -  in voidDisplay () the { theGlclear (Gl_color_buffer_bit);//Empty the color stack About  theMidbresenhamellipse (A, b);//draw an ellipse with a radius of 8 the  theGlflush ();//emptying buffer Directives + } -  the intMainintargcChar**argv)Bayi { theGlutinit (&argc,argv); theGlutinitdisplaymode (glut_single| GLUT_RGB);//Initialize display mode -Glutinitwindowsize (Width,height);//Initialize window size -Glutinitwindowposition ( $, -);//where the initialization window appears theGlutcreatewindow ("midpoint Bresenham Draw Ellipse");//Initialize Window caption the  theGlutdisplayfunc (Display);//Registering display functions theInit ();//Other Initialization -Glutmainloop ();//Enter program Loop the  the     return 0; the}

freecode:www.cnblogs.com/yym2013

"Graphics" experiment Seven: Midpoint Bresenham algorithm Draw ellipse

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.