Ignore:
Timestamp:
Jun 28, 2024, 11:08:43 PM (5 months ago)
Author:
chronos
Message:
  • Modified: Updated Common package.
File:
1 moved

Legend:

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

    r29 r30  
    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;
     
    5061end;
    5162
    52 function PointToLineDistance(const P, V, W: TPoint): Integer;
     63function PointToLineDistance(const P, V, W: TPoint; out Intersect: TPoint): Integer;
    5364var
    5465  l2, t: Double;
     
    6879  if T < 0 then begin
    6980    Result := Distance(P, V);       // Beyond the 'v' end of the segment
    70     exit;
     81    Intersect := V;
     82    Exit;
    7183  end
    7284  else if T > 1 then begin
    7385    Result := Distance(P, W);  // Beyond the 'w' end of the segment
     86    Intersect := W;
    7487    Exit;
    7588  end;
     
    7790  TT.Y := Trunc(V.Y + T * (W.Y - V.Y));
    7891  Result := Distance(P, TT);
     92  Intersect := TT;
    7993end;
    8094
     
    162176end;
    163177
     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;
    164204
    165205end.
    166 
Note: See TracChangeset for help on using the changeset viewer.