Changeset 562 for Common/UGeometric.pas


Ignore:
Timestamp:
May 27, 2023, 11:00:20 AM (18 months ago)
Author:
chronos
Message:
  • Modified: Updated Common packages.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Common/UGeometric.pas

    r558 r562  
    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.