-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ellipse.c
100 lines (94 loc) · 2.4 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
#include <windows.h>
#include <GL/glut.h>
#include <math.h>
#define PI 3.14
#include <stdio.h>
float major, minor, C_X, C_Y;
void init()
{
gluOrtho2D(0, 300, 0, 300);
}
void DP(int x, int y)
{
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
glFlush();
}
void bresenham_ellipse()
{
float res1, res2, p1, d2, x, y;
x = 0;
y = minor;
// Region 1
p1 = (minor * minor) - (major * major * minor) + (0.25 * major * major); // ry2-rx2*ry+0.25rx2
res1 = 2 * minor * minor * x;
res2 = 2 * major * major * y;
while (res1 < res2)
{
DP(x + C_X, y + C_Y); // drawing points
DP(-x + C_X, y + C_Y);
DP(x + C_X, -y + C_Y);
DP(-x + C_X, -y + C_Y);
if (p1 < 0)
{
x++;
res1 = res1 + (2 * minor * minor);
p1 = p1 + res1 + (minor * minor);
}
else
{
x++;
y--;
res1 = res1 + (2 * minor * minor);
res2 = res2 - (2 * major * major);
p1 = p1 + res1 - res2 + (minor * minor);
}
}
// Region 2
d2 = ((minor * minor) * ((x + 0.5) * (x + 0.5))) + ((major * major) * ((y - 1) * (y - 1))) - (major * major * minor * minor);
while (y >= 0)
{
DP(x + C_X, y + C_Y);
DP(-x + C_X, y + C_Y);
DP(x + C_X, -y + C_Y);
DP(-x + C_X, -y + C_Y);
if (d2 > 0)
{
y--;
res2 = res2 - (2 * major * major);
d2 = d2 + (major * major) - res2;
}
else
{
y--;
x++;
res1 = res1 + (2 * minor * minor);
res2 = res2 - (2 * major * major);
d2 = d2 + res1 - res2 + (major * major);
}
}
}
void display()
{
bresenham_ellipse();
}
int main(int argc, char **argv)
{
printf("ENTER THE MAJOR MINOR: ");
scanf("%f %f", &major, &minor);
printf("ENTER THE COORDINATES OF CENTER: ");
scanf("%f %f", &C_X, &C_Y);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE);
glutInitWindowSize(400, 300);
glutInitWindowPosition(100, 100);
glutCreateWindow("Ellipse");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
// gcc -o out Ellipse.c -lglut32 -lopengl32 -lglu32
//Enter center co-ordinates: 50 50
//Enter radius value: 50 50