source: Common/Geometric.pas

Last change on this file was 563, checked in by chronos, 18 months ago
  • Modified: Removed U prefix from all Common package units.
File size: 5.3 KB
Line 
1unit Geometric;
2
3interface
4
5uses
6 Classes, SysUtils, Math;
7
8type
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
22function Distance(P1, P2: TPoint): Integer;
23function Dot(const P1, P2: TPoint): Double;
24function AddPoint(const P1, P2: TPoint): TPoint;
25function SubPoint(const P1, P2: TPoint): TPoint;
26function PointToLineDistance(const P, V, W: TPoint; out Intersect: TPoint): Integer;
27function ComparePoint(P1, P2: TPoint): Boolean;
28function RotatePoint(Center, P: TPoint; Angle: Double): TPoint;
29function RotatePoints(Center: TPoint; P: TPointArray; Angle: Double): TPointArray;
30function LineIntersect(LineAP1, LineAP2, LineBP1, LineBP2: TPoint;
31 out Intersection: TPoint): Boolean;
32function ArcTan2Point(Point: TPoint): Float;
33function ArcTanPoint(Point: TPoint): Float;
34function RectEquals(A, B: TRect): Boolean;
35function RectEnlarge(Rect: TRect; Value: Integer): TRect;
36function ShiftRect(ARect: TRect; Delta: TPoint): TRect;
37
38
39implementation
40
41function Distance(P1, P2: TPoint): Integer;
42begin
43 Result := Trunc(Sqrt(Sqr(P2.X - P1.X) + Sqr(P2.Y - P1.Y)));
44end;
45
46function Dot(const P1, P2: TPoint): Double;
47begin
48 Result := P1.X * P2.X + P1.Y * P2.Y;
49end;
50
51function AddPoint(const P1, P2: TPoint): TPoint;
52begin
53 Result.X := P1.X + P2.X;
54 Result.Y := P1.Y + P2.Y;
55end;
56
57function SubPoint(const P1, P2: TPoint): TPoint;
58begin
59 Result.X := P1.X - P2.X;
60 Result.Y := P1.Y - P2.Y;
61end;
62
63function PointToLineDistance(const P, V, W: TPoint; out Intersect: TPoint): Integer;
64var
65 l2, t: Double;
66 tt: TPoint;
67begin
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;
93end;
94
95function ComparePoint(P1, P2: TPoint): Boolean;
96begin
97 Result := (P1.X = P2.X) and (P1.Y = P2.Y);
98end;
99
100function RotatePoint(Center, P: TPoint; Angle: Double): TPoint;
101begin
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)));
105end;
106
107function RotatePoints(Center: TPoint; P: TPointArray; Angle: Double): TPointArray;
108var
109 I: Integer;
110begin
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);
115end;
116
117function LineIntersect(LineAP1, LineAP2, LineBP1, LineBP2: TPoint;
118 out Intersection: TPoint): Boolean;
119Var
120 LDetLineA, LDetLineB, LDetDivInv: Double;
121 LDiffLA, LDiffLB: TPoint;
122 D: Double;
123begin
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;
145end;
146
147function ArcTan2Point(Point: TPoint): Float;
148begin
149 Result := ArcTan2(Point.Y, Point.X);
150end;
151
152function ArcTanPoint(Point: TPoint): Float;
153begin
154 if Point.Y = 0 then Result := Infinity
155 else Result := ArcTan(Point.X / Point.Y);
156end;
157
158function RectEquals(A, B: TRect): Boolean;
159begin
160 Result := (A.Left = B.Left) and (A.Top = B.Top) and
161 (A.Right = B.Right) and (A.Bottom = B.Bottom);
162end;
163
164function RectEnlarge(Rect: TRect; Value: Integer): TRect;
165begin
166 Result.Left := Rect.Left - Value;
167 Result.Right := Rect.Right + Value;
168 Result.Top := Rect.Top - Value;
169 Result.Bottom := Rect.Bottom + Value;
170end;
171
172function ShiftRect(ARect: TRect; Delta: TPoint): TRect;
173begin
174 Result := Rect(ARect.Left + Delta.X, ARect.Top + Delta.Y,
175 ARect.Right + Delta.X, ARect.Bottom + Delta.Y);
176end;
177
178{ TVector }
179
180function TVector.GetLength: Double;
181begin
182 Result := Sqrt(Sqr(Direction.X) + Sqr(Direction.Y));
183end;
184
185function TVector.GetAngle: Double;
186begin
187 Result := ArcTan2(Direction.Y, Direction.X);
188end;
189
190procedure TVector.SetLength(Value: Double);
191var
192 Angle: Double;
193begin
194 Angle := GetAngle;
195 Direction := Point(Round(Cos(Angle) * Value),
196 Round(Sin(Angle) * Value));
197end;
198
199class function TVector.Create(P1, P2: TPoint): TVector;
200begin
201 Result.Position := P1;
202 Result.Direction := Point(P2.X - P1.X, P2.Y - P1.Y);
203end;
204
205end.
Note: See TracBrowser for help on using the repository browser.