-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDrawingFace.cpp
More file actions
114 lines (94 loc) · 3.42 KB
/
Copy pathDrawingFace.cpp
File metadata and controls
114 lines (94 loc) · 3.42 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
#include <windows.h>
#include <cmath>
#include <algorithm>
#define M_PI 3.14159265358979323846
POINT center;
bool centerSet = false;
void Draw8Points(HDC hdc, int xc, int yc, int x, int y, COLORREF color) {
SetPixel(hdc, xc + x, yc + y, color);
SetPixel(hdc, xc - x, yc + y, color);
SetPixel(hdc, xc - x, yc - y, color);
SetPixel(hdc, xc + x, yc - y, color);
SetPixel(hdc, xc + y, yc + x, color);
SetPixel(hdc, xc - y, yc + x, color);
SetPixel(hdc, xc - y, yc - x, color);
SetPixel(hdc, xc + y, yc - x, color);
}
void CirclePolar(HDC hdc, int xc, int yc, int R, COLORREF color) {
if (R == 0) return;
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 DrawFaceAndEyes(HDC hdc, int xc, int yc, int R, COLORREF color) {
CirclePolar(hdc, xc, yc, R, color);
int eyeRadius = R / 4;
int eyeDistance = R / 2;
double rightRadians = 45.0 * (M_PI / 180.0);
int rightEyeX = round(xc + eyeDistance * cos(rightRadians));
int rightEyeY = round(yc - eyeDistance * sin(rightRadians));
CirclePolar(hdc, rightEyeX, rightEyeY, eyeRadius, color);
double leftRadians = 135.0 * (M_PI / 180.0);
int leftEyeX = round(xc + eyeDistance * cos(leftRadians));
int leftEyeY = round(yc - eyeDistance * sin(leftRadians));
CirclePolar(hdc, leftEyeX, leftEyeY, eyeRadius, color);
}
LRESULT WINAPI WndProc(HWND hwnd, UINT mcode, WPARAM wp, LPARAM lp) {
switch (mcode) {
case WM_LBUTTONDOWN:
if (!centerSet) {
// Click 1: Set the Face Center
center.x = LOWORD(lp);
center.y = HIWORD(lp);
centerSet = true;
}
else {
// Click 2: Calculate Face Radius and Draw!
int rX = LOWORD(lp);
int rY = HIWORD(lp);
// Distance formula for radius
int R = round(sqrt(pow(rX - center.x, 2) + pow(rY - center.y, 2)));
HDC hdc = GetDC(hwnd);
// Draw everything in Yellow
DrawFaceAndEyes(hdc, center.x, center.y, R, RGB(255, 255, 0));
GdiFlush(); // Force screen update for Wine
ReleaseDC(hwnd, hdc);
// Reset so you can draw more faces!
centerSet = false;
}
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.hInstance = h;
wc.lpfnWndProc = WndProc;
wc.lpszClassName = "SmileyFaceClass";
wc.style = CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wc);
HWND hwnd = CreateWindow(
"SmileyFaceClass",
"Smiley Face (Click Center -> 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;
}