Ignore:
Timestamp:
Jun 5, 2023, 6:44:57 PM (18 months ago)
Author:
chronos
Message:
  • Modified: Remove U prefix from unit names.
  • Modified: Updated Common package.
File:
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/Packages/Common/Geometric.pas

    r144 r145  
    1 unit UGeometric;
     1unit Geometric;
    22
    33interface
     
    88type
    99  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;
    1021
    1122function Distance(P1, P2: TPoint): Integer;
     
    1324function AddPoint(const P1, P2: TPoint): TPoint;
    1425function SubPoint(const P1, P2: TPoint): TPoint;
    15 function PointToLineDistance(const P, V, W: TPoint): Integer;
     26function PointToLineDistance(const P, V, W: TPoint; out Intersect: TPoint): Integer;
    1627function ComparePoint(P1, P2: TPoint): Boolean;
    1728function RotatePoint(Center, P: TPoint; Angle: Double): TPoint;
     
    2536function ShiftRect(ARect: TRect; Delta: TPoint): TRect;
    2637
     38
    2739implementation
    2840
     
    4961end;
    5062
    51 function PointToLineDistance(const P, V, W: TPoint): Integer;
     63function PointToLineDistance(const P, V, W: TPoint; out Intersect: TPoint): Integer;
    5264var
    5365  l2, t: Double;
     
    6779  if T < 0 then begin
    6880    Result := Distance(P, V);       // Beyond the 'v' end of the segment
    69     exit;
     81    Intersect := V;
     82    Exit;
    7083  end
    7184  else if T > 1 then begin
    7285    Result := Distance(P, W);  // Beyond the 'w' end of the segment
     86    Intersect := W;
    7387    Exit;
    7488  end;
     
    7690  TT.Y := Trunc(V.Y + T * (W.Y - V.Y));
    7791  Result := Distance(P, TT);
     92  Intersect := TT;
    7893end;
    7994
     
    161176end;
    162177
     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;
    163204
    164205end.
    165 
Note: See TracChangeset for help on using the changeset viewer.