forked from EpsOnlineEducation/ComputerGraphics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ellipse_Bresenham.cpp
86 lines (70 loc) · 1.58 KB
/
Ellipse_Bresenham.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <GL/glut.h>
#include <math.h>
void initGL()
{
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); //R=0,G=0,B=0, anpha=1
glOrtho(-320,320,-240,240,-1,1);
}
void draw4point(int xc, int yc, int x, int y)
{
glBegin(GL_POINTS);
glVertex2i(x + xc, y + yc);
glVertex2i(x + xc,-y + yc);
glVertex2i(xc - x, yc - y);
glVertex2i(xc - x, yc + y);
glEnd();
}
void EllipsBresenham(int xc,int yc,double a, double b )
{
int x = 0;
int y = b;
float x0 = a*a/sqrt(a*a + b*b);
float P = a*a*(1-2*b)+ b*b;
draw4point(xc,yc,x,y);
while (x <= x0)
{
if (P <0)
P += (2*b*b)*(2*x + 3);
else
{
P += (2*b*b)*(2*x+3) + 4*a*a*(1-y);
y--;
}
x++;
draw4point(xc,yc,x,y);
}
x=a;
y=0;
P=b*b*(1-2*a)+ a*a;
draw4point(xc,yc,x,y);
while (x > x0)
{
if (P <0)
P +=(2*a*a)*(2*y + 3);
else
{
P += (2*a*a)*(2*y + 3) + 4*b*b*(1-x);
x--;
}
y++;
draw4point(xc,yc,x,y);
}
}
void mydisplay()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
EllipsBresenham(0, 0, 300,200); //Tâm C(0,0) bán kính 200
glViewport(0,0,640,480);
glFlush();
}
int main(int argc, char** argv){
int mode=GLUT_SINGLE | GLUT_RGB;
glutInitDisplayMode(mode);
glutInitWindowSize(640, 480);
glutInitWindowPosition(0, 0);
glutCreateWindow("DEMO THUAT TOAN VE DUONG ELLIPSE - BRESENHAM");
initGL();
glutDisplayFunc(mydisplay);
glutMainLoop();
}