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
| #include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <cmath>
#define PI 3.14
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int windowwidth = 660, windowheight = 540;
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow) {
WNDCLASSEX wcex;
LPCSTR szWindowClass = "myclass";
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(0);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
if (!RegisterClassEx(&wcex)) {
MessageBox(NULL, "Call to RegisterClassEx failed!", "Win32 Guided Tour",NULL);
return 1;
}
HINSTANCE hInst = hInstance;
HWND hWnd = CreateWindow(szWindowClass,
"Graph",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
windowwidth, windowheight,
NULL,
NULL,
hInstance,
NULL
);
if (!hWnd) {
MessageBox(NULL,
_T("Call to CreateWindow failed!"),
_T("Win32 Guided Tour"),
NULL);
return 1;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
// Main message loop:
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
}
float y(float x) {
if ( x<0 )
return x * x;
else
if (x <= 2.5)
return 1 +3 * log(abs(1 - x * x * x));
else
return cos(2.3 * x - 1);
}
float round(float d) {
return floor(d + 0.5);
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
PAINTSTRUCT ps;
HDC hdc = GetDC(hWnd);
RECT r;
GetWindowRect(hWnd, &r);
float a = 0, b = 0, x = 0, h = 0, max = 0, min = 0, mx = 0, my = 0, dx = 0;
int x0 = 0, y0 = 0, i = 0, n = 0;
a = -PI / 4;
b = 7 * PI / 4;
h = PI / 4;
x = a;
int pos = 0;
switch (message) {
case WM_PAINT:
BeginPaint(hWnd, &ps);
while (x <= b) {
char msg[200] = {0};
strcpy(msg, "x=");
char num[32];
sprintf(num, "%5.2g", x);
strcat(msg, num);
strcat(msg, " y=");
memset(num, 0, sizeof(num));
sprintf(num, "%7.3g", y(x));
strcat(msg, num);
TextOut(hdc, 0, pos, msg, _tcslen(msg));
pos += 20;
x = x + h;
}
TextOut(hdc, 0, pos, "Нажмите Enter для просмотра графика", _tcslen("Нажмите Enter для просмотра графика"));
EndPaint(hWnd, &ps);
break;
case WM_KEYDOWN:
if (wParam == VK_RETURN) {
BeginPaint(hWnd, &ps);
x0 = round(-windowwidth * a / (b - a));
y0 = windowheight / 2;
mx = (windowwidth-50) / (b - a); // масштаб по Х
x = a;
max = y(x);
min = max;
while (x < b) {
x = x + 0.01;
if (y(x) > max)
max = y(x);
if (y(x) < min)
min = y(x);
}
if (max > abs(min))
my = (y0 + 20) / max; // масштаб по Y
else
my = (y0 + 20) / abs(min);
n = 7;
if (max > n)
n = int(max) + 1;
if (abs(min) > n)
n = int(abs(min)) + 1;
MoveToEx(hdc, 0, y0, NULL);
LineTo(hdc, windowwidth, y0);
MoveToEx(hdc, x0, 0, NULL);
LineTo(hdc, x0, windowheight);
for (i = 1; i <= n; ++i) { // максимальное количество засечек в одну сторону
MoveToEx(hdc, x0 + round(i * PI * mx / 4), y0 - 3, NULL);
LineTo(hdc, x0 + round(i * PI * mx / 4), y0 + 3);
MoveToEx(hdc, x0 - round(i * PI * mx / 4),y0 - 3, NULL);
LineTo(hdc, x0 - round(i * PI * mx / 4), y0 + 3);
MoveToEx(hdc, x0 + 3, y0 - round(i * my), NULL);
LineTo(hdc, x0 - 3, y0-round(i * my));
MoveToEx(hdc, x0 + 3, y0 + round(i * my), NULL);
LineTo(hdc, x0 - 3, y0 + round(i * my));
// подпись оси Х
if (i == 1) {
TextOut(hdc, x0 + round(mx * PI / 4) - 15, y0 + 10, "pi/4", _tcslen("pi/4"));
TextOut(hdc, x0 - round(mx * PI / 4) - 15, y0 + 10, "-pi/4", _tcslen("-pi/4"));
} else {
char msg[20] = {0};
char num[32] = {0};
sprintf(num, "%d", i);
strcpy(msg, num);
strcat(msg, "pi/4");
TextOut(hdc, x0 + round(mx * i * PI / 4) - 15, y0 + 10, msg, _tcslen(msg));
memset(num, 0, sizeof(num));
sprintf(num, "%d", -i);
strcpy(msg, num);
strcat(msg, "pi/4");
TextOut(hdc, x0 - round(mx * i * PI / 4), y0 + 10, msg, _tcslen(msg));
}
// подписи по оси Y
char s[32] = {0};
sprintf(s, "%d", i);
TextOut(hdc, x0 - 25, y0 - round(i * my) - 10, s, _tcslen(s));
char msg[20] = {0};
strcpy(msg, "-");
strcat(msg, s);
TextOut(hdc, x0 - 30, y0 + round(i * my) - 10, msg, _tcslen(msg));
}
// центр
TextOut(hdc, x0 + 5, y0 + 10, "0", _tcslen("0"));
// подписи концов осей
TextOut(hdc, windowwidth - 15, y0 - 25, "X", _tcslen("X"));
TextOut(hdc, x0 + 5, 8, "Y", _tcslen("Y"));
// график
x = a;
dx = 0.001;
while (x <= b) {
x = x + dx;
SetPixel(hdc, x0 + round(x * mx), y0 - round(y(x) * my), RGB(0, 0, 255));
}
SetTextColor(hdc, RGB(0, 0, 255));
TextOut(hdc, x0 + 250, 10, " x<0 y=x^2", _tcslen(" x<0 y=x^2"));
TextOut(hdc, x0 + 250, 30, " 0<=x<=2.5 y=1+3*ln|1-x*x*x|", _tcslen(" 0<=x<=2.5 y=1+3*ln|1-x*x*x|"));
TextOut(hdc, x0 + 250, 50, " x>2.5 y=cos(2.3*x-1)", _tcslen(" x>2.5 y=cos(2.3*x-1)"));
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
break;
}
ReleaseDC(hWnd, hdc);
return 0;
} |