-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFractal.cpp
More file actions
107 lines (90 loc) · 2.78 KB
/
Fractal.cpp
File metadata and controls
107 lines (90 loc) · 2.78 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
#include <iostream>
#include <windows.h>
#include <cmath>
#include <vector>
using namespace std;
struct Point {
double x, y;
};
//Create HilbertCurve Points
void hilbertCurve(vector<Point>& points, double x, double y, double xi, double xj, double yi, double yj, double n) {
if (n <= 0) {
double x1 = x + (xi + yi) / 2;
double y1 = y + (xj + yj) / 2;
points.push_back({ x1, y1 });
}
else {
hilbertCurve(points, x, y, yi / 2, yj / 2, xi / 2, xj / 2, n - 1);
hilbertCurve(points, x + xi / 2, y + xj / 2, xi / 2, xj / 2, yi / 2, yj / 2, n - 1);
hilbertCurve(points, x + xi / 2 + yi / 2, y + xj / 2 + yj / 2, xi / 2, xj / 2, yi / 2, yj / 2, n - 1);
hilbertCurve(points, x + xi / 2 + yi, y + xj / 2 + yj, -yi / 2, -yj / 2, -xi / 2, -xj / 2, n - 1);
}
}
// Function Draw Curve
void drawHilbertCurve(HDC hdc, const vector<Point>& points) {
if (points.empty()) return;
MoveToEx(hdc, points[0].x, points[0].y, NULL);
for (size_t i = 0; i < points.size(); ++i) {
LineTo(hdc, points[i].x, points[i].y);
}
}
// Window
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
static vector<Point> points;
static bool isDrawn = false;
switch (msg) {
case WM_CREATE: {
// Генерация точек кривой Гильберта
int depth = 3; // Глубина рекурсии
hilbertCurve(points, 80, 80, 600, 0, 0, 600, depth);
break;
}
case WM_PAINT: {
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
// Отрисовка кривой
if (!isDrawn) {
drawHilbertCurve(hdc, points);
isDrawn = true;
}
EndPaint(hwnd, &ps);
break;
}
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
// Window Main
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
const wchar_t CLASS_NAME[] = L"HilbertCurveWindowClass";
WNDCLASS wc = {};
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
HWND hwnd = CreateWindowEx(
0,
CLASS_NAME,
L"Hilbert Curve",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 1000, 1000,
NULL,
NULL,
hInstance,
NULL
);
if (hwnd == NULL) {
return 0;
}
ShowWindow(hwnd, nCmdShow);
MSG msg = {};
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}