-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview.cpp
326 lines (286 loc) · 7.77 KB
/
view.cpp
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
#include "view.h"
#include <math.h>
#include "globals.h"
#include "expression.h"
#include "main.h"
#include "bitmap.h"
static HWND m_hwnd;
static COLORREF m_crpaper = RGB(255, 255, 255);
static COLORREF m_craxis = RGB(0, 0, 0);
static COLORREF m_crdots = RGB(0, 0, 0);
static COLORREF m_crgraph = RGB(0, 0, 255);
static HBRUSH m_brpaper;
static HBRUSH m_braxis;
static HBRUSH m_brdots;
static HBRUSH m_brgraph;
static TCHAR m_expression[256];
static int m_pixelsinonex = 10;
static int m_pixelsinoney = 10;
static double m_accuracy[5] = { 0.1, 0.01, 0.001, 0.0001, 0.00001 };
static int m_accuracyi = 2;
static int exp_index, exp_max;
ATOM view_register_class() {
WNDCLASSEX wcex;
ZeroMemory(&wcex, sizeof(wcex));
wcex.cbSize = sizeof(wcex);
wcex.lpszClassName = "view";
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = view_wndproc;
wcex.hInstance = g_hinst;
wcex.hCursor = LoadCursor(NULL, IDC_CROSS);
return RegisterClassEx(&wcex);
}
HWND view_create(HWND hwndParent) {
m_hwnd = CreateWindowEx(0, "view", g_szAppTitle,
WS_CHILD | WS_VISIBLE,
0, 0, 0, 0,
hwndParent, NULL,
g_hinst, NULL);
SendMessage(m_hwnd, WM_SIZE, 0, 0);
return m_hwnd;
}
LRESULT CALLBACK view_wndproc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
PAINTSTRUCT ps;
HDC hdc;
static RECT rv, rc, rr, rs;
static int rrHeight, sbHeight, x, y;
static BOOLEAN bFirstPaint = TRUE;
switch(message) {
case WM_CREATE:
view_set_expression("x^2");
m_brpaper = CreateSolidBrush(m_crpaper);
break;
case WM_DESTROY:
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
view_drawgrid();
if(!g_sizing) view_graph_expression();
break;
case WM_MOUSEMOVE:
main_set_statusxy(LOWORD(lParam), HIWORD(lParam));
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
break;
}
return 0;
}
COLORREF view_get_paperc() {
return m_crpaper;
}
void view_set_paperc(COLORREF c) {
m_crpaper = c;
DeleteObject(m_brpaper);
m_brpaper = CreateSolidBrush(c);
}
COLORREF view_get_axisc() {
return m_craxis;
}
void view_set_axisc(COLORREF c) {
m_craxis = c;
DeleteObject(m_braxis);
m_braxis = CreateSolidBrush(c);
}
COLORREF view_get_dotsc() {
return m_crdots;
}
void view_set_dotsc(COLORREF c) {
m_crdots = c;
DeleteObject(m_brdots);
m_brdots = CreateSolidBrush(c);
}
COLORREF view_get_graphc() {
return m_crgraph;
}
void view_set_graphc(COLORREF c) {
m_crgraph = c;
DeleteObject(m_brgraph);
m_brgraph = CreateSolidBrush(c);
}
void view_set_default_colors() {
m_crpaper = RGB(255, 255, 255);
m_craxis = RGB(0, 0, 0);
m_crdots = RGB(0, 0, 0);
m_crgraph = RGB(0, 0, 255);
DeleteObject(m_brpaper);
DeleteObject(m_braxis);
DeleteObject(m_brdots);
DeleteObject(m_brgraph);
m_brpaper = CreateSolidBrush(m_crpaper);
m_braxis = CreateSolidBrush(m_crpaper);
m_brdots = CreateSolidBrush(m_crdots);
m_brgraph = CreateSolidBrush(m_crgraph);
}
void view_set_default_graphopt() {
m_pixelsinonex = 10;
m_pixelsinoney = 10;
m_accuracyi = 2;
}
int view_get_accuracyi() {
return m_accuracyi;
}
void view_set_accuracyi(int a) {
m_accuracyi = a;
}
int view_get_pixelsinonex() {
return m_pixelsinonex;
}
void view_set_pixelsinonex(int x) {
m_pixelsinonex = x;
}
int view_get_pixelsinoney() {
return m_pixelsinoney;
}
void view_set_pixelsinoney(int y) {
m_pixelsinoney = y;
}
TCHAR* view_get_expression() {
return m_expression;
}
void view_set_expression(TCHAR* e) {
strcpy_s(m_expression, 256, e);
}
void view_redraw() {
RedrawWindow(m_hwnd, NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW);
}
void view_drawgrid() {
RECT rect;
int x;
int y;
HDC dc;
HPEN p;
GetClientRect(m_hwnd, &rect);
dc = GetDC(m_hwnd);
FillRect(dc, &rect, m_brpaper);
p = CreatePen(PS_SOLID, 0, m_craxis);
SelectObject(dc, p);
MoveToEx(dc, 0, rect.bottom/2, 0);
LineTo(dc, rect.right,rect.bottom/2);
MoveToEx(dc, rect.right / 2, 0,0);
LineTo(dc, rect.right / 2, rect.bottom);
//Q1
for (x = rect.right / 2 + m_pixelsinonex; x <= rect.right; x += m_pixelsinonex)
{
for (y = rect.bottom / 2 - m_pixelsinoney; y >= rect.top; y -= m_pixelsinoney)
{
SetPixel(dc, x, y, m_crdots);
}
}
//Q2
for (x = rect.right / 2 - m_pixelsinonex; x >= rect.left; x -= m_pixelsinonex)
{
for (y = rect.bottom / 2 - m_pixelsinoney; y >= rect.top; y -= m_pixelsinoney)
{
SetPixel(dc, x, y, m_crdots);
}
}
//Q3
for (x = rect.right / 2 - m_pixelsinonex; x >= rect.left; x -= m_pixelsinonex)
{
for (y = rect.bottom / 2 + m_pixelsinoney; y <= rect.bottom; y += m_pixelsinoney)
{
SetPixel(dc, x, y, m_crdots);
}
}
//Q4
for (x = rect.right / 2 + m_pixelsinonex; x <= rect.right; x += m_pixelsinonex)
{
for (y = rect.bottom / 2 + m_pixelsinoney; y <= rect.bottom; y += m_pixelsinoney)
{
SetPixel(dc, x, y, m_crdots);
}
}
DeleteObject(p);
ReleaseDC(m_hwnd, dc);
return;
}
void view_graph_expression() {
double x;
double y;
double startx;
double endx;
double lowy;
double highy;
RECT rect;
HDC dc;
GetClientRect(m_hwnd, &rect);
dc = GetDC(m_hwnd);
double acc;
int x0, x1, y0, y1;
startx = (0 - rect.right / 2) / m_pixelsinonex - 1;
endx = (rect.right / 2) / m_pixelsinonex + 1;
lowy = (0 - rect.bottom / 2) / m_pixelsinoney - 1;
highy = (rect.bottom / 2) / m_pixelsinoney + 1;
main_get_cbtext(m_expression);
switch (ParseExpression(m_expression, &exp_index, &exp_max))
{
case 0:
break;
case 1:
MessageBox(m_hwnd, "You didn't enter an expression!", "error", MB_OK);
return;
case 2:
case 3:
MessageBox(m_hwnd, "Illegal character in expression!","error",MB_OK);
return;
case 4:
MessageBox(m_hwnd, "Unmatching parenthises!","error",MB_OK);
return;
default:
break;
}
acc = m_accuracy[m_accuracyi];
x1 = -1;
y1 = -1;
for (x = startx; x <= endx; x += acc)
{
exp_index = 0;
y = -(GetExpression(m_expression, &exp_index, &exp_max, x));
if (y >= lowy && y <= highy)
{
x0 = x1; y0 = y1;
x1 = (int)((x * m_pixelsinonex + rect.right / 2) + .5);
y1 = (int)((y * m_pixelsinoney + rect.bottom / 2) + .5);
if(x0!=x1 || y0!=y1) SetPixel(dc, x1, y1, m_crgraph);
}
}
ReleaseDC(m_hwnd, dc);
}
void view_save() {
HDC wdc, cdc;
HBITMAP bm;
RECT r;
int w, h;
PBITMAPINFO pbmi;
OPENFILENAME ofn;
char sz[256];
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = NULL;
ofn.lpstrFile = sz;
ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = sizeof(sz);
ofn.lpstrFilter = "All (*.*)\0*.*\0Bitmap (*.bmp)\0*.bmp\0";
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.lpstrDefExt = "bmp";
ofn.Flags = 0;
if (!GetSaveFileName(&ofn)) return;
wdc = GetDC(m_hwnd);
cdc = CreateCompatibleDC(wdc);
GetClientRect(m_hwnd, &r);
w = r.right - r.left;
h = r.bottom - r.top;
bm = CreateCompatibleBitmap(wdc, w, h);
SelectObject(cdc, bm);
BitBlt(cdc, 0, 0, w, h, wdc, 0, 0, SRCCOPY);
pbmi = CreateBitmapInfoStruct(m_hwnd, bm);
CreateBMPFile(m_hwnd, sz, pbmi, bm, cdc);
DeleteObject(bm);
ReleaseDC(m_hwnd, wdc);
DeleteDC(cdc);
}