forked from EpsOnlineEducation/ComputerGraphics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ellipse_Midpoint.cpp
87 lines (70 loc) · 1.57 KB
/
Ellipse_Midpoint.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
87
#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 EllipsMidpoint(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 = b*b - a*a*b +a*a/4;
draw4point(xc,yc,x,y);
while (x <= x0)
{
if (P <0)
P += (b*b)*(2*x + 3);
else
{
P += (b*b)*(2*x+3) - 2*a*a*(y-1);
y--;
}
x++;
draw4point(xc,yc,x,y);
}
x=a;
y=0;
P = a*a - b*b*a +b*b/4;
draw4point(xc,yc,x,y);
while (x > x0)
{
if (P <0)
P +=(a*a)*(2*y + 3);
else
{
P += (a*a)*(2*y+3) - 2*b*b*(x-1);
x--;
}
y++;
draw4point(xc,yc,x,y);
}
}
void mydisplay()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
EllipsMidpoint(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 - MIDPOINT");
initGL();
glutDisplayFunc(mydisplay);
glutMainLoop();
}