Advertisement
iamyeasin

Brasenhas algorithm to draw a circle

Oct 16th, 2019
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. /*
  2. Brasenha's Algorithm of drawing circle
  3. */
  4. #include <GL/gl.h>
  5. #include <GL/glut.h>
  6. #include <bits/stdc++.h>
  7.  
  8. using namespace std;
  9.  
  10. double x,y, pk,r;
  11.  
  12.  
  13. void display(void){
  14. glClear(GL_COLOR_BUFFER_BIT);
  15. glColor3f(1.0,0.0,0.0);
  16.  
  17. pk = 1-r;
  18. x =0 , y = r;
  19.  
  20. while( x <= y ){
  21. glBegin(GL_POINTS);
  22. glVertex2i (x, y);
  23. glVertex2i (x, -y);
  24. glVertex2i (-x, y);
  25. glVertex2i (-x, -y);
  26.  
  27. glVertex2i (y, x);
  28. glVertex2i (-y, -x);
  29. glVertex2i (y, -x);
  30. glVertex2i (-y, x);
  31. glEnd();
  32. x++;
  33.  
  34. if( pk < 0 ){
  35. pk = pk + 2 * x + 1;
  36. }
  37. else{
  38. pk = pk + (2*x - 2*y) + 1;
  39. y = y-1;
  40. }
  41. }
  42.  
  43.  
  44. // glBegin(GL_POLYGON);
  45. // glVertex2i (40, 60);
  46. // glVertex2i (60, 60);
  47. // glVertex2i (60, 40);
  48. // glVertex2i (40, 40);
  49. // glEnd();
  50.  
  51.  
  52. glFlush ();
  53. }
  54.  
  55.  
  56. void init (void){
  57. glClearColor (1.0, 1.0, 1.0, 0.0);
  58. glMatrixMode(GL_PROJECTION);
  59. glLoadIdentity();
  60. glPointSize(5.0);
  61. glOrtho(-100.0, 100.0, -100.0, 100.0, -100.0, 100.0);
  62.  
  63. }
  64.  
  65. int main(int a, char **ar){
  66. glutInit(&a, ar);
  67. glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
  68. glutInitWindowSize (800, 800);
  69. glutInitWindowPosition (100, 100);
  70. glutCreateWindow ("Circle");
  71. init ();
  72. cin >> r;
  73. glutDisplayFunc(display);
  74. glutMainLoop();
  75. return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement