include windows include mem include lib matrix define BUTTON_1 define

  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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include <windows.h>
#include <mem.h>
#include ".../lib/matrix.h"
#define BUTTON_1 1
#define BUTTON_2 2
#define BUTTON_3 3
#define BUTTON_4 4
#define BUTTON_5 5
LRESULT CALLBACK _export WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK _export ChildWndProc(HWND, UINT, WPARAM, LPARAM);
void DrawAxises(HDC, int, int);
BOOL InitApp(HINSTANCE);
class Figure
{
private:
Vector *v_cur, *v;
public:
Figure(int PolyW,int PolyH)
{
v_cur = new Vector[8];
v = new Vector[8];
int x0 = PolyW, y0 = PolyH;
int dx = PolyW/3;
int dy = PolyH/3;
v[0].x = x0;
v[0].y = y0;
v[1].x = v[0].x + dx;
v[1].y = v[0].y;
v[2].x = v[1].x + dx;
v[2].y = v[1].y - dy;
v[3].x = v[2].x;
v[3].y = v[2].y - dy;
v[4].x = v[3].x - dx;
v[4].y = v[3].y - dy;
v[5].x = v[4].x - dx;
v[5].y = v[4].y;
v[6].x = v[5].x - dx;
v[6].y = v[5].y + dy;
v[7].x = v[6].x;
v[7].y = v[6].y + dy;
updateCoords();
}
~Figure()
{
delete [] v;
delete [] v_cur;
}
/* Метод вычисляет координаты фигуры после афинного преобразования */
void calcNewCoords(Matrix m)
{
for(int i=0;i<8;i++)
v_cur[i] = m * v_cur[i];
}
void updateCoords()
{
for (int i = 0; i < 8; i++)
v_cur[i] = v[i];
}
void draw(HDC hdc, COLORREF col, bool isNew)
{
Vector * vc;
POINT p[8];
HPEN hPen = (HPEN)CreatePen(PS_SOLID, 1, RGB(1,1,1));
HPEN hOldPen = (HPEN)SelectObject(hdc, hPen);
HBRUSH hBrush = (HBRUSH)CreateSolidBrush(col);
HBRUSH hOldBrush = (HBRUSH)SelectObject(hdc, hBrush);
if(isNew)
vc = v_cur;
else
vc = v;
for (int i = 0; i < 8; i++)
{
if(i == 8)
{
p[i].x = vc[0].x;
p[i].y = vc[0].y;
break;
}
p[i].x = vc[i].x;
p[i].y = vc[i].y;
}
Polygon(hdc,p,8);
SelectObject(hdc, hOldPen);
DeleteObject(hPen);
SelectObject(hdc,hOldBrush);
DeleteObject(hBrush);
}
};
const int Tx = -150, Ty = 50, Sx = 2, Sy = 1;
const int figureWidth = 50, figureHeight = 50;
Vector vec(Sx,Sy,1); // Вектор масштабирования
Vector vec_trans(Tx,Ty,1); // Вектор переноса
Matrix rotate = RotateZ(-M_PI); // Матрица поворота
Matrix scale = Scale(vec); // Матрица масштабирования
Matrix trans = Translate(vec_trans) ; // Матрица переноса
const int W_WIDTH = 1024, W_HEIGHT = 768;
const int PLOT_WIDTH = W_WIDTH - 200, PLOT_HEIGHT = W_HEIGHT - 35;
Figure polygon = Figure(figureWidth, figureHeight);
char const szClassName[] = "WindowAppClass";
char const szChildClassName[] = "AppChildClass";
char const szWindowTitle[] = "Верещашин А. В. ММ-494 Lab1";
HINSTANCE hInst;
HWND childhwnd;
#pragma argsused
WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
HWND hWnd;
hInst = hInstance;
hWnd = FindWindow(szClassName, NULL);
MSG msg;
if(!hPrevInstance)
{
if(!InitApp(hInstance))
return FALSE;
}
else
{
MessageBox(NULL, "Можно запускать только одну копию приложения",
"Ошибка", MB_OK | MB_ICONSTOP);
return FALSE;
}
hWnd = CreateWindowEx(WS_EX_TOPMOST,szClassName,szWindowTitle,
WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX & ~WS_THICKFRAME, 100, 100, W_WIDTH,
W_HEIGHT, 0, 0, hInstance, NULL);
childhwnd = CreateWindowEx(NULL,szChildClassName, "", WS_CHILDWINDOW |
CS_HREDRAW | CS_VREDRAW |WS_VISIBLE, 5, 5, PLOT_WIDTH, PLOT_HEIGHT , hWnd,
0, hInstance, NULL);
if(!childhwnd)
return FALSE;
if(!hWnd)
return FALSE;
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
while(GetMessage(&msg, 0, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
BOOL InitApp(HINSTANCE hInstance)
{
ATOM aWndClass;
WNDCLASS wc;
memset(&wc, 0, sizeof(wc));
wc.style = 0;
wc.lpfnWndProc = (WNDPROC) WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = CreateSolidBrush(RGB(10*5, 9*5, 9*5));
wc.lpszMenuName = (LPSTR)NULL;
wc.lpszClassName = (LPSTR)szClassName;
aWndClass = RegisterClass(&wc);
if(aWndClass == 0) return FALSE;
wc.lpfnWndProc = (WNDPROC) ChildWndProc;
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszClassName = (LPSTR)szChildClassName;
aWndClass = RegisterClass(&wc);
if(aWndClass == 0) return FALSE;
return TRUE;
}
LRESULT CALLBACK _export WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT Rect,rc;
static mem[3] = {1, 1, 1};
static HWND hb[3];
switch (msg)
{
case WM_CREATE:
{
hb[0] = CreateWindow("button", "Поворот", WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,
PLOT_WIDTH+10, 60, 130, 30, hwnd, (HMENU)BUTTON_1, hInst, NULL);
hb[1] = CreateWindow("button", "Перенос", WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,
PLOT_WIDTH+10, 100, 130, 30, hwnd, (HMENU)BUTTON_2, hInst, NULL);
hb[2] = CreateWindow("button", "Масштабирование", WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,
PLOT_WIDTH+10, 140, 130, 30, hwnd, (HMENU)BUTTON_3, hInst, NULL);
CreateWindow("button", "Отмена", WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,
PLOT_WIDTH+10, 240, 130, 30, hwnd, (HMENU)BUTTON_4, hInst, NULL);
CreateWindow("button", "Выход", WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,
PLOT_WIDTH+10, 320, 130, 30, hwnd, (HMENU)BUTTON_5, hInst, NULL);
return 0;
}
case WM_COMMAND:
{
if (LOWORD(wParam) >= BUTTON_1 && LOWORD(wParam) < BUTTON_4)
{
EnableWindow((HWND)lParam, (WPARAM)false);
mem[LOWORD(wParam) - 1] = 0;
switch(LOWORD(wParam))
{
case BUTTON_1: {polygon.calcNewCoords(rotate); break;}
case BUTTON_2: {polygon.calcNewCoords(trans); break;}
case BUTTON_3: {polygon.calcNewCoords(scale); break;}
default: break;
}
}
if (LOWORD(wParam) == BUTTON_4)
{
for (int i = BUTTON_1; i < BUTTON_4; ++i)
{
mem[i - 1] = 1;
EnableWindow(hb[i - 1], (WPARAM)true);
}
polygon.updateCoords();
}
else if(LOWORD(wParam) == BUTTON_5)
{
SendMessage(hwnd,WM_DESTROY,(WPARAM)0,(LPARAM)0);
}
InvalidateRect(childhwnd, NULL, true);
return 0;
}
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
LRESULT CALLBACK _export ChildWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rc;
switch (msg)
{
case WM_CREATE:
{
return 0;
}
case WM_PAINT:
{
hdc = BeginPaint(hwnd,&ps);
GetClientRect(hwnd,&rc);
DrawAxises(hdc, rc.right, rc.bottom);
SetMapMode(hdc, MM_ANISOTROPIC);
SetWindowExtEx(hdc,1,-1,NULL);
SetViewportExtEx(hdc,1,1,NULL);
SetViewportOrgEx(hdc,PLOT_WIDTH/2,PLOT_HEIGHT/2,NULL);
polygon.draw(hdc, RGB(100, 173, 217), true);
polygon.draw(hdc, RGB(51, 102, 255), false);
EndPaint(hwnd,&ps);
return 0;
}
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
/* Функция выполняет вывод на экран координатных осей */
void DrawAxises(HDC hdc, int Width, int Height)
{
POINT p[3];
p[0].x = Width / 2;
p[0].y = 0;
p[1].x = Width / 2;
p[1].y = Height ;
Polyline(hdc, p, 2);
p[0].x = 0;
p[0].y = Height /2;
p[1].x = Width;
p[1].y = Height /2 ;
Polyline(hdc, p, 2);
p[0].x = Width / 2 - 5;
p[0].y = 10;
p[1].x = p[0].x + 5;
p[1].y = p[0].y - 10;
p[2].x = p[1].x + 5;
p[2].y = p[1].y + 10;
Polyline(hdc, p, 3);
p[0].x = Width - 10;
p[0].y = Height / 2 - 5;
p[1].x = p[0].x + 10;
p[1].y = p[0].y + 5;
p[2].x = p[1].x - 10;
p[2].y = p[1].y + 5;
Polyline(hdc, p, 3);
TextOut(hdc, Width / 2 - 10, Height / 2 + 2, "0", 1);
TextOut(hdc, Width - 20, Height / 2 + 2, "X", 1);
TextOut(hdc, Width / 2 - 20, 2, "Y", 1);
}