-
Notifications
You must be signed in to change notification settings - Fork 0
/
ellipse.c
158 lines (101 loc) · 2.79 KB
/
ellipse.c
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <stdio.h>
#include <math.h>
#include <GL/glut.h>
double dx, dy, d1, d2, x, y;
double rx, ry, xc=50, yc=50;
float round_value(float v)
{
return floor(v + 0.5);
}
void midptellipse()
{
x = 0;
y = ry;
// Initial decision parameter of region 1
d1 = (ry * ry)
- (rx * rx * ry)
+ (0.25 * rx * rx);
dx = 2 * ry * ry * x;
dy = 2 * rx * rx * y;
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
while (dx <= dy) {
glVertex2d(x + xc, y + yc);
glVertex2d(-x + xc, y + yc);
glVertex2d(x + xc, -y + yc);
glVertex2d(-x + xc, -y + yc);
if (d1 < 0) {
x++;
dx = dx + (2 * ry * ry);
d1 = d1 + dx + (ry * ry); //pk+1
}
else {
x++;
y--;
dx = dx + (2 * ry * ry);
dy = dy - (2 * rx * rx);
d1 = d1 + dx - dy + (ry * ry); //pk+1
}
}
// Decision parameter of region 2
d2 = ((ry * ry) * ((x + 0.5) * (x + 0.5)))
+ ((rx * rx) * ((y - 1) * (y - 1)))
- (rx * rx * ry * ry);
while (y >= 0) {
glVertex2d(x + xc, y + yc);
glVertex2d(-x + xc, y + yc);
glVertex2d(x + xc, -y + yc);
glVertex2d(-x + xc, -y + yc);
if (d2 > 0) {
y--;
dy = dy - (2 * rx * rx);
d2 = d2 + (rx * rx) - dy;
}
else {
y--;
x++;
dx = dx + (2 * ry * ry);
dy = dy - (2 * rx * rx);
d2 = d2 + dx - dy + (rx * rx);
}
}
glEnd();
glFlush();
}
void Init()
{
/* Set clear color to white */
glClearColor(0.0,0.8,0.4,0);
/* Set fill color to black */
glColor3f(0.0,0.0,0.0);
/* glViewport(0 , 0 , 640 , 480); */
/* glMatrixMode(GL_PROJECTION); */
/* glLoadIdentity(); */
gluOrtho2D(-640 , 640 , -640 , 640);
}
int main(int argc, char **argv)
{
printf("\nEnter minor radius( X1 , Y1):\n");
scanf("%lf",&rx);
printf("\nEnter major radius( X1 , Y1):\n");
scanf("%lf",&ry);
// printf("\nEnter Point1( X1 , Y1):\n");
//scanf("%lf",&xc);
//printf("\nEnter Point1( X2 , Y2):\n");
//scanf("%lf",&yc);
/* Initialise GLUT library */
glutInit(&argc,argv);
/* Set the initial display mode */
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
/* Set the initial window position and size */
glutInitWindowPosition(0,0);
glutInitWindowSize(640,480);
/* Create the window with title "DDA_Line" */
glutCreateWindow("Ellipse");
/* Initialize drawing colors */
Init();
/* Call the displaying function */
glutDisplayFunc(midptellipse);
/* Keep displaying untill the program is closed */
glutMainLoop();
}