-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathRlcFrm.pas
191 lines (156 loc) · 4.79 KB
/
RlcFrm.pas
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
//This Source Code Form is subject to the terms of the Mozilla Public
//License, v. 2.0. If a copy of the MPL was not distributed with this
//file, You can obtain one at http://mozilla.org/MPL/2.0/.
//Copyright (c) 2014 Alex Shovkoplyas VE3NEA
unit RlcFrm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Types, Plot, Math, Dialogs, ExtCtrls, RlcFit, ImgList;
type
TRlcFrame = class(TFrame)
Panel1: TPanel;
ScrollBox: TScrollBox;
ImageList1: TImageList;
Image1: TImage;
private
FTanks: TTanks;
TankW, RowH, TotalW: integer;
CntC, CntR, CntL: integer;
procedure PaintImage;
public
procedure PlotCircuit(ATanks: TTanks);
end;
implementation
{$R *.dfm}
const
MARG_W = 20;
GAP_W = 10;
MARG_H = 6;
procedure TRlcFrame.PaintImage;
var
W, H, X0, Y0, x, y, dy, p, PrevY: integer;
procedure PutCircles;
begin
if y > PrevY then
with Image1.Picture.Bitmap.Canvas do
begin
MoveTo(x, PrevY);
LineTo(x, y);
MoveTo(x+TankW-1, PrevY);
LineTo(x+TankW-1, y);
Brush.Color := clBlack;
Ellipse(x-2, PrevY-2, x+3, PrevY+3);
Ellipse(x+TankW-3, PrevY-2, x + TankW + 2, PrevY+3);
end;
PrevY := y;
Inc(y, RowH);
end;
procedure PutText(AText: string);
var
Sz: TSize;
begin
with Image1.Picture.Bitmap.Canvas do
begin
Brush.Style := bsClear;
Sz := Image1.Picture.Bitmap.Canvas.TextExtent(AText);
TextOut(x + (TankW - Sz.cx) div 2, y - RowH div 2, AText);
end;
Inc(y, RowH);
end;
begin
W := Image1.Picture.Bitmap.Width;
H := Image1.Picture.Bitmap.Height;
with Image1.Picture.Bitmap.Canvas do
begin
//background
Brush.Color := clWhite;
FillRect(Rect(0,0,W,H));
if Ftanks = nil then Exit;
//origin
X0 := Max(MARG_W, (W - TotalW) div 2);
Y0 := MARG_H + RowH div 2;
//terminals
Brush.Color := clRed;
Pen.Color := Brush.Color;
Ellipse(X0-7, Y0-3, X0, Y0+4);
Ellipse(X0+TotalW, Y0-3, X0+TotalW+7, Y0+4);
Brush.Color := clBlack;
Pen.Color := Brush.Color;
Font.Color := clBlue;
MoveTo(X0, Y0);
x := X0 + GAP_W;
y := Y0;
LineTo(x, Y0);
CntC := 1; CntR := 1; CntL := 1;
if FTanks[0].IsShortCircuit or FTanks[0].IsOpenCircuit then
begin
ImageList1.Draw(Image1.Picture.Bitmap.Canvas, x, y - RowH div 2, 1);
Inc(y, RowH);
if FTanks[0].IsShortCircuit then PutText('R1 0 Ω') else PutText('R1 ∞ Ω');
Inc(x, TankW); MoveTo(x, Y0);
Inc(x, GAP_W+1); LineTo(x, Y0);
end
else
//tanks
for p:=0 to FTanks.Count-1 do
begin
y := Y0;
PrevY := Y0;
if FTanks[p].C <> 0 then
begin
ImageList1.Draw(Image1.Picture.Bitmap.Canvas, x, y - RowH div 2, 0);
PutCircles;
PutText(Format('C%d %sF', [CntC, FormatWithPrefix(FTanks[p].C)]));
Inc(CntC);
end;
if FTanks[p].G <> 0 then
begin
ImageList1.Draw(Image1.Picture.Bitmap.Canvas, x, y - RowH div 2, 1);
PutCircles;
PutText(Format('R%d %sΩ', [CntC, FormatWithPrefix(1 / FTanks[p].G)]));
Inc(CntR);
end;
if (FTanks[p].L <> INFINITY) and (FTanks[p].R <> INFINITY) then
if FTanks[p].R <> 0
then
begin
ImageList1.Draw(Image1.Picture.Bitmap.Canvas, x, y - RowH div 2, 2);
PutCircles;
PutText(Format('L%d %sH', [CntC, FormatWithPrefix(FTanks[p].L)]));
PutText(Format('R%d %sΩ', [CntR, FormatWithPrefix(FTanks[p].R)]));
Inc(CntR); Inc(CntL);
end
else
begin
ImageList1.Draw(Image1.Picture.Bitmap.Canvas, x, y - RowH div 2, 3);
PutCircles;
PutText(Format('L%d %sH', [CntL, FormatWithPrefix(FTanks[p].L)]));
Inc(CntL);
end;
//link to next tank
Inc(x, TankW);
MoveTo(x, Y0);
Inc(x, GAP_W+1);
LineTo(x, Y0);
end;
end;
end;
procedure TRlcFrame.PlotCircuit(ATanks: TTanks);
var
Tank: TTank;
begin
FTanks := ATanks;
ScrollBox.Visible := Ftanks <> nil;
if Ftanks = nil then Exit;
TankW := ImageList1.Width;
RowH := ImageList1.Height;
TotalW := GAP_W + (TankW + GAP_W + 1) * FTanks.Count;
Image1.Picture.Bitmap.PixelFormat := pf24Bit;
Image1.Picture.Bitmap.Height := Image1.Height;
Image1.Picture.Bitmap.Width := Max(ScrollBox.ClientWidth, TotalW + 2 * MARG_W);
Image1.Width := Image1.Picture.Bitmap.Width;
PaintImage;
Image1.Repaint;
end;
end.