-
Notifications
You must be signed in to change notification settings - Fork 14
/
AsciiImage.Shapes.pas
164 lines (137 loc) · 3.57 KB
/
AsciiImage.Shapes.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
unit AsciiImage.Shapes;
interface
uses
{$if CompilerVersion > 22}
System.Types,
{$IfEnd}
Generics.Collections,
AsciiImage.RenderContext.Types,
AsciiImage.RenderContext.Intf;
type
TAsciiShape = class
private
FScaledPoints: TList<TPointF>;
FPoints: TList<TPointF>;
FScaleX: Single;
FScaleY: Single;
procedure SetScaleX(const Value: Single);
function GetScaledPoints: TList<TPointF>;
procedure SetScaleY(const Value: Single);
public
constructor Create();
destructor Destroy(); override;
procedure Draw(const AContext: IRenderContext); virtual; abstract;
property Points: TList<TPointF> read FPoints;
property ScaledPoints: TList<TPointF> read GetScaledPoints;
property ScaleX: Single read FScaleX write SetScaleX;
property ScaleY: Single read FScaleY write SetScaleY;
end;
TAsciiEllipsis = class(TAsciiShape)
protected
function GetRect(): TRectF;
public
procedure Draw(const AContext: IRenderContext); override;
end;
TAsciiPath = class(TAsciiShape)
public
procedure Draw(const AContext: IRenderContext); override;
end;
TAsciiDot = class(TAsciiShape)
public
procedure Draw(const AContext: IRenderContext); override;
end;
TAsciiLine = class(TAsciiShape)
public
procedure Draw(const AContext: IRenderContext); override;
end;
implementation
{ TAsciiShape }
constructor TAsciiShape.Create;
begin
inherited;
FPoints := TList<TPointF>.Create();
FScaledPoints := TList<TPointF>.Create();
end;
destructor TAsciiShape.Destroy;
begin
FPoints.Free;
FScaledPoints.Free;
end;
function TAsciiShape.GetScaledPoints: TList<TPointF>;
var
LPoint: TPointF;
begin
if FScaledPoints.Count = 0 then
begin
for LPoint in Points do
begin
FScaledPoints.Add(PointF(LPoint.X*ScaleX + ScaleX/2, LPoint.Y*ScaleY + ScaleY / 2));
end;
end;
Result := FScaledPoints;
end;
procedure TAsciiShape.SetScaleX(const Value: Single);
begin
FScaleX := Value;
FScaledPoints.Clear;
end;
procedure TAsciiShape.SetScaleY(const Value: Single);
begin
if FScaleY <> Value then
begin
FScaleY := Value;
FScaledPoints.Clear;
end;
end;
{ TAsciiLine }
procedure TAsciiLine.Draw(const AContext: IRenderContext);
begin
AContext.DrawLine(ScaledPoints[0], ScaledPoints[1]);
end;
{ TAsciiDot }
procedure TAsciiDot.Draw(const AContext: IRenderContext);
var
LPoint: TPointF;
LRect: TRectF;
begin
LPoint := ScaledPoints[0];
LRect.Left := LPoint.X - ScaleX / 2;
LRect.Top := LPoint.Y - ScaleY / 2;
LRect.Right := LPoint.X + ScaleX / 2;
LRect.Bottom := LPoint.Y + ScaleY / 2;
AContext.FillRectangle(LRect);
end;
{ TAsciiPath }
procedure TAsciiPath.Draw(const AContext: IRenderContext);
begin
AContext.DrawPolygon(ScaledPoints.ToArray);
end;
{ TAsciiEllipsis }
procedure TAsciiEllipsis.Draw(const AContext: IRenderContext);
begin
AContext.DrawEllipsis(GetRect());
end;
function TAsciiEllipsis.GetRect: TRectF;
var
LPoint: TPointF;
const
CHighSingle = 10000;
CLowSingle = -10000;
begin
Result.Left := CHighSingle;
Result.Top := CHighSingle;
Result.Right := CLowSingle;
Result.Bottom := CLowSingle;
for LPoint in ScaledPoints do
begin
if LPoint.X < Result.Left then
Result.Left := LPoint.X;
if LPoint.X > Result.Right then
Result.Right := LPoint.X;
if LPoint.Y < Result.Top then
Result.Top := LPoint.Y;
if LPoint.Y > Result.Bottom then
Result.Bottom := LPoint.Y;
end;
end;
end.