unit UGeometryClasses;

{$mode delphi}{$H+}

interface

uses
  Classes, SysUtils, Math;

type
  TPointArray = array of TPoint;

function Distance(const P1, P2: TPoint): Integer;
function Dot(const P1, P2: TPoint): Double;
function AddPoint(const P1, P2: TPoint): TPoint;
function SubPoint(const P1, P2: TPoint): TPoint;
function PointToLineDistance(const P, V, W: TPoint): Integer;
function ComparePoint(const P1, P2: TPoint): Boolean;
function RotatePoint(const Center, P: TPoint; Angle: Double): TPoint;
function RotatePoints(const Center: TPoint; P: TPointArray; Angle: Double): TPointArray;
function ArcTan2Point(const Point: TPoint): Double;
function ArcTanPoint(const Point: TPoint): Double;
function RectEquals(const A, B: Classes.TRect): Boolean;
function RectEnlarge(const Rect: Classes.TRect; Value: Integer): Classes.TRect;
function ShiftRect(const ARect: TRect; Delta: TPoint): TRect;
function PointsToRect(const P1, P2: TPoint): TRect;
function PointInRect(const P: TPoint; aRect: TRect): Boolean;
function PtInPoly(const Points: array of TPoint; Pos: TPoint): Boolean;
function HalfDistancePoint(const P1, P2: TPoint): TPoint;
function NormalizeAngle(const Angle: Double): Double;
function SubAngle(A1, A2: Double): Double;


implementation

function Distance(const P1, P2: TPoint): Integer;
begin
  Result := Trunc(Sqrt(Sqr(P2.X - P1.X) + Sqr(P2.Y - P1.Y)));
end;

function Dot(const P1, P2: TPoint): Double;
begin
  Result := P1.X * P2.X + P1.Y * P2.Y;
end;

function AddPoint(const P1, P2: TPoint): TPoint;
begin
  Result.X := P1.X + P2.X;
  Result.Y := P1.Y + P2.Y;
end;

function SubPoint(const P1, P2: TPoint): TPoint;
begin
  Result.X := P1.X - P2.X;
  Result.Y := P1.Y - P2.Y;
end;

function PointToLineDistance(const P, V, W: TPoint): Integer;
var
  l2, t: Double;
  tt: TPoint;
begin
  // Return minimum distance between line segment vw and point p
  L2 := Distance(V, W); // i.e. |w-v|^2 -  avoid a sqrt
  L2 := Power(l2, 2);
  if L2 = 0 then begin
    Result := Distance(P, V);   // v == w case
    Exit;
  end;
  // Consider the line extending the segment, parameterized as v + t (w - v).
  // We find projection of point p onto the line.
  // It falls where t = [(p-v) . (w-v)] / |w-v|^2
  T := Dot(SubPoint(P, V), SubPoint(W, V)) / L2;
  if T < 0 then begin
    Result := Distance(P, V);       // Beyond the 'v' end of the segment
    exit;
  end
  else if T > 1 then begin
    Result := Distance(P, W);  // Beyond the 'w' end of the segment
    Exit;
  end;
  TT.X := Trunc(V.X + T * (W.X - V.X));
  TT.Y := Trunc(V.Y + T * (W.Y - V.Y));
  Result := Distance(P, TT);
end;

function ComparePoint(const P1, P2: TPoint): Boolean;
begin
  Result := (P1.X = P2.X) and (P1.Y = P2.Y);
end;

function RotatePoint(const Center, P: TPoint; Angle: Double): TPoint;
var
  D: TPoint;
begin
  D := Point(P.X - Center.X, P.Y - Center.Y);
  Result := Point(Center.X + Round(D.X * Cos(Angle) - D.Y * Sin(Angle)),
    Center.Y + Round(D.X * Sin(Angle) + D.Y * Cos(Angle)));
end;

function RotatePoints(const Center: TPoint; P: TPointArray; Angle: Double): TPointArray;
var
  I: Integer;
begin
  SetLength(Result, Length(P));
  for I := 0 to High(P) do
    Result[I] := RotatePoint(Center, P[I], Angle);
end;

function ArcTan2Point(const Point: TPoint): Double;
begin
  Result := ArcTan2(Point.Y, Point.X);
end;

function ArcTanPoint(const Point: TPoint): Double;
begin
  if Point.Y = 0 then Result := Infinity
    else Result := ArcTan(Point.X / Point.Y);
end;

function RectEquals(const A, B: Classes.TRect): Boolean;
begin
  Result := (A.Left = B.Left) and (A.Top = B.Top) and
    (A.Right = B.Right) and (A.Bottom = B.Bottom);
end;

function RectEnlarge(const Rect: Classes.TRect; Value: Integer): Classes.TRect;
begin
  Result.Left := Rect.Left - Value;
  Result.Right := Rect.Right + Value;
  Result.Top := Rect.Top - Value;
  Result.Bottom := Rect.Bottom + Value;
end;

function ShiftRect(const ARect: TRect; Delta: TPoint): TRect;
begin
  Result := Rect(ARect.Left + Delta.X, ARect.Top + Delta.Y,
    ARect.Right + Delta.X, ARect.Bottom + Delta.Y);
end;

function PointsToRect(const P1, P2: TPoint): TRect;
begin
  if P1.X < P2.X then Result.Left := P1.X else Result.Left := P2.X;
  if P1.Y < P2.Y then Result.Top := P1.Y else Result.Top := P2.Y;
  if P1.X > P2.X then Result.Right := P1.X else Result.Right := P2.X;
  if P1.Y > P2.Y then Result.Bottom := P1.Y else Result.Bottom := P2.Y;
end;

function PointInRect(const P: TPoint; aRect: TRect): Boolean;
begin
  Result := (P.X >= aRect.Left) and (P.X <= aRect.Right) and
    (P.Y >= aRect.Top) and (P.Y <= aRect.Bottom);
end;

function HalfDistancePoint(const P1, P2: TPoint): TPoint;
begin
  Result := Point(P1.X + (P2.X - P1.X) div 2, P1.Y + (P2.Y - P1.Y) div 2)
end;

function NormalizeAngle(const Angle: Double): Double;
begin
  if Angle < 0 then Result := Angle + (Trunc(Angle / (2 * Pi)) + 1) * (2 * Pi)
  else if Angle > 2 * Pi then Result := Angle - Trunc(Angle / (2 * Pi)) * (2 * Pi)
  else Result := Angle;
end;

function SubAngle(A1, A2: Double): Double;
begin
  A1 := NormalizeAngle(A1);
  A2 := NormalizeAngle(A2);
  if A1 < A2 then Result := A1 + 2 * Pi - A2
    else Result := A1 - A2;
end;

function PtInPoly(const Points: array of TPoint; Pos: TPoint): Boolean;
var
  Count, K, J : Integer;
begin
  Result := False;
  Count := Length(Points) ;
  J := Count - 1;
  for K := 0 to Count - 1 do begin
  if ((Points[K].Y <= Pos.Y) and (Pos.Y < Points[J].Y)) or
    ((Points[J].Y <= Pos.Y) and (Pos.Y < Points[K].Y)) then
    begin
    if (Pos.X < (Points[j].X - Points[K].X) *
       (Pos.Y - Points[K].Y) /
       (Points[j].Y - Points[K].Y) + Points[K].X) then
        Result := not Result;
    end;
    J := K;
  end;
end;

end.

