-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircle_Rasterization.cpp
More file actions
86 lines (74 loc) · 2.69 KB
/
Copy pathCircle_Rasterization.cpp
File metadata and controls
86 lines (74 loc) · 2.69 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
#include <windows.h>
#include <cmath>
#include <algorithm>
using namespace std;
void DrawLineDDA(HDC hdc, int x1, int y1, int x2, int y2, COLORREF c) {
int dx = x2 - x1, dy = y2 - y1;
if (abs(dx) >= abs(dy)) {
if (x1 > x2) {
swap(x1, x2);
swap(y1, y2);
}
double m = (double)dy / dx, y = y1;
for (int x = x1; x <= x2; x++) {
SetPixel(hdc, x, (int)round(y), c);
y += m;
}
} else {
if (y1 > y2) { swap(x1, x2); swap(y1, y2); }
double minv = (double)dx / dy, x = x1;
for (int y = y1; y <= y2; y++){
SetPixel(hdc, (int)round(x), y, c);
x += minv;
}
}
}
void Draw8Points(HDC hdc, int xc, int yc, int x, int y, COLORREF c) {
SetPixel(hdc, xc + x, yc + y, c); SetPixel(hdc, xc - x, yc + y, c);
SetPixel(hdc, xc + x, yc - y, c); SetPixel(hdc, xc - x, yc - y, c);
SetPixel(hdc, xc + y, yc + x, c); SetPixel(hdc, xc - y, yc + x, c);
SetPixel(hdc, xc + y, yc - x, c); SetPixel(hdc, xc - y, yc - x, c);
}
void DrawCirclePolar(HDC hdc, int xc, int yc, int R, COLORREF c) {
if (R <= 0) return;
double dtheta = 1.0 / R;
for (double th = 0; th <= 0.785398; th += dtheta)
Draw8Points(hdc, xc, yc, (int)round(R * cos(th)), (int)round(R * sin(th)), c);
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) {
static int xc, yc, hasC = 0;
if (msg == WM_LBUTTONDOWN) {
xc = LOWORD(lp); yc = HIWORD(lp); hasC = 1;
}
else if (msg == WM_RBUTTONDOWN && hasC) {
int x1 = LOWORD(lp), y1 = HIWORD(lp);
HDC hdc = GetDC(hwnd);
int R = (int)round(sqrt(pow(x1 - xc, 2) + pow(y1 - yc, 2)));
DrawCirclePolar(hdc, xc, yc, R, RGB(0,0,0));
DrawLineDDA(hdc, xc - R, yc, xc + R, yc, RGB(0,0,0));
DrawLineDDA(hdc, xc, yc - R, xc, yc + R, RGB(0,0,0));
int f = (int)round(R * 0.7071);
DrawLineDDA(hdc, xc - f, yc - f, xc + f, yc + f, RGB(0,0,0));
DrawLineDDA(hdc, xc + f, yc - f, xc - f, yc + f, RGB(0,0,0));
ReleaseDC(hwnd, hdc);
hasC = 0;
}
else if (msg == WM_DESTROY) {
PostQuitMessage(0);
}
return DefWindowProc(hwnd, msg, wp, lp);
}
int main() {
HINSTANCE h = GetModuleHandle(NULL);
WNDCLASS wc = { 0 };
wc.lpfnWndProc = WndProc; wc.hInstance = h;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.lpszClassName = "CircleApp";
RegisterClass(&wc);
HWND hwnd = CreateWindow("CircleApp", "Graphics Task", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 100, 800, 600, NULL, NULL, h, NULL);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg); DispatchMessage(&msg);
}
return 0;
}