1 | unit GeometryClasses;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, Math;
|
---|
7 |
|
---|
8 | type
|
---|
9 | TPointArray = array of TPoint;
|
---|
10 |
|
---|
11 | function Distance(const P1, P2: TPoint): Integer;
|
---|
12 | function Dot(const P1, P2: TPoint): Double;
|
---|
13 | function AddPoint(const P1, P2: TPoint): TPoint;
|
---|
14 | function SubPoint(const P1, P2: TPoint): TPoint;
|
---|
15 | function PointToLineDistance(const P, V, W: TPoint): Integer;
|
---|
16 | function ComparePoint(const P1, P2: TPoint): Boolean;
|
---|
17 | function RotatePoint(const Center, P: TPoint; Angle: Double): TPoint;
|
---|
18 | function RotatePoints(const Center: TPoint; P: TPointArray; Angle: Double): TPointArray;
|
---|
19 | function ArcTan2Point(const Point: TPoint): Double;
|
---|
20 | function ArcTanPoint(const Point: TPoint): Double;
|
---|
21 | function RectEquals(const A, B: Classes.TRect): Boolean;
|
---|
22 | function RectEnlarge(const Rect: Classes.TRect; Value: Integer): Classes.TRect;
|
---|
23 | function ShiftRect(const ARect: TRect; Delta: TPoint): TRect;
|
---|
24 | function PointsToRect(const P1, P2: TPoint): TRect;
|
---|
25 | function PointInRect(const P: TPoint; aRect: TRect): Boolean;
|
---|
26 | function PtInPoly(const Points: array of TPoint; Pos: TPoint): Boolean;
|
---|
27 | function HalfDistancePoint(const P1, P2: TPoint): TPoint;
|
---|
28 | function NormalizeAngle(const Angle: Double): Double;
|
---|
29 | function SubAngle(A1, A2: Double): Double;
|
---|
30 |
|
---|
31 |
|
---|
32 | implementation
|
---|
33 |
|
---|
34 | function Distance(const P1, P2: TPoint): Integer;
|
---|
35 | begin
|
---|
36 | Result := Trunc(Sqrt(Sqr(P2.X - P1.X) + Sqr(P2.Y - P1.Y)));
|
---|
37 | end;
|
---|
38 |
|
---|
39 | function Dot(const P1, P2: TPoint): Double;
|
---|
40 | begin
|
---|
41 | Result := P1.X * P2.X + P1.Y * P2.Y;
|
---|
42 | end;
|
---|
43 |
|
---|
44 | function AddPoint(const P1, P2: TPoint): TPoint;
|
---|
45 | begin
|
---|
46 | Result.X := P1.X + P2.X;
|
---|
47 | Result.Y := P1.Y + P2.Y;
|
---|
48 | end;
|
---|
49 |
|
---|
50 | function SubPoint(const P1, P2: TPoint): TPoint;
|
---|
51 | begin
|
---|
52 | Result.X := P1.X - P2.X;
|
---|
53 | Result.Y := P1.Y - P2.Y;
|
---|
54 | end;
|
---|
55 |
|
---|
56 | function PointToLineDistance(const P, V, W: TPoint): Integer;
|
---|
57 | var
|
---|
58 | l2, t: Double;
|
---|
59 | tt: TPoint;
|
---|
60 | begin
|
---|
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);
|
---|
83 | end;
|
---|
84 |
|
---|
85 | function ComparePoint(const P1, P2: TPoint): Boolean;
|
---|
86 | begin
|
---|
87 | Result := (P1.X = P2.X) and (P1.Y = P2.Y);
|
---|
88 | end;
|
---|
89 |
|
---|
90 | function RotatePoint(const Center, P: TPoint; Angle: Double): TPoint;
|
---|
91 | var
|
---|
92 | D: TPoint;
|
---|
93 | begin
|
---|
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)));
|
---|
97 | end;
|
---|
98 |
|
---|
99 | function RotatePoints(const Center: TPoint; P: TPointArray; Angle: Double): TPointArray;
|
---|
100 | var
|
---|
101 | I: Integer;
|
---|
102 | begin
|
---|
103 | SetLength(Result, Length(P));
|
---|
104 | for I := 0 to High(P) do
|
---|
105 | Result[I] := RotatePoint(Center, P[I], Angle);
|
---|
106 | end;
|
---|
107 |
|
---|
108 | function ArcTan2Point(const Point: TPoint): Double;
|
---|
109 | begin
|
---|
110 | Result := ArcTan2(Point.Y, Point.X);
|
---|
111 | end;
|
---|
112 |
|
---|
113 | function ArcTanPoint(const Point: TPoint): Double;
|
---|
114 | begin
|
---|
115 | if Point.Y = 0 then Result := Infinity
|
---|
116 | else Result := ArcTan(Point.X / Point.Y);
|
---|
117 | end;
|
---|
118 |
|
---|
119 | function RectEquals(const A, B: Classes.TRect): Boolean;
|
---|
120 | begin
|
---|
121 | Result := (A.Left = B.Left) and (A.Top = B.Top) and
|
---|
122 | (A.Right = B.Right) and (A.Bottom = B.Bottom);
|
---|
123 | end;
|
---|
124 |
|
---|
125 | function RectEnlarge(const Rect: Classes.TRect; Value: Integer): Classes.TRect;
|
---|
126 | begin
|
---|
127 | Result.Left := Rect.Left - Value;
|
---|
128 | Result.Right := Rect.Right + Value;
|
---|
129 | Result.Top := Rect.Top - Value;
|
---|
130 | Result.Bottom := Rect.Bottom + Value;
|
---|
131 | end;
|
---|
132 |
|
---|
133 | function ShiftRect(const ARect: TRect; Delta: TPoint): TRect;
|
---|
134 | begin
|
---|
135 | Result := Rect(ARect.Left + Delta.X, ARect.Top + Delta.Y,
|
---|
136 | ARect.Right + Delta.X, ARect.Bottom + Delta.Y);
|
---|
137 | end;
|
---|
138 |
|
---|
139 | function PointsToRect(const P1, P2: TPoint): TRect;
|
---|
140 | begin
|
---|
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;
|
---|
145 | end;
|
---|
146 |
|
---|
147 | function PointInRect(const P: TPoint; aRect: TRect): Boolean;
|
---|
148 | begin
|
---|
149 | Result := (P.X >= aRect.Left) and (P.X <= aRect.Right) and
|
---|
150 | (P.Y >= aRect.Top) and (P.Y <= aRect.Bottom);
|
---|
151 | end;
|
---|
152 |
|
---|
153 | function HalfDistancePoint(const P1, P2: TPoint): TPoint;
|
---|
154 | begin
|
---|
155 | Result := Point(P1.X + (P2.X - P1.X) div 2, P1.Y + (P2.Y - P1.Y) div 2)
|
---|
156 | end;
|
---|
157 |
|
---|
158 | function NormalizeAngle(const Angle: Double): Double;
|
---|
159 | begin
|
---|
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;
|
---|
163 | end;
|
---|
164 |
|
---|
165 | function SubAngle(A1, A2: Double): Double;
|
---|
166 | begin
|
---|
167 | A1 := NormalizeAngle(A1);
|
---|
168 | A2 := NormalizeAngle(A2);
|
---|
169 | if A1 < A2 then Result := A1 + 2 * Pi - A2
|
---|
170 | else Result := A1 - A2;
|
---|
171 | end;
|
---|
172 |
|
---|
173 | function PtInPoly(const Points: array of TPoint; Pos: TPoint): Boolean;
|
---|
174 | var
|
---|
175 | Count, K, J : Integer;
|
---|
176 | begin
|
---|
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;
|
---|
191 | end;
|
---|
192 |
|
---|
193 | end.
|
---|
194 |
|
---|