source: trunk/Packages/Common/UGeometry.pas

Last change on this file was 19, checked in by chronos, 7 years ago
  • Fixed: Build under Lazarus 1.8.0.
  • Modified: Updated Common package.
File size: 11.7 KB
Line 
1unit UGeometry;
2
3{$mode delphi}
4
5interface
6
7uses
8 Classes, SysUtils, Math;
9
10type
11 TPointArray = array of TPoint;
12
13 { TLine }
14
15 TLine = record
16 private
17 function GetDistance: Double;
18 procedure SetDistance(AValue: Double);
19 public
20 P1: TPoint;
21 P2: TPoint;
22 function Create(const P1, P2: TPoint): TLine;
23 function GetMiddle: TPoint;
24 function GetAngle: Double;
25 function GetSize: TPoint;
26 function ToRect: TRect;
27 function DotProduct: Double;
28 procedure Rotate(const Angle: Double);
29 class operator Equal(const A, B: TLine): Boolean;
30 property Distance: Double read GetDistance write SetDistance;
31 end;
32
33 { TPolygon }
34
35 TPolygon = record
36 Points: TPointArray;
37 function IsPointInside(const P: TPoint): Boolean;
38 function Create(const Points: TPointArray): TPolygon; overload;
39 function Create(const Rect: TRect): TPolygon; overload;
40 procedure AddPoint(const P: TPoint);
41 procedure Clear;
42 procedure CutLine(const Vector: TLine; const PointInside: TPoint);
43 end;
44
45function Distance(const P1, P2: TPoint): Integer;
46function Dot(const P1, P2: TPoint): Double;
47function AddPoint(const P1, P2: TPoint): TPoint;
48function SubPoint(const P1, P2: TPoint): TPoint;
49function PointToLineDistance(const P, V, W: TPoint): Integer;
50function ComparePoint(const P1, P2: TPoint): Boolean;
51function RotatePoint(const Center, P: TPoint; Angle: Double): TPoint;
52function RotatePoints(const Center: TPoint; P: TPointArray; Angle: Double): TPointArray;
53function LineIntersect(const LineA, LineB: TLine; out Intersection: TPoint): Boolean;
54function ArcTan2Point(const Point: TPoint): Float;
55function ArcTanPoint(const Point: TPoint): Float;
56function RectEquals(const A, B: TRect): Boolean;
57function RectEnlarge(const Rect: TRect; Value: Integer): TRect;
58function ShiftRect(const ARect: TRect; Delta: TPoint): TRect;
59function PointsToRect(const P1, P2: TPoint): TRect;
60function PointInRect(const P: TPoint; aRect: TRect): Boolean;
61function HalfDistancePoint(const P1, P2: TPoint): TPoint;
62function NormalizeAngle(const Angle: Double): Double;
63function SubAngle(A1, A2: Double): Double;
64
65implementation
66
67function Distance(const P1, P2: TPoint): Integer;
68begin
69 Result := Trunc(Sqrt(Sqr(P2.X - P1.X) + Sqr(P2.Y - P1.Y)));
70end;
71
72function Dot(const P1, P2: TPoint): Double;
73begin
74 Result := P1.X * P2.X + P1.Y * P2.Y;
75end;
76
77function AddPoint(const P1, P2: TPoint): TPoint;
78begin
79 Result.X := P1.X + P2.X;
80 Result.Y := P1.Y + P2.Y;
81end;
82
83function SubPoint(const P1, P2: TPoint): TPoint;
84begin
85 Result.X := P1.X - P2.X;
86 Result.Y := P1.Y - P2.Y;
87end;
88
89function PointToLineDistance(const P, V, W: TPoint): Integer;
90var
91 l2, t: Double;
92 tt: TPoint;
93begin
94 // Return minimum distance between line segment vw and point p
95 L2 := Distance(V, W); // i.e. |w-v|^2 - avoid a sqrt
96 L2 := Power(l2, 2);
97 if L2 = 0 then begin
98 Result := Distance(P, V); // v == w case
99 Exit;
100 end;
101 // Consider the line extending the segment, parameterized as v + t (w - v).
102 // We find projection of point p onto the line.
103 // It falls where t = [(p-v) . (w-v)] / |w-v|^2
104 T := Dot(SubPoint(P, V), SubPoint(W, V)) / L2;
105 if T < 0 then begin
106 Result := Distance(P, V); // Beyond the 'v' end of the segment
107 exit;
108 end
109 else if T > 1 then begin
110 Result := Distance(P, W); // Beyond the 'w' end of the segment
111 Exit;
112 end;
113 TT.X := Trunc(V.X + T * (W.X - V.X));
114 TT.Y := Trunc(V.Y + T * (W.Y - V.Y));
115 Result := Distance(P, TT);
116end;
117
118function ComparePoint(const P1, P2: TPoint): Boolean;
119begin
120 Result := (P1.X = P2.X) and (P1.Y = P2.Y);
121end;
122
123function RotatePoint(const Center, P: TPoint; Angle: Double): TPoint;
124var
125 D: TPoint;
126begin
127 D := Point(P.X - Center.X, P.Y - Center.Y);
128 Result := Point(Center.X + Round(D.X * Cos(Angle) - D.Y * Sin(Angle)),
129 Center.Y + Round(D.X * Sin(Angle) + D.Y * Cos(Angle)));
130end;
131
132function RotatePoints(const Center: TPoint; P: TPointArray; Angle: Double): TPointArray;
133var
134 I: Integer;
135begin
136 SetLength(Result, Length(P));
137 for I := 0 to High(P) do
138 Result[I] := RotatePoint(Center, P[I], Angle);
139end;
140
141function LineIntersect(const LineA, LineB: TLine; out Intersection: TPoint): Boolean;
142Var
143 LDetLineA, LDetLineB, LDetDivInv: Double;
144 LDiffLA, LDiffLB: TPoint;
145 D: Integer;
146begin
147 if ComparePoint(LineA.P1, LineA.P2) or ComparePoint(LineB.P1, LineB.P2) then begin
148 Result := False;
149 Exit;
150 end;
151 LDetLineA := LineA.P1.X * LineA.P2.Y - LineA.P1.Y * LineA.P2.X;
152 LDetLineB := LineB.P1.X * LineB.P2.Y - LineB.P1.Y * LineB.P2.X;
153
154 LDiffLA := SubPoint(LineA.P1, LineA.P2);
155 LDiffLB := SubPoint(LineB.P1, LineB.P2);
156
157 D := (LDiffLA.X * LDiffLB.Y) - (LDiffLA.Y * LDiffLB.X);
158 if D = 0 then begin
159 // Parallel lines without intersection
160 Result := False;
161 Exit;
162 end;
163 LDetDivInv := 1 / D;
164
165 Intersection.X := Round(((LDetLineA * LDiffLB.X) - (LDiffLA.X * LDetLineB)) * LDetDivInv);
166 Intersection.Y := Round(((LDetLineA * LDiffLB.Y) - (LDiffLA.Y * LDetLineB)) * LDetDivInv);
167 Result := True;
168end;
169
170function ArcTan2Point(const Point: TPoint): Float;
171begin
172 Result := ArcTan2(Point.Y, Point.X);
173end;
174
175function ArcTanPoint(const Point: TPoint): Float;
176begin
177 if Point.Y = 0 then Result := Infinity
178 else Result := ArcTan(Point.X / Point.Y);
179end;
180
181function RectEquals(const A, B: TRect): Boolean;
182begin
183 Result := (A.Left = B.Left) and (A.Top = B.Top) and
184 (A.Right = B.Right) and (A.Bottom = B.Bottom);
185end;
186
187function RectEnlarge(const Rect: TRect; Value: Integer): TRect;
188begin
189 Result.Left := Rect.Left - Value;
190 Result.Right := Rect.Right + Value;
191 Result.Top := Rect.Top - Value;
192 Result.Bottom := Rect.Bottom + Value;
193end;
194
195function ShiftRect(const ARect: TRect; Delta: TPoint): TRect;
196begin
197 Result := Rect(ARect.Left + Delta.X, ARect.Top + Delta.Y,
198 ARect.Right + Delta.X, ARect.Bottom + Delta.Y);
199end;
200
201function PointsToRect(const P1, P2: TPoint): TRect;
202begin
203 if P1.X < P2.X then Result.Left := P1.X else Result.Left := P2.X;
204 if P1.Y < P2.Y then Result.Top := P1.Y else Result.Top := P2.Y;
205 if P1.X > P2.X then Result.Right := P1.X else Result.Right := P2.X;
206 if P1.Y > P2.Y then Result.Bottom := P1.Y else Result.Bottom := P2.Y;
207end;
208
209function PointInRect(const P: TPoint; aRect: TRect): Boolean;
210begin
211 Result := (P.X >= aRect.Left) and (P.X <= aRect.Right) and
212 (P.Y >= aRect.Top) and (P.Y <= aRect.Bottom);
213end;
214
215function HalfDistancePoint(const P1, P2: TPoint): TPoint;
216begin
217 Result := Point(P1.X + (P2.X - P1.X) div 2, P1.Y + (P2.Y - P1.Y) div 2)
218end;
219
220function NormalizeAngle(const Angle: Double): Double;
221begin
222 if Angle < 0 then Result := Angle + (Trunc(Angle / (2 * Pi)) + 1) * (2 * Pi)
223 else if Angle > 2 * Pi then Result := Angle - Trunc(Angle / (2 * Pi)) * (2 * Pi)
224 else Result := Angle;
225end;
226
227function SubAngle(A1, A2: Double): Double;
228begin
229 A1 := NormalizeAngle(A1);
230 A2 := NormalizeAngle(A2);
231 if A1 < A2 then Result := A1 + 2 * Pi - A2
232 else Result := A1 - A2;
233end;
234
235{ TPolygon }
236
237function TPolygon.IsPointInside(const P: TPoint): Boolean;
238var
239 I, J: Integer;
240begin
241 Result := False;
242 J := High(Points);
243 for I := Low(Points) to High(Points) do begin
244 if ((Points[I].Y <= P.Y) and (P.Y < Points[J].Y)) or
245 ((Points[J].Y <= P.Y) and (P.Y < Points[I].Y)) then
246 begin
247 if (P.X < (Points[J].X - Points[I].X) *
248 (P.Y - Points[I].Y) /
249 (Points[J].Y - Points[I].Y) + Points[I].X) then
250 Result := not Result;
251 end;
252 J := I;
253 end;
254end;
255
256function PtInPoly(const Points: array of TPoint; Pos: TPoint): Boolean;
257var
258 Count, K, J : Integer;
259begin
260 Result := False;
261 Count := Length(Points) ;
262 J := Count - 1;
263 for K := 0 to Count - 1 do begin
264 if ((Points[K].Y <= Pos.Y) and (Pos.Y < Points[J].Y)) or
265 ((Points[J].Y <= Pos.Y) and (Pos.Y < Points[K].Y)) then
266 begin
267 if (Pos.X < (Points[j].X - Points[K].X) *
268 (Pos.Y - Points[K].Y) /
269 (Points[j].Y - Points[K].Y) + Points[K].X) then
270 Result := not Result;
271 end;
272 J := K;
273 end;
274end;
275
276
277function TPolygon.Create(const Points: TPointArray): TPolygon;
278var
279 I: Integer;
280begin
281 SetLength(Result.Points, Length(Points));
282 for I := 0 to Length(Points) - 1 do
283 Result.Points[I] := Points[I];
284end;
285
286function TPolygon.Create(const Rect: TRect): TPolygon;
287begin
288 SetLength(Result.Points, 4);
289 Result.Points[0] := Point(Rect.Left, Rect.Top);
290 Result.Points[1] := Point(Rect.Right, Rect.Top);
291 Result.Points[2] := Point(Rect.Right, Rect.Bottom);
292 Result.Points[3] := Point(Rect.Left, Rect.Bottom);
293end;
294
295procedure TPolygon.AddPoint(const P: TPoint);
296begin
297 SetLength(Points, Length(Points) + 1);
298 Points[Length(Points) - 1] := P;
299end;
300
301procedure TPolygon.Clear;
302begin
303 SetLength(Points, 0);
304end;
305
306procedure TPolygon.CutLine(const Vector: TLine; const PointInside: TPoint);
307var
308 I: Integer;
309 PointsChecked: Integer;
310 L1, L2: TLine;
311 Intersection: TPoint;
312 NewPoly: TPolygon;
313 NewPolygonStarted: Boolean;
314 Success: Boolean;
315begin
316 NewPoly.Clear;
317 Success := False;
318 NewPolygonStarted := False;
319 I := 0;
320 PointsChecked := 0;
321 L1 := Vector;
322 L1.Rotate(Pi / 2);
323 if Length(Points) > 0 then
324 while True do begin
325 L2 := TLine.Create(Points[I], Points[(I + 1) mod Length(Points)]);
326 if LineIntersect(L1, L2, Intersection) then
327 if PointInRect(Intersection, L2.ToRect) then begin
328 if not NewPolygonStarted then begin
329 // Crossing line, start new polygon
330 NewPoly.Clear;
331 NewPoly.AddPoint(Intersection);
332 NewPolygonStarted := True;
333 end else begin
334 // Crossing line, end polygon. If point NewPolygonStarted, the use polygon as result
335 NewPoly.AddPoint(Points[I]);
336 NewPoly.AddPoint(Intersection);
337 if NewPoly.IsPointInside(PointInside) then begin
338 Success := True;
339 Break;
340 end else begin
341 NewPoly.Clear;
342 NewPoly.AddPoint(Intersection);
343 NewPolygonStarted := True;
344 end;
345 end;
346 end else
347 NewPoly.AddPoint(Points[I]);
348 I := (I + 1) mod Length(Points);
349 Inc(PointsChecked);
350 if PointsChecked > 2 * Length(Points) then Break;
351 end;
352 if Success then Points := NewPoly.Points;
353end;
354
355{ TLine }
356
357function TLine.GetDistance: Double;
358begin
359 Result := Sqrt(Sqr(P2.X - P1.X) + Sqr(P2.Y - P1.Y));
360end;
361
362procedure TLine.SetDistance(AValue: Double);
363var
364 Angle: Double;
365begin
366 Angle := GetAngle;
367 P2 := Point(Round(P1.X + Cos(Angle) * AValue),
368 Round(P1.Y + Sin(Angle) * AValue));
369end;
370
371function TLine.Create(const P1, P2: TPoint): TLine;
372begin
373 Result.P1 := P1;
374 Result.P2 := P2;
375end;
376
377function TLine.GetMiddle: TPoint;
378begin
379 Result := Point(P1.X + (P2.X - P1.X) div 2, P1.Y + (P2.Y - P1.Y) div 2);
380end;
381
382function TLine.GetAngle: Double;
383begin
384 Result := ArcTan2(P2.Y - P1.Y, P2.X - P1.X);
385end;
386
387function TLine.GetSize: TPoint;
388begin
389 Result := Point(P2.X - P1.X, P2.Y - P1.Y);
390end;
391
392function TLine.ToRect: TRect;
393begin
394 if P1.X < P2.X then Result.Left := P1.X else Result.Left := P2.X;
395 if P1.Y < P2.Y then Result.Top := P1.Y else Result.Top := P2.Y;
396 if P1.X > P2.X then Result.Right := P1.X else Result.Right := P2.X;
397 if P1.Y > P2.Y then Result.Bottom := P1.Y else Result.Bottom := P2.Y;
398end;
399
400function TLine.DotProduct: Double;
401begin
402 Result := P1.X * P2.X + P1.Y * P2.Y;
403end;
404
405procedure TLine.Rotate(const Angle: Double);
406begin
407 P2 := RotatePoint(P1, P2, Angle);
408end;
409
410class operator TLine.Equal(const A, B: TLine): Boolean;
411begin
412 Result := ComparePoint(A.P1, B.P1) and ComparePoint(A.P2, B.P2);
413end;
414
415end.
416
Note: See TracBrowser for help on using the repository browser.