-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircle.cpp
More file actions
203 lines (175 loc) · 6.16 KB
/
Copy pathCircle.cpp
File metadata and controls
203 lines (175 loc) · 6.16 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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include <windows.h>
#include <cmath>
#define M_PI 3.14159265358979323846
// Menu IDs for the 5 algorithms
#define ID_ALGO_DIRECT 1
#define ID_ALGO_POLAR 2
#define ID_ALGO_ITER_POLAR 3
#define ID_ALGO_BRESENHAM 4
#define ID_ALGO_FAST_BRESENHAM 5
POINT center;
bool centerSet = false;
int currentAlgorithm = ID_ALGO_DIRECT; // Default algorithm
void Draw8Points(HDC hdc, int xc, int yc, int a, int b, COLORREF color) {
SetPixel(hdc, xc + a, yc + b, color);
SetPixel(hdc, xc - a, yc + b, color);
SetPixel(hdc, xc - a, yc - b, color);
SetPixel(hdc, xc + a, yc - b, color);
SetPixel(hdc, xc + b, yc + a, color);
SetPixel(hdc, xc - b, yc + a, color);
SetPixel(hdc, xc - b, yc - a, color);
SetPixel(hdc, xc + b, yc - a, color);
}
void CircleDirect(HDC hdc, int xc, int yc, int R, COLORREF color) {
int x = 0, y = R;
int R2 = R * R;
Draw8Points(hdc, xc, yc, x, y, color);
while (x < y) {
x++;
y = round(sqrt((double)(R2 - x * x)));
Draw8Points(hdc, xc, yc, x, y, color);
}
}
void CirclePolar(HDC hdc, int xc, int yc, int R, COLORREF color) {
int x = R, y = 0;
double theta = 0, dtheta = 1.0 / R;
Draw8Points(hdc, xc, yc, x, y, color);
while (x > y) {
theta += dtheta;
x = round(R * cos(theta));
y = round(R * sin(theta));
Draw8Points(hdc, xc, yc, x, y, color);
}
}
void CircleIterativePolar(HDC hdc, int xc, int yc, int R, COLORREF color) {
double x = R, y = 0;
double dtheta = 1.0 / R;
double cdtheta = cos(dtheta), sdtheta = sin(dtheta);
Draw8Points(hdc, xc, yc, R, 0, color);
while (x > y) {
double x1 = x * cdtheta - y * sdtheta;
y = x * sdtheta + y * cdtheta;
x = x1;
Draw8Points(hdc, xc, yc, round(x), round(y), color);
}
}
void CircleBresenham(HDC hdc, int xc, int yc, int R, COLORREF color) {
int x = 0, y = R;
int d = 1 - R;
Draw8Points(hdc, xc, yc, x, y, color);
while (x < y) {
if (d < 0)
d += 2 * x + 2;
else {
d += 2 * (x - y) + 5;
y--;
}
x++;
Draw8Points(hdc, xc, yc, x, y, color);
}
}
void CircleFasterBresenham(HDC hdc, int xc, int yc, int R, COLORREF color) {
int x = 0, y = R;
int d = 1 - R;
int c1 = 3, c2 = 5 - 2 * R;
Draw8Points(hdc, xc, yc, x, y, color);
while (x < y) {
if (d < 0) {
d += c1;
c2 += 2;
} else {
d += c2;
c2 += 4;
y--;
}
c1 += 2;
x++;
Draw8Points(hdc, xc, yc, x, y, color);
}
}
LRESULT WINAPI WndProc(HWND hwnd, UINT mcode, WPARAM wp, LPARAM lp) {
switch (mcode) {
case WM_CREATE: {
// Create the top menu bar
HMENU hMenu = CreateMenu();
HMENU hSubMenu = CreatePopupMenu();
AppendMenu(hSubMenu, MF_STRING, ID_ALGO_DIRECT, "1. Direct Cartesian");
AppendMenu(hSubMenu, MF_STRING, ID_ALGO_POLAR, "2. Polar");
AppendMenu(hSubMenu, MF_STRING, ID_ALGO_ITER_POLAR, "3. Iterative Polar");
AppendMenu(hSubMenu, MF_STRING, ID_ALGO_BRESENHAM, "4. Bresenham");
AppendMenu(hSubMenu, MF_STRING, ID_ALGO_FAST_BRESENHAM, "5. Faster Bresenham");
AppendMenu(hMenu, MF_POPUP, (UINT_PTR)hSubMenu, "Algorithms");
SetMenu(hwnd, hMenu);
break;
}
case WM_COMMAND:
// Handle menu selections
if (LOWORD(wp) >= ID_ALGO_DIRECT && LOWORD(wp) <= ID_ALGO_FAST_BRESENHAM) {
currentAlgorithm = LOWORD(wp);
}
break;
case WM_LBUTTONDOWN:
// First Click: Set Center
center.x = LOWORD(lp);
center.y = HIWORD(lp);
centerSet = true;
break;
case WM_RBUTTONDOWN:
// Second Click: Calculate Radius and Draw
if (centerSet) {
int rX = LOWORD(lp);
int rY = HIWORD(lp);
// Calculate Radius
int R = round(sqrt(pow(rX - center.x, 2) + pow(rY - center.y, 2)));
if (R == 0) R = 1; // Prevent invisible circles
HDC hdc = GetDC(hwnd);
// Draw based on selected algorithm
switch (currentAlgorithm) {
case ID_ALGO_DIRECT: CircleDirect(hdc, center.x, center.y, R, RGB(255, 0, 0)); break; // Red
case ID_ALGO_POLAR: CirclePolar(hdc, center.x, center.y, R, RGB(0, 255, 0)); break; // Green
case ID_ALGO_ITER_POLAR: CircleIterativePolar(hdc, center.x, center.y, R, RGB(0, 0, 255)); break; // Blue
case ID_ALGO_BRESENHAM: CircleBresenham(hdc, center.x, center.y, R, RGB(255, 255, 0)); break; // Yellow
case ID_ALGO_FAST_BRESENHAM: CircleFasterBresenham(hdc, center.x, center.y, R, RGB(255, 0, 255)); break; // Magenta
}
GdiFlush(); // CRITICAL FOR WINE: Forces screen update immediately
ReleaseDC(hwnd, hdc);
centerSet = false; // Reset for the next pair of clicks
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, mcode, wp, lp);
}
return 0;
}
int APIENTRY WinMain(HINSTANCE h, HINSTANCE, LPSTR, int nsh) {
WNDCLASS wc = {};
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hInstance = h;
wc.lpfnWndProc = WndProc;
wc.lpszClassName = "CircleAlgorithmsClass";
wc.style = CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wc);
HWND hwnd = CreateWindow(
"CircleAlgorithmsClass",
"Select Algorithm from Menu -> Left Click (Center) -> Right Click (Radius)",
WS_OVERLAPPEDWINDOW,
100, 100, 800, 600,
NULL, NULL, h, NULL
);
ShowWindow(hwnd, nsh);
UpdateWindow(hwnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}