1 | unit UGeometry;
|
---|
2 |
|
---|
3 | {$mode delphi}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, Math;
|
---|
9 |
|
---|
10 | type
|
---|
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 |
|
---|
45 | function Distance(const P1, P2: TPoint): Integer;
|
---|
46 | function Dot(const P1, P2: TPoint): Double;
|
---|
47 | function AddPoint(const P1, P2: TPoint): TPoint;
|
---|
48 | function SubPoint(const P1, P2: TPoint): TPoint;
|
---|
49 | function PointToLineDistance(const P, V, W: TPoint): Integer;
|
---|
50 | function ComparePoint(const P1, P2: TPoint): Boolean;
|
---|
51 | function RotatePoint(const Center, P: TPoint; Angle: Double): TPoint;
|
---|
52 | function RotatePoints(const Center: TPoint; P: TPointArray; Angle: Double): TPointArray;
|
---|
53 | function LineIntersect(const LineA, LineB: TLine; out Intersection: TPoint): Boolean;
|
---|
54 | function ArcTan2Point(const Point: TPoint): Float;
|
---|
55 | function ArcTanPoint(const Point: TPoint): Float;
|
---|
56 | function RectEquals(const A, B: TRect): Boolean;
|
---|
57 | function RectEnlarge(const Rect: TRect; Value: Integer): TRect;
|
---|
58 | function ShiftRect(const ARect: TRect; Delta: TPoint): TRect;
|
---|
59 | function PointsToRect(const P1, P2: TPoint): TRect;
|
---|
60 | function PointInRect(const P: TPoint; aRect: TRect): Boolean;
|
---|
61 | function HalfDistancePoint(const P1, P2: TPoint): TPoint;
|
---|
62 | function NormalizeAngle(const Angle: Double): Double;
|
---|
63 | function SubAngle(A1, A2: Double): Double;
|
---|
64 |
|
---|
65 | implementation
|
---|
66 |
|
---|
67 | function Distance(const P1, P2: TPoint): Integer;
|
---|
68 | begin
|
---|
69 | Result := Trunc(Sqrt(Sqr(P2.X - P1.X) + Sqr(P2.Y - P1.Y)));
|
---|
70 | end;
|
---|
71 |
|
---|
72 | function Dot(const P1, P2: TPoint): Double;
|
---|
73 | begin
|
---|
74 | Result := P1.X * P2.X + P1.Y * P2.Y;
|
---|
75 | end;
|
---|
76 |
|
---|
77 | function AddPoint(const P1, P2: TPoint): TPoint;
|
---|
78 | begin
|
---|
79 | Result.X := P1.X + P2.X;
|
---|
80 | Result.Y := P1.Y + P2.Y;
|
---|
81 | end;
|
---|
82 |
|
---|
83 | function SubPoint(const P1, P2: TPoint): TPoint;
|
---|
84 | begin
|
---|
85 | Result.X := P1.X - P2.X;
|
---|
86 | Result.Y := P1.Y - P2.Y;
|
---|
87 | end;
|
---|
88 |
|
---|
89 | function PointToLineDistance(const P, V, W: TPoint): Integer;
|
---|
90 | var
|
---|
91 | l2, t: Double;
|
---|
92 | tt: TPoint;
|
---|
93 | begin
|
---|
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);
|
---|
116 | end;
|
---|
117 |
|
---|
118 | function ComparePoint(const P1, P2: TPoint): Boolean;
|
---|
119 | begin
|
---|
120 | Result := (P1.X = P2.X) and (P1.Y = P2.Y);
|
---|
121 | end;
|
---|
122 |
|
---|
123 | function RotatePoint(const Center, P: TPoint; Angle: Double): TPoint;
|
---|
124 | var
|
---|
125 | D: TPoint;
|
---|
126 | begin
|
---|
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)));
|
---|
130 | end;
|
---|
131 |
|
---|
132 | function RotatePoints(const Center: TPoint; P: TPointArray; Angle: Double): TPointArray;
|
---|
133 | var
|
---|
134 | I: Integer;
|
---|
135 | begin
|
---|
136 | SetLength(Result, Length(P));
|
---|
137 | for I := 0 to High(P) do
|
---|
138 | Result[I] := RotatePoint(Center, P[I], Angle);
|
---|
139 | end;
|
---|
140 |
|
---|
141 | function LineIntersect(const LineA, LineB: TLine; out Intersection: TPoint): Boolean;
|
---|
142 | Var
|
---|
143 | LDetLineA, LDetLineB, LDetDivInv: Double;
|
---|
144 | LDiffLA, LDiffLB: TPoint;
|
---|
145 | D: Integer;
|
---|
146 | begin
|
---|
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;
|
---|
168 | end;
|
---|
169 |
|
---|
170 | function ArcTan2Point(const Point: TPoint): Float;
|
---|
171 | begin
|
---|
172 | Result := ArcTan2(Point.Y, Point.X);
|
---|
173 | end;
|
---|
174 |
|
---|
175 | function ArcTanPoint(const Point: TPoint): Float;
|
---|
176 | begin
|
---|
177 | if Point.Y = 0 then Result := Infinity
|
---|
178 | else Result := ArcTan(Point.X / Point.Y);
|
---|
179 | end;
|
---|
180 |
|
---|
181 | function RectEquals(const A, B: TRect): Boolean;
|
---|
182 | begin
|
---|
183 | Result := (A.Left = B.Left) and (A.Top = B.Top) and
|
---|
184 | (A.Right = B.Right) and (A.Bottom = B.Bottom);
|
---|
185 | end;
|
---|
186 |
|
---|
187 | function RectEnlarge(const Rect: TRect; Value: Integer): TRect;
|
---|
188 | begin
|
---|
189 | Result.Left := Rect.Left - Value;
|
---|
190 | Result.Right := Rect.Right + Value;
|
---|
191 | Result.Top := Rect.Top - Value;
|
---|
192 | Result.Bottom := Rect.Bottom + Value;
|
---|
193 | end;
|
---|
194 |
|
---|
195 | function ShiftRect(const ARect: TRect; Delta: TPoint): TRect;
|
---|
196 | begin
|
---|
197 | Result := Rect(ARect.Left + Delta.X, ARect.Top + Delta.Y,
|
---|
198 | ARect.Right + Delta.X, ARect.Bottom + Delta.Y);
|
---|
199 | end;
|
---|
200 |
|
---|
201 | function PointsToRect(const P1, P2: TPoint): TRect;
|
---|
202 | begin
|
---|
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;
|
---|
207 | end;
|
---|
208 |
|
---|
209 | function PointInRect(const P: TPoint; aRect: TRect): Boolean;
|
---|
210 | begin
|
---|
211 | Result := (P.X >= aRect.Left) and (P.X <= aRect.Right) and
|
---|
212 | (P.Y >= aRect.Top) and (P.Y <= aRect.Bottom);
|
---|
213 | end;
|
---|
214 |
|
---|
215 | function HalfDistancePoint(const P1, P2: TPoint): TPoint;
|
---|
216 | begin
|
---|
217 | Result := Point(P1.X + (P2.X - P1.X) div 2, P1.Y + (P2.Y - P1.Y) div 2)
|
---|
218 | end;
|
---|
219 |
|
---|
220 | function NormalizeAngle(const Angle: Double): Double;
|
---|
221 | begin
|
---|
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;
|
---|
225 | end;
|
---|
226 |
|
---|
227 | function SubAngle(A1, A2: Double): Double;
|
---|
228 | begin
|
---|
229 | A1 := NormalizeAngle(A1);
|
---|
230 | A2 := NormalizeAngle(A2);
|
---|
231 | if A1 < A2 then Result := A1 + 2 * Pi - A2
|
---|
232 | else Result := A1 - A2;
|
---|
233 | end;
|
---|
234 |
|
---|
235 | { TPolygon }
|
---|
236 |
|
---|
237 | function TPolygon.IsPointInside(const P: TPoint): Boolean;
|
---|
238 | var
|
---|
239 | I, J: Integer;
|
---|
240 | begin
|
---|
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;
|
---|
254 | end;
|
---|
255 |
|
---|
256 | function PtInPoly(const Points: array of TPoint; Pos: TPoint): Boolean;
|
---|
257 | var
|
---|
258 | Count, K, J : Integer;
|
---|
259 | begin
|
---|
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;
|
---|
274 | end;
|
---|
275 |
|
---|
276 |
|
---|
277 | function TPolygon.Create(const Points: TPointArray): TPolygon;
|
---|
278 | var
|
---|
279 | I: Integer;
|
---|
280 | begin
|
---|
281 | SetLength(Result.Points, Length(Points));
|
---|
282 | for I := 0 to Length(Points) - 1 do
|
---|
283 | Result.Points[I] := Points[I];
|
---|
284 | end;
|
---|
285 |
|
---|
286 | function TPolygon.Create(const Rect: TRect): TPolygon;
|
---|
287 | begin
|
---|
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);
|
---|
293 | end;
|
---|
294 |
|
---|
295 | procedure TPolygon.AddPoint(const P: TPoint);
|
---|
296 | begin
|
---|
297 | SetLength(Points, Length(Points) + 1);
|
---|
298 | Points[Length(Points) - 1] := P;
|
---|
299 | end;
|
---|
300 |
|
---|
301 | procedure TPolygon.Clear;
|
---|
302 | begin
|
---|
303 | SetLength(Points, 0);
|
---|
304 | end;
|
---|
305 |
|
---|
306 | procedure TPolygon.CutLine(const Vector: TLine; const PointInside: TPoint);
|
---|
307 | var
|
---|
308 | I: Integer;
|
---|
309 | PointsChecked: Integer;
|
---|
310 | L1, L2: TLine;
|
---|
311 | Intersection: TPoint;
|
---|
312 | NewPoly: TPolygon;
|
---|
313 | NewPolygonStarted: Boolean;
|
---|
314 | Success: Boolean;
|
---|
315 | begin
|
---|
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;
|
---|
353 | end;
|
---|
354 |
|
---|
355 | { TLine }
|
---|
356 |
|
---|
357 | function TLine.GetDistance: Double;
|
---|
358 | begin
|
---|
359 | Result := Sqrt(Sqr(P2.X - P1.X) + Sqr(P2.Y - P1.Y));
|
---|
360 | end;
|
---|
361 |
|
---|
362 | procedure TLine.SetDistance(AValue: Double);
|
---|
363 | var
|
---|
364 | Angle: Double;
|
---|
365 | begin
|
---|
366 | Angle := GetAngle;
|
---|
367 | P2 := Point(Round(P1.X + Cos(Angle) * AValue),
|
---|
368 | Round(P1.Y + Sin(Angle) * AValue));
|
---|
369 | end;
|
---|
370 |
|
---|
371 | function TLine.Create(const P1, P2: TPoint): TLine;
|
---|
372 | begin
|
---|
373 | Result.P1 := P1;
|
---|
374 | Result.P2 := P2;
|
---|
375 | end;
|
---|
376 |
|
---|
377 | function TLine.GetMiddle: TPoint;
|
---|
378 | begin
|
---|
379 | Result := Point(P1.X + (P2.X - P1.X) div 2, P1.Y + (P2.Y - P1.Y) div 2);
|
---|
380 | end;
|
---|
381 |
|
---|
382 | function TLine.GetAngle: Double;
|
---|
383 | begin
|
---|
384 | Result := ArcTan2(P2.Y - P1.Y, P2.X - P1.X);
|
---|
385 | end;
|
---|
386 |
|
---|
387 | function TLine.GetSize: TPoint;
|
---|
388 | begin
|
---|
389 | Result := Point(P2.X - P1.X, P2.Y - P1.Y);
|
---|
390 | end;
|
---|
391 |
|
---|
392 | function TLine.ToRect: TRect;
|
---|
393 | begin
|
---|
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;
|
---|
398 | end;
|
---|
399 |
|
---|
400 | function TLine.DotProduct: Double;
|
---|
401 | begin
|
---|
402 | Result := P1.X * P2.X + P1.Y * P2.Y;
|
---|
403 | end;
|
---|
404 |
|
---|
405 | procedure TLine.Rotate(const Angle: Double);
|
---|
406 | begin
|
---|
407 | P2 := RotatePoint(P1, P2, Angle);
|
---|
408 | end;
|
---|
409 |
|
---|
410 | class operator TLine.Equal(const A, B: TLine): Boolean;
|
---|
411 | begin
|
---|
412 | Result := ComparePoint(A.P1, B.P1) and ComparePoint(A.P2, B.P2);
|
---|
413 | end;
|
---|
414 |
|
---|
415 | end.
|
---|
416 |
|
---|