-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path05_Implementation_of_ellipse_generating_Algorithm.cpp
More file actions
105 lines (83 loc) · 1.99 KB
/
Copy path05_Implementation_of_ellipse_generating_Algorithm.cpp
File metadata and controls
105 lines (83 loc) · 1.99 KB
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
#include <iostream>
#include <graphics.h>
#include <conio.h>
using namespace std;
// Function to plot four symmetric points
void plotEllipsePoints(int xc, int yc, int x, int y)
{
putpixel(xc + x, yc + y, WHITE);
putpixel(xc - x, yc + y, WHITE);
putpixel(xc + x, yc - y, WHITE);
putpixel(xc - x, yc - y, WHITE);
}
// Midpoint Ellipse Algorithm
void midpointEllipse(int xc, int yc, int rx, int ry)
{
float dx, dy, d1, d2;
int x = 0;
int 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;
// Region 1
while (dx < dy)
{
plotEllipsePoints(xc, yc, x, y);
if (d1 < 0)
{
x++;
dx = dx + (2 * ry * ry);
d1 = d1 + dx + (ry * ry);
}
else
{
x++;
y--;
dx = dx + (2 * ry * ry);
dy = dy - (2 * rx * rx);
d1 = d1 + dx - dy + (ry * ry);
}
}
// Decision parameter for Region 2
d2 = ((ry * ry) * ((x + 0.5) * (x + 0.5))) +
((rx * rx) * ((y - 1) * (y - 1))) -
(rx * rx * ry * ry);
// Region 2
while (y >= 0)
{
plotEllipsePoints(xc, yc, x, y);
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);
}
}
}
int main()
{
// Open graphics window
initwindow(800, 600, "Midpoint Ellipse Algorithm");
int xc, yc, rx, ry;
cout << "Enter center coordinates (xc yc): ";
cin >> xc >> yc;
cout << "Enter X-axis radius (rx): ";
cin >> rx;
cout << "Enter Y-axis radius (ry): ";
cin >> ry;
// Draw ellipse
midpointEllipse(xc, yc, rx, ry);
getch();
closegraph();
return 0;
}