source: trunk/GeometryClasses.pas

Last change on this file was 317, checked in by chronos, 6 months ago
  • Modified: Remove U prefix from unit names.
  • Modified: Use TFormEx for all forms for code simplification.
File size: 5.8 KB
Line 
1unit GeometryClasses;
2
3interface
4
5uses
6 Classes, SysUtils, Math;
7
8type
9 TPointArray = array of TPoint;
10
11function Distance(const P1, P2: TPoint): Integer;
12function Dot(const P1, P2: TPoint): Double;
13function AddPoint(const P1, P2: TPoint): TPoint;
14function SubPoint(const P1, P2: TPoint): TPoint;
15function PointToLineDistance(const P, V, W: TPoint): Integer;
16function ComparePoint(const P1, P2: TPoint): Boolean;
17function RotatePoint(const Center, P: TPoint; Angle: Double): TPoint;
18function RotatePoints(const Center: TPoint; P: TPointArray; Angle: Double): TPointArray;
19function ArcTan2Point(const Point: TPoint): Double;
20function ArcTanPoint(const Point: TPoint): Double;
21function RectEquals(const A, B: Classes.TRect): Boolean;
22function RectEnlarge(const Rect: Classes.TRect; Value: Integer): Classes.TRect;
23function ShiftRect(const ARect: TRect; Delta: TPoint): TRect;
24function PointsToRect(const P1, P2: TPoint): TRect;
25function PointInRect(const P: TPoint; aRect: TRect): Boolean;
26function PtInPoly(const Points: array of TPoint; Pos: TPoint): Boolean;
27function HalfDistancePoint(const P1, P2: TPoint): TPoint;
28function NormalizeAngle(const Angle: Double): Double;
29function SubAngle(A1, A2: Double): Double;
30
31
32implementation
33
34function Distance(const P1, P2: TPoint): Integer;
35begin
36 Result := Trunc(Sqrt(Sqr(P2.X - P1.X) + Sqr(P2.Y - P1.Y)));
37end;
38
39function Dot(const P1, P2: TPoint): Double;
40begin
41 Result := P1.X * P2.X + P1.Y * P2.Y;
42end;
43
44function AddPoint(const P1, P2: TPoint): TPoint;
45begin
46 Result.X := P1.X + P2.X;
47 Result.Y := P1.Y + P2.Y;
48end;
49
50function SubPoint(const P1, P2: TPoint): TPoint;
51begin
52 Result.X := P1.X - P2.X;
53 Result.Y := P1.Y - P2.Y;
54end;
55
56function PointToLineDistance(const P, V, W: TPoint): Integer;
57var
58 l2, t: Double;
59 tt: TPoint;
60begin
61 // Return minimum distance between line segment vw and point p
62 L2 := Distance(V, W); // i.e. |w-v|^2 - avoid a sqrt
63 L2 := Power(l2, 2);
64 if L2 = 0 then begin
65 Result := Distance(P, V); // v == w case
66 Exit;
67 end;
68 // Consider the line extending the segment, parameterized as v + t (w - v).
69 // We find projection of point p onto the line.
70 // It falls where t = [(p-v) . (w-v)] / |w-v|^2
71 T := Dot(SubPoint(P, V), SubPoint(W, V)) / L2;
72 if T < 0 then begin
73 Result := Distance(P, V); // Beyond the 'v' end of the segment
74 exit;
75 end
76 else if T > 1 then begin
77 Result := Distance(P, W); // Beyond the 'w' end of the segment
78 Exit;
79 end;
80 TT.X := Trunc(V.X + T * (W.X - V.X));
81 TT.Y := Trunc(V.Y + T * (W.Y - V.Y));
82 Result := Distance(P, TT);
83end;
84
85function ComparePoint(const P1, P2: TPoint): Boolean;
86begin
87 Result := (P1.X = P2.X) and (P1.Y = P2.Y);
88end;
89
90function RotatePoint(const Center, P: TPoint; Angle: Double): TPoint;
91var
92 D: TPoint;
93begin
94 D := Point(P.X - Center.X, P.Y - Center.Y);
95 Result := Point(Center.X + Round(D.X * Cos(Angle) - D.Y * Sin(Angle)),
96 Center.Y + Round(D.X * Sin(Angle) + D.Y * Cos(Angle)));
97end;
98
99function RotatePoints(const Center: TPoint; P: TPointArray; Angle: Double): TPointArray;
100var
101 I: Integer;
102begin
103 SetLength(Result, Length(P));
104 for I := 0 to High(P) do
105 Result[I] := RotatePoint(Center, P[I], Angle);
106end;
107
108function ArcTan2Point(const Point: TPoint): Double;
109begin
110 Result := ArcTan2(Point.Y, Point.X);
111end;
112
113function ArcTanPoint(const Point: TPoint): Double;
114begin
115 if Point.Y = 0 then Result := Infinity
116 else Result := ArcTan(Point.X / Point.Y);
117end;
118
119function RectEquals(const A, B: Classes.TRect): Boolean;
120begin
121 Result := (A.Left = B.Left) and (A.Top = B.Top) and
122 (A.Right = B.Right) and (A.Bottom = B.Bottom);
123end;
124
125function RectEnlarge(const Rect: Classes.TRect; Value: Integer): Classes.TRect;
126begin
127 Result.Left := Rect.Left - Value;
128 Result.Right := Rect.Right + Value;
129 Result.Top := Rect.Top - Value;
130 Result.Bottom := Rect.Bottom + Value;
131end;
132
133function ShiftRect(const ARect: TRect; Delta: TPoint): TRect;
134begin
135 Result := Rect(ARect.Left + Delta.X, ARect.Top + Delta.Y,
136 ARect.Right + Delta.X, ARect.Bottom + Delta.Y);
137end;
138
139function PointsToRect(const P1, P2: TPoint): TRect;
140begin
141 if P1.X < P2.X then Result.Left := P1.X else Result.Left := P2.X;
142 if P1.Y < P2.Y then Result.Top := P1.Y else Result.Top := P2.Y;
143 if P1.X > P2.X then Result.Right := P1.X else Result.Right := P2.X;
144 if P1.Y > P2.Y then Result.Bottom := P1.Y else Result.Bottom := P2.Y;
145end;
146
147function PointInRect(const P: TPoint; aRect: TRect): Boolean;
148begin
149 Result := (P.X >= aRect.Left) and (P.X <= aRect.Right) and
150 (P.Y >= aRect.Top) and (P.Y <= aRect.Bottom);
151end;
152
153function HalfDistancePoint(const P1, P2: TPoint): TPoint;
154begin
155 Result := Point(P1.X + (P2.X - P1.X) div 2, P1.Y + (P2.Y - P1.Y) div 2)
156end;
157
158function NormalizeAngle(const Angle: Double): Double;
159begin
160 if Angle < 0 then Result := Angle + (Trunc(Angle / (2 * Pi)) + 1) * (2 * Pi)
161 else if Angle > 2 * Pi then Result := Angle - Trunc(Angle / (2 * Pi)) * (2 * Pi)
162 else Result := Angle;
163end;
164
165function SubAngle(A1, A2: Double): Double;
166begin
167 A1 := NormalizeAngle(A1);
168 A2 := NormalizeAngle(A2);
169 if A1 < A2 then Result := A1 + 2 * Pi - A2
170 else Result := A1 - A2;
171end;
172
173function PtInPoly(const Points: array of TPoint; Pos: TPoint): Boolean;
174var
175 Count, K, J : Integer;
176begin
177 Result := False;
178 Count := Length(Points) ;
179 J := Count - 1;
180 for K := 0 to Count - 1 do begin
181 if ((Points[K].Y <= Pos.Y) and (Pos.Y < Points[J].Y)) or
182 ((Points[J].Y <= Pos.Y) and (Pos.Y < Points[K].Y)) then
183 begin
184 if (Pos.X < (Points[j].X - Points[K].X) *
185 (Pos.Y - Points[K].Y) /
186 (Points[j].Y - Points[K].Y) + Points[K].X) then
187 Result := not Result;
188 end;
189 J := K;
190 end;
191end;
192
193end.
194
Note: See TracBrowser for help on using the repository browser.