-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bresenhams Circle.cpp
70 lines (57 loc) · 1.29 KB
/
Bresenhams Circle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <GL/glut.h>
#include<bits/stdc++.h>
using namespace std;
void plot_point(int x, int y, int xc, int yc)
{
glBegin(GL_POINTS);
glVertex2i( xc + x, yc + y );
glVertex2i( xc + x, yc - y );
glVertex2i( xc + y, yc + x );
glVertex2i( xc + y, yc - x );
glVertex2i( xc - x, yc - y );
glVertex2i( xc - y, yc - x );
glVertex2i( xc - x, yc + y );
glVertex2i( xc - y, yc + x );
glEnd();
}
void bresenham_circle(int r)
{
int x = 0, y = r, dx;
plot_point(x, y, 0, 0);
dx = 3 - 2 * r;
x += 1;
if(dx >= 0)
y -= 1;
while(x < y)
{
plot_point(x, y, 0, 0);
if(dx < 0)
dx += 4 * x + 6;
else
dx += 4 * (x - y) + 10;
if(dx >= 0)
y -= 1;
x += 1;
}
}
void display()
{
glClearColor(0.5, 0.5, 0.5, 1);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-100, 100, -100, 100, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
bresenham_circle(50);
glutSwapBuffers();
}
int main( int argc, char** argv )
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutCreateWindow("Bresenham's Circle");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}