close Warning: Can't synchronize with repository "(default)" (No changeset 184 in the repository). Look in the Trac log for more information.

Ignore:
Timestamp:
Nov 23, 2017, 7:05:37 PM (7 years ago)
Author:
chronos
Message:
  • Modified: Use specialized TPointF type from generic class.
  • Modified: Part of fixes for Voronoi map generation.
File:
1 edited

Legend:

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

    r170 r171  
    66
    77uses
    8   Classes, SysUtils, Math;
     8  Classes, SysUtils, Math, typinfo;
    99
    1010type
     
    1616    Y: T;
    1717    constructor Create(const X, Y: T);
    18     procedure Rotate(Base: TGPoint<T>; Angle: Double);
    1918    class operator Add(const A, B: TGPoint<T>): TGPoint<T>;
    2019    class operator Subtract(const A, B: TGPoint<T>): TGPoint<T>;
    2120    class operator GreaterThan(const A, B: TGPoint<T>): Boolean;
     21    class operator GreaterThanOrEqual(const A, B: TGPoint<T>): Boolean;
    2222    class operator LessThan(const A, B: TGPoint<T>): Boolean;
     23    class operator LessThanOrEqual(const A, B: TGPoint<T>): Boolean;
    2324    class operator Equal(const A, B: TGPoint<T>): Boolean;
    2425    function Min(const A, B: TGPoint<T>): TGPoint<T>;
    2526    function Max(const A, B: TGPoint<T>): TGPoint<T>;
     27    procedure Rotate(Base: TGPoint<T>; Angle: Double);
    2628  end;
    2729
     
    9496    procedure AddPoint(const P: T);
    9597    procedure Clear;
    96     procedure CutLine(const Vector: TGLine<T>; const PointInside: T);
     98    procedure CutLine(Vector: TGLine<T>; const PointInside: T);
    9799    property Items[Index: Integer]: T read GetPoint write SetPoint; default;
    98100  end;
     
    185187end;
    186188
    187 function PtInPoly(const Points: array of TPoint; Pos: TPoint): Boolean;
    188 var
    189   Count, K, J : Integer;
    190 begin
    191   Result := False;
    192   Count := Length(Points) ;
    193   J := Count - 1;
    194   for K := 0 to Count - 1 do begin
    195   if ((Points[K].Y <= Pos.Y) and (Pos.Y < Points[J].Y)) or
    196     ((Points[J].Y <= Pos.Y) and (Pos.Y < Points[K].Y)) then
    197     begin
    198     if (Pos.X < (Points[j].X - Points[K].X) *
    199        (Pos.Y - Points[K].Y) /
    200        (Points[j].Y - Points[K].Y) + Points[K].X) then
    201         Result := not Result;
    202     end;
    203     J := K;
    204   end;
    205 end;
    206 
    207 
    208189constructor TGPolygon<T>.Create(const Points: TPointArray);
    209190var
     
    227208var
    228209  I: Integer;
    229   P1: TGPoint<T>;
    230210begin
    231211  if Length(Points) = 0 then
     
    252232end;
    253233
    254 procedure TGPolygon<T>.CutLine(const Vector: TGLine<T>; const PointInside: T);
     234procedure TGPolygon<T>.CutLine(Vector: TGLine<T>; const PointInside: T);
    255235var
    256236  I: Integer;
     
    359339end;
    360340
    361 { TGPoint3D }
    362 
    363 constructor TGPoint3D<T>.Create(const X, Y, Z: T);
    364 begin
    365   Self.X := X;
    366   Self.Y := Y;
    367   Self.Z := Z;
    368 end;
    369 
    370 { TGPoint }
    371 
    372 constructor TGPoint<T>.Create(const X, Y: T);
    373 begin
    374   Self.X := X;
    375   Self.Y := Y;
    376 end;
    377 
    378 class operator TGPoint<T>.Equal(const A, B: TGPoint<T>): Boolean;
    379 begin
    380   Result := (A.X = B.X) and (A.Y = B.Y);
    381 end;
    382 
    383 class operator TGPoint<T>.Add(const A, B: TGPoint<T>): TGPoint<T>;
    384 begin
    385   Result.X := A.X + B.X;
    386   Result.Y := A.Y + B.Y;
    387 end;
    388 
    389 class operator TGPoint<T>.Subtract(const A, B: TGPoint<T>): TGPoint<T>;
    390 begin
    391   Result.X := A.X - B.X;
    392   Result.Y := A.Y - B.Y;
    393 end;
    394 
    395 class operator TGPoint<T>.GreaterThan(const A, B: TGPoint<T>): Boolean;
    396 begin
    397   Result := (B.X > A.X) and (B.Y > A.Y);
    398 end;
    399 
    400 class operator TGPoint<T>.LessThan(const A, B: TGPoint<T>): Boolean;
    401 begin
    402   Result := (B.X < A.X) and (B.Y < A.Y);
    403 end;
    404 
    405 { TGLine }
    406 
    407341class function TGLine<T>.LineIntersect(const LineA, LineB: TGLine<T>; out
    408342  Intersection: T): Boolean;
     
    435369end;
    436370
    437 procedure TGPoint<T>.Rotate(Base: TGPoint<T>; Angle: Double);
    438 var
    439   D: TGPoint<T>;
    440 begin
    441   D := TGPoint<T>.Create(X - Base.X, Y - Base.Y);
    442   //X := Base.X + TypedRound(D.X * Cos(Angle) - D.Y * Sin(Angle));
    443   //Y := Base.Y + TypedRound(D.X * Sin(Angle) + D.Y * Cos(Angle));
     371{ TGPoint3D }
     372
     373constructor TGPoint3D<T>.Create(const X, Y, Z: T);
     374begin
     375  Self.X := X;
     376  Self.Y := Y;
     377  Self.Z := Z;
     378end;
     379
     380{ TGPoint }
     381
     382constructor TGPoint<T>.Create(const X, Y: T);
     383begin
     384  Self.X := X;
     385  Self.Y := Y;
     386end;
     387
     388class operator TGPoint<T>.Equal(const A, B: TGPoint<T>): Boolean;
     389begin
     390  Result := (A.X = B.X) and (A.Y = B.Y);
     391end;
     392
     393class operator TGPoint<T>.Add(const A, B: TGPoint<T>): TGPoint<T>;
     394begin
     395  Result.X := A.X + B.X;
     396  Result.Y := A.Y + B.Y;
     397end;
     398
     399class operator TGPoint<T>.Subtract(const A, B: TGPoint<T>): TGPoint<T>;
     400begin
     401  Result.X := A.X - B.X;
     402  Result.Y := A.Y - B.Y;
     403end;
     404
     405class operator TGPoint<T>.GreaterThan(const A, B: TGPoint<T>): Boolean;
     406begin
     407  Result := (A.X > B.X) and (A.Y > B.Y);
     408end;
     409
     410class operator TGPoint<T>.GreaterThanOrEqual(const A, B: TGPoint<T>): Boolean;
     411begin
     412  Result := (A.X >= B.X) and (A.Y >= B.Y);
     413end;
     414
     415class operator TGPoint<T>.LessThan(const A, B: TGPoint<T>): Boolean;
     416begin
     417  Result := (A.X < B.X) and (A.Y < B.Y);
     418end;
     419
     420class operator TGPoint<T>.LessThanOrEqual(const A, B: TGPoint<T>): Boolean;
     421begin
     422  Result := (A.X <= B.X) and (A.Y <= B.Y);
    444423end;
    445424
     
    456435end;
    457436
     437procedure TGPoint<T>.Rotate(Base: TGPoint<T>; Angle: Double);
     438var
     439  D: TGPoint<T>;
     440begin
     441  D := TGPoint<T>.Create(X - Base.X, Y - Base.Y);
     442  X := Base.X + TypedRound(D.X * Cos(Angle) - D.Y * Sin(Angle));
     443  Y := Base.Y + TypedRound(D.X * Sin(Angle) + D.Y * Cos(Angle));
     444end;
     445
    458446{ TGRect }
    459447
    460448function TGRect<T>.IsPointInside(const P: T): Boolean;
    461449begin
    462   Result := (P > P1) and (P < P2);
     450  Result := (P >= P1) and (P <= P2);
    463451end;
    464452
Note: See TracChangeset for help on using the changeset viewer.