source: trunk/Packages/Common/UGeometric.pas

Last change on this file was 215, checked in by chronos, 3 years ago
  • Modified: Build under Lazarus 2.2.0.
  • Modified: Updated Common package.
File size: 4.5 KB
Line 
1unit UGeometric;
2
3{$mode delphi}
4
5interface
6
7uses
8 Classes, SysUtils, Math;
9
10type
11 TPointArray = array of TPoint;
12
13function Distance(P1, P2: TPoint): Integer;
14function Dot(const P1, P2: TPoint): Double;
15function AddPoint(const P1, P2: TPoint): TPoint;
16function SubPoint(const P1, P2: TPoint): TPoint;
17function PointToLineDistance(const P, V, W: TPoint): Integer;
18function ComparePoint(P1, P2: TPoint): Boolean;
19function RotatePoint(Center, P: TPoint; Angle: Double): TPoint;
20function RotatePoints(Center: TPoint; P: TPointArray; Angle: Double): TPointArray;
21function LineIntersect(LineAP1, LineAP2, LineBP1, LineBP2: TPoint;
22 out Intersection: TPoint): Boolean;
23function ArcTan2Point(Point: TPoint): Float;
24function ArcTanPoint(Point: TPoint): Float;
25function RectEquals(A, B: TRect): Boolean;
26function RectEnlarge(Rect: TRect; Value: Integer): TRect;
27function ShiftRect(ARect: TRect; Delta: TPoint): TRect;
28
29implementation
30
31function Distance(P1, P2: TPoint): Integer;
32begin
33 Result := Trunc(Sqrt(Sqr(P2.X - P1.X) + Sqr(P2.Y - P1.Y)));
34end;
35
36function Dot(const P1, P2: TPoint): Double;
37begin
38 Result := P1.X * P2.X + P1.Y * P2.Y;
39end;
40
41function AddPoint(const P1, P2: TPoint): TPoint;
42begin
43 Result.X := P1.X + P2.X;
44 Result.Y := P1.Y + P2.Y;
45end;
46
47function SubPoint(const P1, P2: TPoint): TPoint;
48begin
49 Result.X := P1.X - P2.X;
50 Result.Y := P1.Y - P2.Y;
51end;
52
53function PointToLineDistance(const P, V, W: TPoint): Integer;
54var
55 l2, t: Double;
56 tt: TPoint;
57begin
58 // Return minimum distance between line segment vw and point p
59 L2 := Distance(V, W); // i.e. |w-v|^2 - avoid a sqrt
60 L2 := Power(l2, 2);
61 if L2 = 0 then begin
62 Result := Distance(P, V); // v == w case
63 Exit;
64 end;
65 // Consider the line extending the segment, parameterized as v + t (w - v).
66 // We find projection of point p onto the line.
67 // It falls where t = [(p-v) . (w-v)] / |w-v|^2
68 T := Dot(SubPoint(P, V), SubPoint(W, V)) / L2;
69 if T < 0 then begin
70 Result := Distance(P, V); // Beyond the 'v' end of the segment
71 exit;
72 end
73 else if T > 1 then begin
74 Result := Distance(P, W); // Beyond the 'w' end of the segment
75 Exit;
76 end;
77 TT.X := Trunc(V.X + T * (W.X - V.X));
78 TT.Y := Trunc(V.Y + T * (W.Y - V.Y));
79 Result := Distance(P, TT);
80end;
81
82function ComparePoint(P1, P2: TPoint): Boolean;
83begin
84 Result := (P1.X = P2.X) and (P1.Y = P2.Y);
85end;
86
87function RotatePoint(Center, P: TPoint; Angle: Double): TPoint;
88begin
89 P := Point(P.X - Center.X, P.Y - Center.Y);
90 Result := Point(Center.X + Round(P.X * Cos(Angle) - P.Y * Sin(Angle)),
91 Center.Y + Round(P.X * Sin(Angle) + P.Y * Cos(Angle)));
92end;
93
94function RotatePoints(Center: TPoint; P: TPointArray; Angle: Double): TPointArray;
95var
96 I: Integer;
97begin
98 Result := Default(TPointArray);
99 SetLength(Result, Length(P));
100 for I := 0 to High(P) do
101 Result[I] := RotatePoint(Center, P[I], Angle);
102end;
103
104function LineIntersect(LineAP1, LineAP2, LineBP1, LineBP2: TPoint;
105 out Intersection: TPoint): Boolean;
106Var
107 LDetLineA, LDetLineB, LDetDivInv: Double;
108 LDiffLA, LDiffLB: TPoint;
109 D: Double;
110begin
111 if (LineAP1 = LineAP2) or (LineBP1 = LineBP2) then begin
112 Result := False;
113 Exit;
114 end;
115 LDetLineA := LineAP1.X * LineAP2.Y - LineAP1.Y * LineAP2.X;
116 LDetLineB := LineBP1.X * LineBP2.Y - LineBP1.Y * LineBP2.X;
117
118 LDiffLA := SubPoint(LineAP1, LineAP2);
119 LDiffLB := SubPoint(LineBP1, LineBP2);
120
121 D := ((LDiffLA.X * LDiffLB.Y) - (LDiffLA.Y * LDiffLB.X));
122 if D = 0 then begin
123 // Parallel lines without intersection
124 Result := False;
125 Exit;
126 end;
127 LDetDivInv := 1 / D;
128
129 Intersection.X := Trunc(((LDetLineA * LDiffLB.X) - (LDiffLA.X * LDetLineB)) * LDetDivInv);
130 Intersection.Y := Trunc(((LDetLineA * LDiffLB.Y) - (LDiffLA.Y * LDetLineB)) * LDetDivInv);
131 Result := True;
132end;
133
134function ArcTan2Point(Point: TPoint): Float;
135begin
136 Result := ArcTan2(Point.Y, Point.X);
137end;
138
139function ArcTanPoint(Point: TPoint): Float;
140begin
141 if Point.Y = 0 then Result := Infinity
142 else Result := ArcTan(Point.X / Point.Y);
143end;
144
145function RectEquals(A, B: TRect): Boolean;
146begin
147 Result := (A.Left = B.Left) and (A.Top = B.Top) and
148 (A.Right = B.Right) and (A.Bottom = B.Bottom);
149end;
150
151function RectEnlarge(Rect: TRect; Value: Integer): TRect;
152begin
153 Result.Left := Rect.Left - Value;
154 Result.Right := Rect.Right + Value;
155 Result.Top := Rect.Top - Value;
156 Result.Bottom := Rect.Bottom + Value;
157end;
158
159function ShiftRect(ARect: TRect; Delta: TPoint): TRect;
160begin
161 Result := Rect(ARect.Left + Delta.X, ARect.Top + Delta.Y,
162 ARect.Right + Delta.X, ARect.Bottom + Delta.Y);
163end;
164
165
166end.
167
Note: See TracBrowser for help on using the repository browser.