Changeset 171 for trunk


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.
Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/Forms/UFormMain.pas

    r170 r171  
    247247procedure TFormMain.AZoomAllExecute(Sender: TObject);
    248248var
    249   Factor: TFloatPoint;
     249  Factor: TPointF;
    250250  MapRect: TRect;
    251251  NewZoom: Single;
     
    253253  with Core, Game, CurrentClient, View do begin
    254254    MapRect := Map.CalculatePixelRect;
    255     Factor := FloatPoint(DestRect.Size.X / MapRect.Size.X,
     255    Factor := TPointF.Create(DestRect.Size.X / MapRect.Size.X,
    256256      DestRect.Size.Y / MapRect.Size.Y);
    257257    if Factor.X < Factor.Y then NewZoom := Factor.X
  • 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
  • trunk/Packages/Common/UGeometryClasses.pas

    r170 r171  
    2626function PointsToRect(const P1, P2: TPoint): TRect;
    2727function PointInRect(const P: TPoint; aRect: TRect): Boolean;
     28function PtInPoly(const Points: array of TPoint; Pos: TPoint): Boolean;
    2829function HalfDistancePoint(const P1, P2: TPoint): TPoint;
    2930function NormalizeAngle(const Angle: Double): Double;
     
    172173end;
    173174
     175function PtInPoly(const Points: array of TPoint; Pos: TPoint): Boolean;
     176var
     177  Count, K, J : Integer;
     178begin
     179  Result := False;
     180  Count := Length(Points) ;
     181  J := Count - 1;
     182  for K := 0 to Count - 1 do begin
     183  if ((Points[K].Y <= Pos.Y) and (Pos.Y < Points[J].Y)) or
     184    ((Points[J].Y <= Pos.Y) and (Pos.Y < Points[K].Y)) then
     185    begin
     186    if (Pos.X < (Points[j].X - Points[K].X) *
     187       (Pos.Y - Points[K].Y) /
     188       (Points[j].Y - Points[K].Y) + Points[K].X) then
     189        Result := not Result;
     190    end;
     191    J := K;
     192  end;
     193end;
    174194
    175195end.
  • trunk/UGame.pas

    r170 r171  
    3030  TMapArea = class;
    3131  TClient = class;
    32 
    33   TFloatPoint = record
    34     X, Y: Double;
    35   end;
    3632
    3733  TTerrainType = (ttVoid, ttNormal, ttCity);
     
    525521
    526522procedure InitStrings;
    527 function FloatPoint(AX, AY: Double): TFloatPoint;
    528523
    529524resourcestring
     
    554549  PlayerModeText[pmHuman] := SHuman;
    555550  PlayerModeText[pmComputer] := SComputer;
    556 end;
    557 
    558 function FloatPoint(AX, AY: Double): TFloatPoint;
    559 begin
    560   Result.X := AX;
    561   Result.Y := AY;
    562551end;
    563552
     
    13931382var
    13941383  Points: array of Classes.TPoint;
    1395   FPoints: array of TFloatPoint;
     1384  FPoints: array of TPointF;
    13961385  I: Integer;
    13971386  ArrowSize: TPoint;
     
    14031392  ArrowSize := TPoint.Create(Trunc(DefaultCellSize.X / 3 * View.Zoom),
    14041393    Trunc(DefaultCellSize.Y / 3 * View.Zoom));
    1405   FPoints[0] := FloatPoint(+0.5 * ArrowSize.X, +0 * ArrowSize.Y);
    1406   FPoints[1] := FloatPoint(+0 * ArrowSize.X, +0.5 * ArrowSize.Y);
    1407   FPoints[2] := FloatPoint(+0 * ArrowSize.X, +0.25 * ArrowSize.Y);
    1408   FPoints[3] := FloatPoint(-0.5 * ArrowSize.X, +0.25 * ArrowSize.Y);
    1409   FPoints[4] := FloatPoint(-0.5 * ArrowSize.X, -0.25 * ArrowSize.Y);
    1410   FPoints[5] := FloatPoint(+0 * ArrowSize.X, -0.25 * ArrowSize.Y);
    1411   FPoints[6] := FloatPoint(+0 * ArrowSize.X, -0.5 * ArrowSize.Y);
    1412   FPoints[7] := FloatPoint(+0.5 * ArrowSize.X, 0 * ArrowSize.Y);
     1394  FPoints[0] := TPointF.Create(+0.5 * ArrowSize.X, +0 * ArrowSize.Y);
     1395  FPoints[1] := TPointF.Create(+0 * ArrowSize.X, +0.5 * ArrowSize.Y);
     1396  FPoints[2] := TPointF.Create(+0 * ArrowSize.X, +0.25 * ArrowSize.Y);
     1397  FPoints[3] := TPointF.Create(-0.5 * ArrowSize.X, +0.25 * ArrowSize.Y);
     1398  FPoints[4] := TPointF.Create(-0.5 * ArrowSize.X, -0.25 * ArrowSize.Y);
     1399  FPoints[5] := TPointF.Create(+0 * ArrowSize.X, -0.25 * ArrowSize.Y);
     1400  FPoints[6] := TPointF.Create(+0 * ArrowSize.X, -0.5 * ArrowSize.Y);
     1401  FPoints[7] := TPointF.Create(+0.5 * ArrowSize.X, 0 * ArrowSize.Y);
    14131402  // Rotate
    14141403  for I := 0 to Length(Points) - 1 do
    1415     FPoints[I] := FloatPoint(FPoints[I].X * Cos(Angle) - FPoints[I].Y * Sin(Angle),
     1404    FPoints[I] := TPointF.Create(FPoints[I].X * Cos(Angle) - FPoints[I].Y * Sin(Angle),
    14161405      FPoints[I].X * Sin(Angle) + FPoints[I].Y * Cos(Angle));
    14171406  // Shift
  • trunk/UMap.pas

    r170 r171  
    66
    77uses
    8   Classes, SysUtils, UGame, XMLRead, XMLWrite, DOM, Contnrs, UGeometryClasses,
    9   UGeometry;
     8  Classes, SysUtils, UGame, XMLRead, XMLWrite, DOM, UGeometryClasses,
     9  UGeometry, fgl;
    1010
    1111type
     
    6464function THexMap.GetHexagonPolygon(Pos: TPoint; Size: TPoint): TPolygon;
    6565var
    66   Shift: TFloatPoint;
    67 begin
    68   Shift := FloatPoint(0.5 * cos(30 / 180 * Pi), 0.5 * sin(30 / 180 * Pi));
     66  Shift: TPointF;
     67begin
     68  Shift := TPointF.Create(0.5 * cos(30 / 180 * Pi), 0.5 * sin(30 / 180 * Pi));
    6969  SetLength(Result.Points, 6);
    7070  Result.Points[0] := TPoint.Create(Trunc(Pos.X + 0 * Size.X), Trunc(Pos.Y - 0.5 * Size.Y));
     
    246246end;
    247247
    248 function CompareDistanceReverse(C1, C2: Pointer): Integer;
    249 begin
    250   if TCellsDistance(C1).Distance > TCellsDistance(C2).Distance then Result := -1
    251   else if TCellsDistance(C1).Distance < TCellsDistance(C2).Distance then Result := 1
     248function CompareDistanceReverse(const C1, C2: TCellsDistance): Integer;
     249begin
     250  if C1.Distance > C2.Distance then Result := -1
     251  else if C1.Distance < C2.Distance then Result := 1
    252252  else Result := 0;
    253253end;
     
    258258  I1, I2: Integer;
    259259  NewCell: TCell;
    260   CellsDistance: TObjectList; // TObjectList<TCellsDistance>
     260  CellsDistance: TFPGObjectList<TCellsDistance>;
    261261  NewCellDist: TCellsDistance;
    262   SelectedCells: TObjectList; // TObjectList<TCellsDistance>
     262  SelectedCells: TFPGObjectList<TCellsDistance>;
    263263  I, J: Integer;
    264264  Intersected: Boolean;
     
    312312
    313313  // Calculate distance between all cells
    314   CellsDistance := TObjectList.Create;
     314  CellsDistance := TFPGObjectList<TCellsDistance>.Create;
    315315  for I1 := 1 to Cells.Count - 1 do
    316316  for I2 := I1 + 1 to Cells.Count - 1 do begin
     
    324324
    325325  // Keep shortest non-intersected cell pairs
    326   SelectedCells := TObjectList.Create;
    327   SelectedCells.OwnsObjects := False;
     326  SelectedCells := TFPGObjectList<TCellsDistance>.Create;
     327  SelectedCells.FreeObjects := False;
    328328  I := CellsDistance.Count - 1;
    329329  while I >= 0 do begin
Note: See TracChangeset for help on using the changeset viewer.