-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircle.cpp
More file actions
77 lines (67 loc) · 1.94 KB
/
circle.cpp
File metadata and controls
77 lines (67 loc) · 1.94 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
#include "proj.h"
void plot_circle_points(int x, int y, int xt, int yt, int ellipse) {
drawGSPoint({x+xt, y+yt}, 0, GS_WHITE);
drawGSPoint({-x+xt, y+yt}, 0, GS_WHITE);
drawGSPoint({-x+xt, -y+yt}, 0, GS_WHITE);
drawGSPoint({x+xt, -y+yt}, 0, GS_WHITE);
if (!ellipse) {
drawGSPoint({-y+xt, x+yt}, 0, GS_WHITE);
drawGSPoint({-y+xt, -x+yt}, 0, GS_WHITE);
drawGSPoint({y+xt, -x+yt}, 0, GS_WHITE);
drawGSPoint({y+xt, x+yt}, 0, GS_WHITE);
}
}
void midpoint_circle(int x_true, int y_true, int r) {
int x = r, y = 0;
plot_circle_points(x, y, x_true, y_true, 0);
int P = 1 - r;
while (x > y) {
y++;
if (P <= 0) P = P + 2*y + 1;
else {
x--;
P = P + 2*y - 2*x + 1;
}
plot_circle_points(x, y, x_true, y_true, 0);
}
}
void midpoint_ellipse(int xc, int yc, int rx, int ry) {
float dx,dy,p1,p2;
float x = 0, y = ry;
// P1_0
p1 = (ry * ry) - (rx*rx*ry) + (.25*rx*rx);
dx = 2 * ry * ry * x;
dy = 2 * rx * rx * y;
while (dx<dy) {
plot_circle_points(std::round(x), std::round(y), xc, yc, 1); // may need to round, not truncate
if (p1 < 0) {
x++;
dx = dx + (2*ry*ry);
p1 = p1 + dx + (ry*ry);
} else {
x++;
y--;
dx = dx + (2*ry*ry);
dy = dy - (2*rx*rx);
p1 = p1 + dx - dy + (ry*ry);
}
}
// P2_0
p2 = ((ry * ry) * ((x + 0.5) * (x + 0.5))) +
((rx * rx) * ((y - 1) * (y - 1))) -
(rx * rx * ry * ry);
while (y >= 0) {
plot_circle_points(std::round(x), std::round(y), xc, yc, 1);
if (p2 > 0) {
y--;
dy = dy - (2*rx*rx);
p2 = p2 + (rx*rx) - dy;
} else {
y--;
x++;
dx = dx + (2*rx*rx);
dy = dy - (2*rx*rx);
p2 = p2 + dx - dy + (rx*rx);
}
}
}