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