1 | unit Geometric;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, Math;
|
---|
7 |
|
---|
8 | type
|
---|
9 | TPointArray = array of TPoint;
|
---|
10 |
|
---|
11 | { TVector }
|
---|
12 |
|
---|
13 | TVector = record
|
---|
14 | Position: TPoint;
|
---|
15 | Direction: TPoint;
|
---|
16 | function GetLength: Double;
|
---|
17 | function GetAngle: Double;
|
---|
18 | procedure SetLength(Value: Double);
|
---|
19 | class function Create(P1, P2: TPoint): TVector; static;
|
---|
20 | end;
|
---|
21 |
|
---|
22 | function Distance(P1, P2: TPoint): Integer;
|
---|
23 | function Dot(const P1, P2: TPoint): Double;
|
---|
24 | function AddPoint(const P1, P2: TPoint): TPoint;
|
---|
25 | function SubPoint(const P1, P2: TPoint): TPoint;
|
---|
26 | function PointToLineDistance(const P, V, W: TPoint; out Intersect: TPoint): Integer;
|
---|
27 | function ComparePoint(P1, P2: TPoint): Boolean;
|
---|
28 | function RotatePoint(Center, P: TPoint; Angle: Double): TPoint;
|
---|
29 | function RotatePoints(Center: TPoint; P: TPointArray; Angle: Double): TPointArray;
|
---|
30 | function LineIntersect(LineAP1, LineAP2, LineBP1, LineBP2: TPoint;
|
---|
31 | out Intersection: TPoint): Boolean;
|
---|
32 | function ArcTan2Point(Point: TPoint): Float;
|
---|
33 | function ArcTanPoint(Point: TPoint): Float;
|
---|
34 | function RectEquals(A, B: TRect): Boolean;
|
---|
35 | function RectEnlarge(Rect: TRect; Value: Integer): TRect;
|
---|
36 | function ShiftRect(ARect: TRect; Delta: TPoint): TRect;
|
---|
37 |
|
---|
38 |
|
---|
39 | implementation
|
---|
40 |
|
---|
41 | function Distance(P1, P2: TPoint): Integer;
|
---|
42 | begin
|
---|
43 | Result := Trunc(Sqrt(Sqr(P2.X - P1.X) + Sqr(P2.Y - P1.Y)));
|
---|
44 | end;
|
---|
45 |
|
---|
46 | function Dot(const P1, P2: TPoint): Double;
|
---|
47 | begin
|
---|
48 | Result := P1.X * P2.X + P1.Y * P2.Y;
|
---|
49 | end;
|
---|
50 |
|
---|
51 | function AddPoint(const P1, P2: TPoint): TPoint;
|
---|
52 | begin
|
---|
53 | Result.X := P1.X + P2.X;
|
---|
54 | Result.Y := P1.Y + P2.Y;
|
---|
55 | end;
|
---|
56 |
|
---|
57 | function SubPoint(const P1, P2: TPoint): TPoint;
|
---|
58 | begin
|
---|
59 | Result.X := P1.X - P2.X;
|
---|
60 | Result.Y := P1.Y - P2.Y;
|
---|
61 | end;
|
---|
62 |
|
---|
63 | function PointToLineDistance(const P, V, W: TPoint; out Intersect: TPoint): Integer;
|
---|
64 | var
|
---|
65 | l2, t: Double;
|
---|
66 | tt: TPoint;
|
---|
67 | begin
|
---|
68 | // Return minimum distance between line segment vw and point p
|
---|
69 | L2 := Distance(V, W); // i.e. |w-v|^2 - avoid a sqrt
|
---|
70 | L2 := Power(l2, 2);
|
---|
71 | if L2 = 0 then begin
|
---|
72 | Result := Distance(P, V); // v == w case
|
---|
73 | Exit;
|
---|
74 | end;
|
---|
75 | // Consider the line extending the segment, parameterized as v + t (w - v).
|
---|
76 | // We find projection of point p onto the line.
|
---|
77 | // It falls where t = [(p-v) . (w-v)] / |w-v|^2
|
---|
78 | T := Dot(SubPoint(P, V), SubPoint(W, V)) / L2;
|
---|
79 | if T < 0 then begin
|
---|
80 | Result := Distance(P, V); // Beyond the 'v' end of the segment
|
---|
81 | Intersect := V;
|
---|
82 | Exit;
|
---|
83 | end
|
---|
84 | else if T > 1 then begin
|
---|
85 | Result := Distance(P, W); // Beyond the 'w' end of the segment
|
---|
86 | Intersect := W;
|
---|
87 | Exit;
|
---|
88 | end;
|
---|
89 | TT.X := Trunc(V.X + T * (W.X - V.X));
|
---|
90 | TT.Y := Trunc(V.Y + T * (W.Y - V.Y));
|
---|
91 | Result := Distance(P, TT);
|
---|
92 | Intersect := TT;
|
---|
93 | end;
|
---|
94 |
|
---|
95 | function ComparePoint(P1, P2: TPoint): Boolean;
|
---|
96 | begin
|
---|
97 | Result := (P1.X = P2.X) and (P1.Y = P2.Y);
|
---|
98 | end;
|
---|
99 |
|
---|
100 | function RotatePoint(Center, P: TPoint; Angle: Double): TPoint;
|
---|
101 | begin
|
---|
102 | P := Point(P.X - Center.X, P.Y - Center.Y);
|
---|
103 | Result := Point(Center.X + Round(P.X * Cos(Angle) - P.Y * Sin(Angle)),
|
---|
104 | Center.Y + Round(P.X * Sin(Angle) + P.Y * Cos(Angle)));
|
---|
105 | end;
|
---|
106 |
|
---|
107 | function RotatePoints(Center: TPoint; P: TPointArray; Angle: Double): TPointArray;
|
---|
108 | var
|
---|
109 | I: Integer;
|
---|
110 | begin
|
---|
111 | Result := Default(TPointArray);
|
---|
112 | SetLength(Result, Length(P));
|
---|
113 | for I := 0 to High(P) do
|
---|
114 | Result[I] := RotatePoint(Center, P[I], Angle);
|
---|
115 | end;
|
---|
116 |
|
---|
117 | function LineIntersect(LineAP1, LineAP2, LineBP1, LineBP2: TPoint;
|
---|
118 | out Intersection: TPoint): Boolean;
|
---|
119 | Var
|
---|
120 | LDetLineA, LDetLineB, LDetDivInv: Double;
|
---|
121 | LDiffLA, LDiffLB: TPoint;
|
---|
122 | D: Double;
|
---|
123 | begin
|
---|
124 | if (LineAP1 = LineAP2) or (LineBP1 = LineBP2) then begin
|
---|
125 | Result := False;
|
---|
126 | Exit;
|
---|
127 | end;
|
---|
128 | LDetLineA := LineAP1.X * LineAP2.Y - LineAP1.Y * LineAP2.X;
|
---|
129 | LDetLineB := LineBP1.X * LineBP2.Y - LineBP1.Y * LineBP2.X;
|
---|
130 |
|
---|
131 | LDiffLA := SubPoint(LineAP1, LineAP2);
|
---|
132 | LDiffLB := SubPoint(LineBP1, LineBP2);
|
---|
133 |
|
---|
134 | D := ((LDiffLA.X * LDiffLB.Y) - (LDiffLA.Y * LDiffLB.X));
|
---|
135 | if D = 0 then begin
|
---|
136 | // Parallel lines without intersection
|
---|
137 | Result := False;
|
---|
138 | Exit;
|
---|
139 | end;
|
---|
140 | LDetDivInv := 1 / D;
|
---|
141 |
|
---|
142 | Intersection.X := Trunc(((LDetLineA * LDiffLB.X) - (LDiffLA.X * LDetLineB)) * LDetDivInv);
|
---|
143 | Intersection.Y := Trunc(((LDetLineA * LDiffLB.Y) - (LDiffLA.Y * LDetLineB)) * LDetDivInv);
|
---|
144 | Result := True;
|
---|
145 | end;
|
---|
146 |
|
---|
147 | function ArcTan2Point(Point: TPoint): Float;
|
---|
148 | begin
|
---|
149 | Result := ArcTan2(Point.Y, Point.X);
|
---|
150 | end;
|
---|
151 |
|
---|
152 | function ArcTanPoint(Point: TPoint): Float;
|
---|
153 | begin
|
---|
154 | if Point.Y = 0 then Result := Infinity
|
---|
155 | else Result := ArcTan(Point.X / Point.Y);
|
---|
156 | end;
|
---|
157 |
|
---|
158 | function RectEquals(A, B: TRect): Boolean;
|
---|
159 | begin
|
---|
160 | Result := (A.Left = B.Left) and (A.Top = B.Top) and
|
---|
161 | (A.Right = B.Right) and (A.Bottom = B.Bottom);
|
---|
162 | end;
|
---|
163 |
|
---|
164 | function RectEnlarge(Rect: TRect; Value: Integer): TRect;
|
---|
165 | begin
|
---|
166 | Result.Left := Rect.Left - Value;
|
---|
167 | Result.Right := Rect.Right + Value;
|
---|
168 | Result.Top := Rect.Top - Value;
|
---|
169 | Result.Bottom := Rect.Bottom + Value;
|
---|
170 | end;
|
---|
171 |
|
---|
172 | function ShiftRect(ARect: TRect; Delta: TPoint): TRect;
|
---|
173 | begin
|
---|
174 | Result := Rect(ARect.Left + Delta.X, ARect.Top + Delta.Y,
|
---|
175 | ARect.Right + Delta.X, ARect.Bottom + Delta.Y);
|
---|
176 | end;
|
---|
177 |
|
---|
178 | { TVector }
|
---|
179 |
|
---|
180 | function TVector.GetLength: Double;
|
---|
181 | begin
|
---|
182 | Result := Sqrt(Sqr(Direction.X) + Sqr(Direction.Y));
|
---|
183 | end;
|
---|
184 |
|
---|
185 | function TVector.GetAngle: Double;
|
---|
186 | begin
|
---|
187 | Result := ArcTan2(Direction.Y, Direction.X);
|
---|
188 | end;
|
---|
189 |
|
---|
190 | procedure TVector.SetLength(Value: Double);
|
---|
191 | var
|
---|
192 | Angle: Double;
|
---|
193 | begin
|
---|
194 | Angle := GetAngle;
|
---|
195 | Direction := Point(Round(Cos(Angle) * Value),
|
---|
196 | Round(Sin(Angle) * Value));
|
---|
197 | end;
|
---|
198 |
|
---|
199 | class function TVector.Create(P1, P2: TPoint): TVector;
|
---|
200 | begin
|
---|
201 | Result.Position := P1;
|
---|
202 | Result.Direction := Point(P2.X - P1.X, P2.Y - P1.Y);
|
---|
203 | end;
|
---|
204 |
|
---|
205 | end.
|
---|