Ignore:
Timestamp:
Nov 29, 2023, 2:35:44 PM (12 months ago)
Author:
chronos
Message:
  • Modified: HighDpi branch updated to trunk version.
File:
1 moved

Legend:

Unmodified
Added
Removed
  • branches/highdpi/Packages/Common/Geometric.pas

    r462 r463  
    1 unit UGeometric;
    2 
    3 {$mode delphi}
     1unit Geometric;
    42
    53interface
     
    108type
    119  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;
    1221
    1322function Distance(P1, P2: TPoint): Integer;
     
    1524function AddPoint(const P1, P2: TPoint): TPoint;
    1625function SubPoint(const P1, P2: TPoint): TPoint;
    17 function PointToLineDistance(const P, V, W: TPoint): Integer;
     26function PointToLineDistance(const P, V, W: TPoint; out Intersect: TPoint): Integer;
    1827function ComparePoint(P1, P2: TPoint): Boolean;
    1928function RotatePoint(Center, P: TPoint; Angle: Double): TPoint;
     
    2736function ShiftRect(ARect: TRect; Delta: TPoint): TRect;
    2837
     38
    2939implementation
    3040
     
    5161end;
    5262
    53 function PointToLineDistance(const P, V, W: TPoint): Integer;
     63function PointToLineDistance(const P, V, W: TPoint; out Intersect: TPoint): Integer;
    5464var
    5565  l2, t: Double;
     
    6979  if T < 0 then begin
    7080    Result := Distance(P, V);       // Beyond the 'v' end of the segment
    71     exit;
     81    Intersect := V;
     82    Exit;
    7283  end
    7384  else if T > 1 then begin
    7485    Result := Distance(P, W);  // Beyond the 'w' end of the segment
     86    Intersect := W;
    7587    Exit;
    7688  end;
     
    7890  TT.Y := Trunc(V.Y + T * (W.Y - V.Y));
    7991  Result := Distance(P, TT);
     92  Intersect := TT;
    8093end;
    8194
     
    96109  I: Integer;
    97110begin
     111  Result := Default(TPointArray);
    98112  SetLength(Result, Length(P));
    99113  for I := 0 to High(P) do
     
    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.
Note: See TracChangeset for help on using the changeset viewer.