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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
| unit uCanvasUtils;
interface
uses
//============================== Модули проекта ==============================
//=============================== Левые модули ===============================
//=================== Системные модули, добавленные вручную ==================
Types, UITypes, Classes, Controls, ComCtrls, StdCtrls, Math,
Graphics, Generics.Collections;
const
COLOR_SINUS = clRed;
COLOR_AXIS = clYellow;
COLOR_GRID = clGreen;
type
TArrowStyle = (asToP1, asToP2, asBoth);
procedure Connect2Points(cv: TCanvas; p1, p2: TPoint);
procedure DiagonLBRT(cv: TCanvas; const r: TRect);
procedure Diameter(cv: TCanvas; const cp: TPoint; const radius: Integer; const angle: Single);
procedure DrawAxis(cv: TCanvas; const r: TRect; const cp: TPoint);
procedure DrawWave(cv: TCanvas; const R: TRect; const Freq, Phs, Scale: Single);
procedure DrawCross(cv: TCanvas; x, y, radius: Integer);
procedure DrawMarkerC(cv: TCanvas; x, y, radius: Integer);
procedure DrawMarkerR(cv: TCanvas; x, y, radius: Integer);
procedure PaintConnect(cv: TCanvas; Rect1, Rect2: TRect);
procedure DrawArrow(Canvas: TCanvas; X1, Y1, X2, Y2: Integer; LW: Extended); overload; // рисование стрелки
procedure DrawArrow(cv: TCanvas; p1, p2: TPoint; LW: Extended); overload; // рисование стрелки
procedure DrawBackgroundGrid(ACanvas: TCanvas;
const AArea: TRect;
const AStep: Integer;
const AColor1, AColor2: TColor);
function GetTextPosition(const Text: string; // текст
const Canvas: TCanvas; // канва
const Rect: TRect; // прямоугольник отрисовки
const TextMarginHrz: Integer; // размер гориз. отступов
const TextMarginVrt: Integer; // размер вертик. отступов
const Alignment: TAlignment= taCenter; // гориз. выравнивание
const Layout: TTextLayout = tlCenter): TPoint; // верт. выравнивание
procedure DrawTextPosition(const Text: string; // текст
const Canvas: TCanvas; // канва
const Rect: TRect; // прямоугольник отрисовки
const TextMarginHrz: Integer; // размер гориз. отступов
const TextMarginVrt: Integer; // размер вертик. отступов
const Alignment: TAlignment = taCenter; // гориз. выравнивание
const Layout: TTextLayout = tlCenter); // верт. выравнивание
function NewPen(const AColor: TColor = clBlack;
const AWidth: int32 = 1;
const AStyle: TPenStyle = psSolid;
const AMode: TPenMode = pmCopy): TPen;
function NewBrush(const AColor: TColor = clWhite;
const AStyle: TBrushStyle = bsSolid): TBrush;
implementation
procedure Connect2Points(cv: TCanvas; p1, p2: TPoint);
begin
cv.MoveTo(p1.X, p1.Y);
cv.LineTo(p2.X, p2.Y);
end;
procedure DiagonLBRT(cv: TCanvas; const r: TRect);
begin
Connect2Points(cv, Point(r.Left, r.Bottom), Point(r.Right, r.Top));
end;
function RotatePoint(const APt, ASc: TPoint; const AAngle: Single): TPoint;
var
r: Single;
psi: Single;
begin
r:= ASc.Distance(APt);
psi:= ASc.Angle(APt) + AAngle;
Result.X:= Round(ASc.X - r * cos(psi));
Result.Y:= Round(ASc.Y - r * sin(psi));
end;
procedure Diameter(cv: TCanvas; const cp: TPoint; const radius: Integer; const angle: Single);
var
p1, p2: TPoint;
begin
p1:= RotatePoint(Point(cp.X + radius, cp.Y), cp, angle);
p2:= RotatePoint(Point(cp.X - radius, cp.Y), cp, angle);
Connect2Points(cv, p1, p2);
end;
procedure DrawAxis(cv: TCanvas; const r: TRect; const cp: TPoint);
begin
Connect2Points(cv, Point(cp.X, r.Top), Point(cp.X, r.Bottom));
Connect2Points(cv, Point(r.Left, cp.Y), Point(r.Right, cp.Y));
end;
procedure DrawWave(cv: TCanvas; const R: TRect; const Freq, Phs, Scale: Single);
var
x, y, idx: Integer;
psi: Single;
mult: Single;
begin
if (R.Width < 2) or (Freq < 0)
then Exit;
// grid
cv.Pen.Color:= COLOR_GRID;
cv.Pen.Width:= 1;
for idx:= 1 to 9 do
begin
x:= Round(idx * R.Width / 10);
Connect2Points(cv, Point(x, R.Top), Point(x, R.Bottom));
end;
for idx:= 1 to 7 do
begin
y:= Round(idx * R.Height / 8);
Connect2Points(cv, Point(R.Left, y), Point(R.Right, y));
end;
// axis
cv.Pen.Color:= COLOR_AXIS;
Connect2Points(cv, Point(R.Left, R.CenterPoint.Y), Point(R.Right, R.CenterPoint.Y));
Connect2Points(cv, Point(R.CenterPoint.X, R.Top), Point(R.CenterPoint.X, R.Bottom));
mult:= Freq / 100.0;
//cv.MoveTo(-2, R.CenterPoint.Y);
cv.Pen.Color:= COLOR_SINUS;
cv.Pen.Width:= 2;
for x:= 0 to R.Width - 1 do
begin
psi:= 2 * pi * (x - R.CenterPoint.X) * mult / (R.Width - 1);
y:= R.CenterPoint.Y + Trunc(Scale * 0.5 * R.Height * -sin(psi + Phs));
if x <> 0
then cv.LineTo(x, y)
else cv.MoveTo(x, y);
end;
end;
procedure DrawCross(cv: TCanvas; x, y, radius: Integer);
begin
Connect2Points(cv, Point(x - radius, y), Point(x + radius, y));
Connect2Points(cv, Point(x, y - radius), Point(x, y + radius));
end;
procedure DrawMarkerC(cv: TCanvas; x, y, radius: Integer);
var
r: TRect;
begin
r:= Rect(x - radius + 1, y - radius + 1, x + radius, y + radius);
//cv.Brush.Color:= COLOR_MARKER;
cv.Ellipse(r);
end;
procedure DrawMarkerR(cv: TCanvas; x, y, radius: Integer);
var
pts: array[0..3] of TPoint;
begin
pts[0]:= Point(x - radius, y); // L
pts[1]:= Point(x, y - radius); // T
pts[2]:= Point(x + radius, y); // R
pts[3]:= Point(x, y + radius); // B
//cv.Brush.Color:= COLOR_MARKER;
cv.Polygon(pts);
end;
function Square(const cp: TPoint; const radius: int32): TRect;
begin
Result:= Rect(cp.X - radius, cp.Y - radius, cp.X + radius, cp.Y + radius);
end;
{-------------------------- Прорисовка соединения -----------------------------}
procedure PaintConnect(cv: TCanvas; Rect1, Rect2: TRect);
const
SArrow = 7;
LArrow = 9;
var
R1, R2, x1, x2, y1, y2, Lx1, Lx2, Ly1, Ly2: Integer;
//----------- Окружность, описанная около прямоугольника -----------
procedure _Circle(Rect: TRect);
var
x1, y1, x2, y2, r: Integer;
begin
with Rect do
begin
r := Round((SqRt(SqR(Bottom - Top) + SqR(Right - Left)) / 2));
x1 := Left + Round((Right - Left) / 2) - r;
x2 := Left + Round((Right - Left) / 2) + r;
y1 := Top + Round((Bottom - Top) / 2) - r;
y2 := Top + Round((Bottom - Top) / 2) + r;
end;
cv.Ellipse(x1, y1, x2, y2);
end;
//---------- Линия-коннектор между двумя окружностями -----------
procedure _CutLine(x1, y1, x2, y2, R1, R2: Integer);
var
dx1, dx2, dy1, dy2, L: Integer;
begin
L := Round(SqRt(SqR(x2 - x1) + SqR(y2 - y1)));
dx1 := Round((x2 - x1) * (R1 / L));
dy1 := -Round((y1 - y2) * (R1 / L));
dx2 := -Round((x2 - x1) * (R2 / L));
dy2 := Round((y1 - y2) * (R2 / L));
Lx1 := x1 + dx1;
Ly1 := y1 + dy1;
Lx2 := x2 + dx2;
Ly2 := y2 + dy2;
cv.MoveTo(Lx1, Ly1);
cv.LineTo(Lx2, Ly2);
end;
//------------- Отрезок --------------
procedure _Line(x1, y1, x2, y2: Integer);
begin
cv.MoveTo(x1, y1);
cv.LineTo(x2, y2);
end;
//--------- Наконечники стрелки -----------
procedure _Arrow(x1, y1, x2, y2: Integer; Style: TArrowStyle);
var
B, C: TPoint;
Sinus, Cosinus: Single;
L, H: Integer;
begin
L := Round(SqRt(SqR(x2 - x1) + SqR(y2 - y1)));
Sinus := (y1 - y2) / L;
Cosinus := (x2 - x1) / L;
B.x := Round(LArrow * Cosinus - SArrow * Sinus / 2);
B.y := Round(LArrow * Sinus + SArrow * Cosinus / 2);
C.x := Round(LArrow * Cosinus + SArrow * Sinus / 2);
C.y := Round(LArrow * Sinus - SArrow * Cosinus / 2);
H := Round(SqRt(SqR(LArrow) + SqR(SArrow) / 4));
case Style of
asToP1:
cv.Pie(x1 - H, y1 - H, x1 + H, y1 + H, x1 + C.x, y1 - C.y, x1 + B.x, y1 - B.y);
asToP2:
cv.Pie(x2 - H, y2 - H, x2 + H, y2 + H, x2 - C.x, y2 + C.y, x2 - B.x, y2 + B.y);
asBoth:
begin
cv.Pie(x1 - H, y1 - H, x1 + H, y1 + H, x1 + C.x, y1 - C.y, x1 + B.x, y1 - B.y);
cv.Pie(x2 - H, y2 - H, x2 + H, y2 + H, x2 - C.x, y2 + C.y, x2 - B.x, y2 + B.y);
end;
end; //case
end;
// Вычисление радиуса окружности, описывающей прямоугольник
function _RectRadius(Rect:TRect):Integer;
begin
with Rect do
Result:=Round(SqRt(SqR(Right-Left)+SqR(Bottom-Top))/2);
end;
begin //PaintConnect
//C := cv.Pen.Color;
//OldStyle := cv.Brush.Style;
//OldMode := cv.Pen.Mode;
//cv.Pen.Color := Color;
//cv.Brush.Style := bsClear;
//cv.Brush.Color:=Color;
//cv.Pen.Mode := pmNotXor;
//CorrectRect(Rect1, ScrollBox.HorzScrollBar.Position, ScrollBox.VertScrollBar.Position);
//CorrectRect(Rect2, ScrollBox.HorzScrollBar.Position, ScrollBox.VertScrollBar.Position);
R1 := _RectRadius(Rect1);
R2 := _RectRadius(Rect2);
x1 := Rect1.CenterPoint().x;
y1 := Rect1.CenterPoint().y;
x2 := Rect2.CenterPoint().x;
y2 := Rect2.CenterPoint().y;
_Circle(Rect1);
_Circle(Rect2);
_CutLine(x1, y1, x2, y2, R1, R2);
//cv.Brush.Style := bsSolid;
//cv.Brush.Color := Color;
// _Arrow(Lx1, Ly1, Lx2, Ly2, asBoth);
//cv.Brush.Style := OldStyle;
//cv.Pen.Color := C;
//cv.Brush.Color := C;
//cv.Pen.Mode := OldMode;
end;
procedure DrawArrowHead(Canvas: TCanvas; X,Y: Integer; Angle, LW: Extended);
var
A1,A2: Extended;
Arrow: array[0..3] of TPoint;
OldWidth: Integer;
const
Beta = 0.322;
LineLen = 4.74;
CentLen = 3;
begin
Angle:=Pi+Angle;
Arrow[0]:=Point(X,Y);
A1:=Angle-Beta;
A2:=Angle+Beta;
Arrow[1]:=Point(X+Round(LineLen*LW*Cos(A1)),Y-Round(LineLen*LW*Sin(A1)));
Arrow[2]:=Point(X+Round(CentLen*LW*Cos(Angle)),Y-Round(CentLen*LW*Sin(Angle)));
Arrow[3]:= Point(x + Round(LineLen * LW * cos(A2)),
y - Round(LineLen * LW * sin(A2)));
OldWidth := Canvas.Pen.Width;
Canvas.Pen.Width:= 1;
Canvas.Polygon(Arrow);
Canvas.Pen.Width:= OldWidth
end;
procedure DrawArrow(Canvas: TCanvas; X1, Y1, X2, Y2: Integer; LW: Extended);
var
angle: Extended;
begin
//спёр отсюда: http://www.programmersclub.ru/нарисовать-стрелку-на-делфи/
angle:= ArcTan2(Y1 - Y2, X2 - X1);
Canvas.MoveTo(X1, Y1);
Canvas.LineTo(X2 - Round(2 * LW * cos(angle)),
Y2 + Round(2 * LW * sin(angle)));
DrawArrowHead(Canvas, X2, Y2, angle, LW);
end;
procedure DrawArrow(cv: TCanvas; p1, p2: TPoint; LW: Extended);
begin
DrawArrow(cv, p1.x, p1.y, p2.x, p2.y, LW);
end;
//рисование клеток фона
procedure DrawBackgroundGrid(ACanvas: TCanvas; const AArea: TRect;
const AStep: Integer; const AColor1, AColor2: TColor);
var
Row, Col, RW, RH: Integer;
Colors: array [Boolean] of TColor;
ColorSwitcher: Boolean;
begin
Colors[False]:= AColor1;
Colors[True]:= AColor2;
// ColorSwitcher:= False;
RW:= RectWidth(AArea);
RH:= RectHeight(AArea);
ACanvas.Brush.Style:= bsSolid;
//ACanvas.Brush.Color:= AColor1;
//ACanvas.Rectangle(AArea); а не тут ли?
Row:= 0;
repeat
Col:= 0;
ColorSwitcher:= Boolean(Row and 1);
ACanvas.Brush.Color:= Colors[ColorSwitcher]; // начальный цвет (серый $C0C0C0)
ACanvas.Pen.Color:= ACanvas.Brush.Color;
repeat
//ACanvas.Brush.Color:= Colors[ColorSwitcher];
//ACanvas.Pen.Color:= ACanvas.Brush.Color;
//ColorSwitcher:= not ColorSwitcher;
ACanvas.Rectangle(AArea.Left + Col * AStep,
AArea.Top + Row * AStep,
AArea.Left + (Col + 1) * AStep,
AArea.Top + (Row + 1) * AStep);
Inc(Col);
ColorSwitcher:= not ColorSwitcher;
ACanvas.Brush.Color:= Colors[ColorSwitcher];
ACanvas.Pen.Color:= ACanvas.Brush.Color;
until Col * AStep > RW;
Inc(Row);
until Row * AStep > RH;
end;
// Начальная точка вывода текста на канве внутри прямоугольника Rect
// с выравниванием горизонтальным и вертикальным
// для вывода предельных величин графика функции (по углам)
function GetTextPosition(const Text: string; // текст
const Canvas: TCanvas; // канва
const Rect: TRect; // прямоугольник отрисовки
const TextMarginHrz: Integer; // размер гориз. отступов
const TextMarginVrt: Integer; // размер вертик. отступов
const Alignment: TAlignment; // гориз. выравнивание
const Layout: TTextLayout): TPoint; // верт. выравнивание
var
th, tw: Integer;
begin
tw:= Canvas.TextWidth(Text);
th:= Canvas.TextHeight(Text);
case Alignment of
taLeftJustify : Result.x:= TextMarginHrz;
taCenter : Result.x:= ((Rect.Right - Rect.Left) div 2) - (tw div 2);
taRightJustify : Result.x:= (Rect.Right - Rect.Left) - tw - TextMarginHrz;
end; //case
case Layout of
tlTop: Result.y:= TextMarginVrt;
tlCenter: Result.y:= ((Rect.Bottom - Rect.Top) div 2) - (th div 2);
tlBottom: Result.y:= (Rect.Bottom - Rect.Top) - th - TextMarginVrt;
end; // case
end;
procedure DrawTextPosition(const Text: string; // текст
const Canvas: TCanvas; // канва
const Rect: TRect; // прямоугольник отрисовки
const TextMarginHrz: Integer; // размер гориз. отступов
const TextMarginVrt: Integer; // размер вертик. отступов
const Alignment: TAlignment; // гориз. выравнивание
const Layout: TTextLayout); // верт. выравнивание
var
tp: TPoint;
begin
tp:= GetTextPosition(Text, Canvas, Rect, TextMarginHrz, TextMarginVrt, Alignment, Layout);
Canvas.TextRect(Rect, Rect.Left + tp.X, Rect.Top + tp.Y, Text);
//Canvas.TextOut(Rect.Left + tp.X, Rect.Top + tp.Y, Text);
end;
function NewPen(const AColor: TColor = clBlack;
const AWidth: int32 = 1;
const AStyle: TPenStyle = psSolid;
const AMode: TPenMode = pmCopy): TPen;
begin
result:= TPen.Create();
result.Color:= AColor;
result.Width:= AWidth;
result.Style:= AStyle;
result.Mode:= AMode;
end;
function NewBrush(const AColor: TColor = clWhite;
const AStyle: TBrushStyle = bsSolid): TBrush;
begin
result:= TBrush.Create();
result.Color:= AColor;
result.Style:= AStyle;
end;
end. |