1 | unit UGeometric;
|
---|
2 |
|
---|
3 | {$mode delphi}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, Math;
|
---|
9 |
|
---|
10 | type
|
---|
11 | TPointArray = array of TPoint;
|
---|
12 |
|
---|
13 | function Distance(P1, P2: TPoint): Integer;
|
---|
14 | function Dot(const P1, P2: TPoint): Double;
|
---|
15 | function AddPoint(const P1, P2: TPoint): TPoint;
|
---|
16 | function SubPoint(const P1, P2: TPoint): TPoint;
|
---|
17 | function PointToLineDistance(const P, V, W: TPoint): Integer;
|
---|
18 | function ComparePoint(P1, P2: TPoint): Boolean;
|
---|
19 | function RotatePoint(Center, P: TPoint; Angle: Double): TPoint;
|
---|
20 | function RotatePoints(Center: TPoint; P: TPointArray; Angle: Double): TPointArray;
|
---|
21 | function LineIntersect(LineAP1, LineAP2, LineBP1, LineBP2: TPoint;
|
---|
22 | out Intersection: TPoint): Boolean;
|
---|
23 | function ArcTan2Point(Point: TPoint): Float;
|
---|
24 | function ArcTanPoint(Point: TPoint): Float;
|
---|
25 | function RectEquals(A, B: TRect): Boolean;
|
---|
26 | function RectEnlarge(Rect: TRect; Value: Integer): TRect;
|
---|
27 | function ShiftRect(ARect: TRect; Delta: TPoint): TRect;
|
---|
28 |
|
---|
29 | implementation
|
---|
30 |
|
---|
31 | function Distance(P1, P2: TPoint): Integer;
|
---|
32 | begin
|
---|
33 | Result := Trunc(Sqrt(Sqr(P2.X - P1.X) + Sqr(P2.Y - P1.Y)));
|
---|
34 | end;
|
---|
35 |
|
---|
36 | function Dot(const P1, P2: TPoint): Double;
|
---|
37 | begin
|
---|
38 | Result := P1.X * P2.X + P1.Y * P2.Y;
|
---|
39 | end;
|
---|
40 |
|
---|
41 | function AddPoint(const P1, P2: TPoint): TPoint;
|
---|
42 | begin
|
---|
43 | Result.X := P1.X + P2.X;
|
---|
44 | Result.Y := P1.Y + P2.Y;
|
---|
45 | end;
|
---|
46 |
|
---|
47 | function SubPoint(const P1, P2: TPoint): TPoint;
|
---|
48 | begin
|
---|
49 | Result.X := P1.X - P2.X;
|
---|
50 | Result.Y := P1.Y - P2.Y;
|
---|
51 | end;
|
---|
52 |
|
---|
53 | function PointToLineDistance(const P, V, W: TPoint): Integer;
|
---|
54 | var
|
---|
55 | l2, t: Double;
|
---|
56 | tt: TPoint;
|
---|
57 | begin
|
---|
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);
|
---|
80 | end;
|
---|
81 |
|
---|
82 | function ComparePoint(P1, P2: TPoint): Boolean;
|
---|
83 | begin
|
---|
84 | Result := (P1.X = P2.X) and (P1.Y = P2.Y);
|
---|
85 | end;
|
---|
86 |
|
---|
87 | function RotatePoint(Center, P: TPoint; Angle: Double): TPoint;
|
---|
88 | begin
|
---|
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)));
|
---|
92 | end;
|
---|
93 |
|
---|
94 | function RotatePoints(Center: TPoint; P: TPointArray; Angle: Double): TPointArray;
|
---|
95 | var
|
---|
96 | I: Integer;
|
---|
97 | begin
|
---|
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);
|
---|
102 | end;
|
---|
103 |
|
---|
104 | function LineIntersect(LineAP1, LineAP2, LineBP1, LineBP2: TPoint;
|
---|
105 | out Intersection: TPoint): Boolean;
|
---|
106 | Var
|
---|
107 | LDetLineA, LDetLineB, LDetDivInv: Double;
|
---|
108 | LDiffLA, LDiffLB: TPoint;
|
---|
109 | D: Double;
|
---|
110 | begin
|
---|
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;
|
---|
132 | end;
|
---|
133 |
|
---|
134 | function ArcTan2Point(Point: TPoint): Float;
|
---|
135 | begin
|
---|
136 | Result := ArcTan2(Point.Y, Point.X);
|
---|
137 | end;
|
---|
138 |
|
---|
139 | function ArcTanPoint(Point: TPoint): Float;
|
---|
140 | begin
|
---|
141 | if Point.Y = 0 then Result := Infinity
|
---|
142 | else Result := ArcTan(Point.X / Point.Y);
|
---|
143 | end;
|
---|
144 |
|
---|
145 | function RectEquals(A, B: TRect): Boolean;
|
---|
146 | begin
|
---|
147 | Result := (A.Left = B.Left) and (A.Top = B.Top) and
|
---|
148 | (A.Right = B.Right) and (A.Bottom = B.Bottom);
|
---|
149 | end;
|
---|
150 |
|
---|
151 | function RectEnlarge(Rect: TRect; Value: Integer): TRect;
|
---|
152 | begin
|
---|
153 | Result.Left := Rect.Left - Value;
|
---|
154 | Result.Right := Rect.Right + Value;
|
---|
155 | Result.Top := Rect.Top - Value;
|
---|
156 | Result.Bottom := Rect.Bottom + Value;
|
---|
157 | end;
|
---|
158 |
|
---|
159 | function ShiftRect(ARect: TRect; Delta: TPoint): TRect;
|
---|
160 | begin
|
---|
161 | Result := Rect(ARect.Left + Delta.X, ARect.Top + Delta.Y,
|
---|
162 | ARect.Right + Delta.X, ARect.Bottom + Delta.Y);
|
---|
163 | end;
|
---|
164 |
|
---|
165 |
|
---|
166 | end.
|
---|
167 |
|
---|