-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathd2dPath.pas
More file actions
177 lines (157 loc) · 3.13 KB
/
d2dPath.pas
File metadata and controls
177 lines (157 loc) · 3.13 KB
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
unit d2dPath;
interface
uses
Classes
;
type
Td2dCommonPath = class
private
f_SegmentsBuilt: Boolean;
f_TotalLength: Single;
procedure CalcTotalLength;
protected
f_Points: TList;
f_Segments: Tlist;
procedure AddSegment(aX1, aY1, aX2, aY2: Single);
procedure ClearPoints;
procedure ClearSegments;
procedure DoRecalcSegments; virtual;
procedure CheckSegments;
public
constructor Create;
destructor Destroy; override;
function AddPoint(const aX, aY: Single): Integer;
procedure Clear;
procedure Recalc;
property TotalLength: Single read f_TotalLength;
end;
Td2dPathCursor = class
private
f_Path: Td2dCommonPath;
f_Speed: Single;
public
constructor Create(aPath: Td2dCommonPath; aSpeed: Single);
end;
implementation
uses
SysUtils,
Math,
d2dTypes;
type
//1 path segment descriptor
Pd2dPathSegment = ^Td2dPathSegment;
Td2dPathSegment = record
X : Single;
Y : Single;
DX : Single;
DY : Single;
Len : Single;
Angle : Single;
end;
constructor Td2dCommonPath.Create;
begin
inherited;
f_Points := TList.Create;
f_Segments := TList.Create;
f_SegmentsBuilt := False;
f_TotalLength := 0;
end;
destructor Td2dCommonPath.Destroy;
begin
Clear;
FreeAndNil(f_Points);
FreeAndNil(f_Segments);
inherited;
end;
function Td2dCommonPath.AddPoint(const aX, aY: Single): Integer;
var
l_New: Pointer;
begin
GetMem(l_New, SizeOf(Td2dPoint));
Result := f_Points.Add(l_New);
f_SegmentsBuilt := False;
end;
procedure Td2dCommonPath.AddSegment(aX1, aY1, aX2, aY2: Single);
var
l_Seg: Pd2dPathSegment;
begin
GetMem(l_Seg, SizeOf(Td2dPathSegment));
with l_Seg^ do
begin
X := aX1;
Y := aY2;
DX := aX2-aX1;
DY := aY2-aY1;
Len := Sqrt(DX*DX+DY*DY);
Angle := ArcCos(DY/Len);
if DX < 0 then
Angle := 2*Pi-Angle;
end;
f_Segments.Add(l_Seg);
end;
procedure Td2dCommonPath.CalcTotalLength;
var
I: Integer;
begin
f_TotalLength := 0;
for I := 0 to Pred(f_Segments.Count) do
f_TotalLength := f_TotalLength + Pd2dPathSegment(f_Segments[I]).Len;
end;
procedure Td2dCommonPath.ClearPoints;
var
I : Integer;
begin
for I := 0 to Pred(f_Points.Count) do
FreeMem(f_Points.Items[I], SizeOf(Td2dPoint));
f_Points.Clear;
f_SegmentsBuilt := False;
end;
procedure Td2dCommonPath.ClearSegments;
var
I : Integer;
begin
for I := 0 to Pred(f_Segments.Count) do
FreeMem(f_Segments.Items[I], SizeOf(Td2dPathSegment));
f_Segments.Clear;
f_SegmentsBuilt := False;
end;
procedure Td2dCommonPath.DoRecalcSegments;
var
I: Integer;
P1, P2: Pd2dPoint;
begin
for I := 1 to Pred(f_Points.Count) do
begin
P1 := f_Points.Items[I-1];
P2 := f_Points.Items[I];
AddSegment(P1.X, P1.Y, P2.X, P2.Y);
end;
end;
procedure Td2dCommonPath.CheckSegments;
begin
if f_SegmentsBuilt then
Exit;
if f_Points.Count < 2 then
Exit;
ClearSegments;
DoRecalcSegments;
CalcTotalLength;
f_SegmentsBuilt := True;
end;
procedure Td2dCommonPath.Clear;
begin
ClearPoints;
ClearSegments;
end;
procedure Td2dCommonPath.Recalc;
begin
f_SegmentsBuilt := False;
CheckSegments;
end;
constructor Td2dPathCursor.Create(aPath: Td2dCommonPath; aSpeed: Single);
begin
inherited Create;
f_Path := aPath;
f_Speed := aSpeed;
end;
end.