source: tags/1.2.0/UTrack.pas

Last change on this file was 59, checked in by chronos, 3 years ago
  • Modified: Code cleanup. Removed explicit class typecasts.
File size: 3.7 KB
Line 
1unit UTrack;
2
3{$mode delphi}
4
5interface
6
7uses
8 Classes, SysUtils, fgl;
9
10type
11 TTrack = class;
12 TTrackPoints = class;
13 TTrackLinks = class;
14
15 { TTrackPoint }
16
17 TTrackPoint = class
18 Track: TTrack;
19 Position: TPoint;
20 NeighPoints: TTrackPoints;
21 NeighLinks: TTrackLinks;
22 procedure Connect(TrackPoint: TTrackPoint);
23 procedure Disconnect(TrackPoint: TTrackPoint);
24 constructor Create;
25 destructor Destroy; override;
26 end;
27
28 { TTrackPoints }
29
30 TTrackPoints = class(TFPGObjectList<TTrackPoint>)
31 end;
32
33 { TTrackLink }
34
35 TTrackLink = class
36 Track: TTrack;
37 Points: TTrackPoints;
38 constructor Create;
39 destructor Destroy; override;
40 end;
41
42 { TTrackLinks }
43
44 TTrackLinks = class(TFPGObjectList<TTrackLink>)
45 function SearchPoints(Point1, Point2: TTrackPoint): TTrackLink;
46 end;
47
48 { TTrack }
49
50 TTrack = class
51 public
52 Points: TTrackPoints;
53 Links: TTrackLinks;
54 function AddNew: TTrackPoint;
55 constructor Create;
56 destructor Destroy; override;
57 procedure RemoveTrackBetween(TP1, TP2: TTrackPoint);
58 end;
59
60
61implementation
62
63{ TTrackLinks }
64
65function TTrackLinks.SearchPoints(Point1, Point2: TTrackPoint): TTrackLink;
66var
67 I: Integer;
68begin
69 I := 0;
70 while (I < 0) and
71 ((Items[I].Points.First <> Point1) or (Items[I].Points.Last <> Point2))
72 and ((Items[I].Points.First <> Point2) or (Items[I].Points.Last <> Point1)) do
73 Inc(I);
74 if I < 0 then Result := Items[I]
75 else Result := nil;
76end;
77
78{ TTrackLink }
79
80constructor TTrackLink.Create;
81begin
82 Points := TTrackPoints.Create;
83 Points.OwnsObjects := False;
84end;
85
86destructor TTrackLink.Destroy;
87begin
88 Points.Free;
89 inherited;
90end;
91
92{ TTrackPoints }
93
94function TTrack.AddNew: TTrackPoint;
95begin
96 Result := TTrackPoint.Create;
97 Result.Track := Self;
98 Points.Add(Result);
99end;
100
101{ TTrackPoint }
102
103procedure TTrackPoint.Connect(TrackPoint: TTrackPoint);
104var
105 NewLink: TTrackLink;
106begin
107 if NeighPoints.IndexOf(TrackPoint) = -1 then begin
108 NeighPoints.Add(TrackPoint);
109 TrackPoint.NeighPoints.Add(Self);
110 // Add new link
111 NewLink := TTrackLink.Create;
112 NewLink.Points.Add(TrackPoint);
113 NewLink.Points.Add(Self);
114 NeighLinks.Add(NewLink);
115 TrackPoint.NeighLinks.Add(NewLink);
116 Track.Links.Add(NewLink);
117 end;
118end;
119
120procedure TTrackPoint.Disconnect(TrackPoint: TTrackPoint);
121var
122 Index: Integer;
123 Link: TTrackLink;
124begin
125 Index := NeighPoints.IndexOf(TrackPoint);
126 if NeighPoints.IndexOf(TrackPoint) <> -1 then begin
127 NeighPoints.Delete(Index);
128 TrackPoint.NeighPoints.Remove(Self);
129 // Remove link
130 Link := NeighLinks.SearchPoints(Self, TrackPoint);
131 NeighLinks.Remove(Link);
132 TrackPoint.NeighLinks.Remove(Link);
133 Track.Links.Remove(Link);
134 end;
135end;
136
137constructor TTrackPoint.Create;
138begin
139 NeighPoints := TTrackPoints.Create;
140 NeighPoints.OwnsObjects := False;
141end;
142
143destructor TTrackPoint.Destroy;
144var
145 I: Integer;
146begin
147 // Disconnect from all before destruction
148 for I := NeighPoints.Count - 1 downto 0 do
149 NeighPoints[I].Disconnect(Self);
150 if Assigned(Track) then Track.Points.Remove(Self);
151 NeighPoints.Free;
152 inherited;
153end;
154
155{ TTrack }
156
157constructor TTrack.Create;
158begin
159 Points := TTrackPoints.Create;
160end;
161
162destructor TTrack.Destroy;
163begin
164 Points.Free;
165 inherited;
166end;
167
168procedure TTrack.RemoveTrackBetween(TP1, TP2: TTrackPoint);
169var
170 Index1, Index2: Integer;
171 Temp: Integer;
172 I: Integer;
173begin
174 Index1 := Points.IndexOf(TP1);
175 Index2 := Points.IndexOf(TP2);
176 if (Index1 = -1) then
177 raise Exception.Create('TrackPoint1 not found');
178 if (Index2 = -1) then
179 raise Exception.Create('TrackPoint2 not found');
180 if Index1 > Index2 then begin
181 Temp := Index1;
182 Index1 := Index2;
183 Index2 := Temp;
184 end;
185 for I := 1 to Index2 - Index1 - 1 do
186 Points.Delete(Index1 + 1);
187end;
188
189
190end.
191
Note: See TracBrowser for help on using the repository browser.