-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestKMeans.cpp
More file actions
144 lines (124 loc) · 2.97 KB
/
TestKMeans.cpp
File metadata and controls
144 lines (124 loc) · 2.97 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
#include "StdAfx.h"
#include "basetype.h"
#include "KMeans.h"
using namespace std;
using namespace algo;
using namespace base_type;
vector<Point2T<float> > g_vPoint(16);
template <class ForwardIterator>
void Render(HDC hDC, ForwardIterator begin, ForwardIterator end, int K)
{
typedef typename ForwardIterator::value_type value_type;
vector<HBRUSH> vBrush(K);
int n = 0;
generate_n(vBrush.begin(), K, [&n]()
{
return CreateSolidBrush(RGB(256 - n * 64, (n++) * 64, 128));
}
);
for_each(begin, end, [&hDC, &vBrush](value_type & pt)
{
RECT rc;
rc.left = LONG(pt.x);
rc.right = LONG(pt.x + 8);
rc.bottom = LONG(pt.y + 8);
rc.top = LONG(pt.y);
FillRect(hDC, &rc, vBrush[pt.ID]);
}
);
for_each(vBrush.begin(), vBrush.end(), [&](HBRUSH &brush)
{
DeleteObject(brush);
}
);
}
void OnSize(HWND hWnd, WPARAM wParam, int cx, int cy)
{
RECT rc;
GetClientRect(hWnd, &rc);
InvalidateRect(hWnd, &rc, TRUE);
}
LRESULT CALLBACK WindowProcess(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
RECT rc;
switch (msg)
{
case WM_CREATE:
#if 0
srand(GetTickCount());
generate(g_vPoint.begin(), g_vPoint.end(), [&]()
{
return Point2T<float>(float(rand() % 600), float(rand() % 600));
}
);
#else
for (size_t ii = 0; ii < g_vPoint.size(); ++ii)
{
if (ii < 8)
{
g_vPoint[ii].x = rand() % 100;
g_vPoint[ii].y = rand() % 100;
}
else
{
g_vPoint[ii].x = rand() % 100 + 300;
g_vPoint[ii].y = rand() % 100 + 300;
}
}
#endif
return 0;
case WM_LBUTTONUP:
KMeans(g_vPoint.begin(), g_vPoint.end(), 2);
GetClientRect(hWnd, &rc);
InvalidateRect(hWnd, &rc, TRUE);
return 0;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
Render(hdc, g_vPoint.begin(), g_vPoint.end(), 2);
EndPaint(hWnd, &ps);
return 0;
case WM_SIZE:
HANDLE_WM_SIZE(hWnd, wParam, lParam, OnSize);
return 0;
case WM_ERASEBKGND:
GetClientRect(hWnd, &rc);
hdc = GetDC(hWnd);
FillRect(hdc, &rc, WHITE_BRUSH);
ReleaseDC(hWnd, hdc);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hWnd, msg, wParam, lParam);
}
return LRESULT(0);
}
TCHAR szClassName[] = TEXT("KMeans");
int WINAPI WinMain(HINSTANCE hPrev, HINSTANCE hInst, LPSTR lpCmd, int nCmd)
{
WNDCLASS wc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = WHITE_BRUSH;
wc.hCursor = ::LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW));
wc.hIcon = ::LoadIcon(NULL, MAKEINTRESOURCE(IDI_APPLICATION));
wc.lpszClassName = szClassName;
wc.lpszMenuName = NULL;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProcess;
RegisterClass(&wc);
HWND hWnd = CreateWindow(szClassName, TEXT("KMeans"), WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInst, 0);
ShowWindow(hWnd, nCmd);
UpdateWindow(hWnd);
MSG msg;
while (GetMessage(&msg, 0, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.lParam;
}