Changeset 168 for trunk/UMap.pas


Ignore:
Timestamp:
Nov 23, 2017, 10:16:50 AM (7 years ago)
Author:
chronos
Message:
  • Modified: Used TPolygon in TCell.
  • Modified: Moved some generic geometry related functions to UGeometry unit.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UMap.pas

    r167 r168  
    2121    function IsCellsPosNeighbor(CellPos1, CellPos2: TPoint): Boolean;
    2222    procedure GetCellPosNeighbors(CellPos: TPoint; Neighbours: TCells);
    23     function GetHexagonPolygon(Pos: TPoint; Size: TPoint): TPointArray;
     23    function GetHexagonPolygon(Pos: TPoint; Size: TPoint): TPolygon;
    2424  public
    2525    procedure LoadFromFile(FileName: string); override;
     
    3333  TSquareMap = class(TMap)
    3434  private
    35     function GetSquarePolygon(Pos: TPoint; Size: TPoint): TPointArray;
     35    function GetSquarePolygon(Pos: TPoint; Size: TPoint): TPolygon;
    3636  public
    3737    function IsValidIndex(Index: TPoint): Boolean; override;
     
    4343  TTriangleMap = class(TMap)
    4444  private
    45     function GetTrianglePolygon(Pos: TPoint; Size: TPoint; Reverse: Boolean): TPointArray;
     45    function GetTrianglePolygon(Pos: TPoint; Size: TPoint; Reverse: Boolean): TPolygon;
    4646  public
    4747    function IsValidIndex(Index: TPoint): Boolean; override;
     
    6262{ THexMap }
    6363
    64 function THexMap.GetHexagonPolygon(Pos: TPoint; Size: TPoint): TPointArray;
     64function THexMap.GetHexagonPolygon(Pos: TPoint; Size: TPoint): TPolygon;
    6565var
    6666  Shift: TFloatPoint;
    6767begin
    6868  Shift := FloatPoint(0.5 * cos(30 / 180 * Pi), 0.5 * sin(30 / 180 * Pi));
    69   SetLength(Result, 6);
    70   Result[0] := Point(Trunc(Pos.X + 0 * Size.X), Trunc(Pos.Y - 0.5 * Size.Y));
    71   Result[1] := Point(Trunc(Pos.X + Shift.X * Size.X), Trunc(Pos.Y - Shift.Y * Size.Y));
    72   Result[2] := Point(Trunc(Pos.X + Shift.X * Size.X), Trunc(Pos.Y + Shift.Y * Size.Y));
    73   Result[3] := Point(Trunc(Pos.X + 0 * Size.X), Trunc(Pos.Y + 0.5 * Size.Y));
    74   Result[4] := Point(Trunc(Pos.X - Shift.X * Size.X), Trunc(Pos.Y + Shift.Y * Size.Y));
    75   Result[5] := Point(Trunc(Pos.X - Shift.X * Size.X), Trunc(Pos.Y - Shift.Y * Size.Y));
     69  SetLength(Result.Points, 6);
     70  Result.Points[0] := Point(Trunc(Pos.X + 0 * Size.X), Trunc(Pos.Y - 0.5 * Size.Y));
     71  Result.Points[1] := Point(Trunc(Pos.X + Shift.X * Size.X), Trunc(Pos.Y - Shift.Y * Size.Y));
     72  Result.Points[2] := Point(Trunc(Pos.X + Shift.X * Size.X), Trunc(Pos.Y + Shift.Y * Size.Y));
     73  Result.Points[3] := Point(Trunc(Pos.X + 0 * Size.X), Trunc(Pos.Y + 0.5 * Size.Y));
     74  Result.Points[4] := Point(Trunc(Pos.X - Shift.X * Size.X), Trunc(Pos.Y + Shift.Y * Size.Y));
     75  Result.Points[5] := Point(Trunc(Pos.X - Shift.X * Size.X), Trunc(Pos.Y - Shift.Y * Size.Y));
    7676end;
    7777
     
    228228end;
    229229
    230 function TSquareMap.GetSquarePolygon(Pos: TPoint; Size: TPoint): TPointArray;
    231 begin
    232   SetLength(Result, 4);
    233   Result[0] := Point(Trunc(Pos.X - Size.X / 2), Trunc(Pos.Y - Size.Y / 2));
    234   Result[1] := Point(Trunc(Pos.X + Size.X / 2), Trunc(Pos.Y - Size.Y / 2));
    235   Result[2] := Point(Trunc(Pos.X + Size.X / 2), Trunc(Pos.Y + Size.Y / 2));
    236   Result[3] := Point(Trunc(Pos.X - Size.X / 2), Trunc(Pos.Y + Size.Y / 2));
     230function TSquareMap.GetSquarePolygon(Pos: TPoint; Size: TPoint): TPolygon;
     231begin
     232  SetLength(Result.Points, 4);
     233  Result.Points[0] := Point(Trunc(Pos.X - Size.X / 2), Trunc(Pos.Y - Size.Y / 2));
     234  Result.Points[1] := Point(Trunc(Pos.X + Size.X / 2), Trunc(Pos.Y - Size.Y / 2));
     235  Result.Points[2] := Point(Trunc(Pos.X + Size.X / 2), Trunc(Pos.Y + Size.Y / 2));
     236  Result.Points[3] := Point(Trunc(Pos.X - Size.X / 2), Trunc(Pos.Y + Size.Y / 2));
    237237end;
    238238
     
    311311    NewCell.PosPx := Point(Trunc(Random * Size.X * DefaultCellSize.X),
    312312      Trunc(Random * Size.Y * DefaultCellSize.Y));
    313     SetLength(NewCell.Polygon, 1);
    314     NewCell.Polygon[0] := NewCell.PosPx;
     313    SetLength(NewCell.Polygon.Points, 1);
     314    NewCell.Polygon.Points[0] := NewCell.PosPx;
    315315    NewCell.Id := GetNewCellId;
    316316    Cells[Y * Size.X + X] := NewCell;
     
    435435        Polygon.CutLine(L1, Cell.PosPx);
    436436      end;
    437       Cell.Polygon := Polygon.Points;
    438     end else SetLength(Cell.Polygon, 0);
     437      Cell.Polygon := Polygon;
     438    end else Cell.Polygon.Clear;
    439439  end;
    440440
     
    445445
    446446function TTriangleMap.GetTrianglePolygon(Pos: TPoint; Size: TPoint;
    447   Reverse: Boolean): TPointArray;
     447  Reverse: Boolean): TPolygon;
    448448var
    449449  Rev: Integer;
     
    451451  if Reverse then Rev := -1
    452452    else Rev := 1;
    453   SetLength(Result, 3);
    454   Result[0] := Point(Trunc(Pos.X - Size.X / 2), Trunc(Pos.Y - (Size.Y * 0.8) / 2 * Rev));
    455   Result[1] := Point(Trunc(Pos.X + Size.X / 2), Trunc(Pos.Y - (Size.Y * 0.8) / 2 * Rev));
    456   Result[2] := Point(Trunc(Pos.X), Trunc(Pos.Y + (Size.Y * 1.2) / 2 * Rev));
     453  SetLength(Result.Points, 3);
     454  Result.Points[0] := Point(Trunc(Pos.X - Size.X / 2), Trunc(Pos.Y - (Size.Y * 0.8) / 2 * Rev));
     455  Result.Points[1] := Point(Trunc(Pos.X + Size.X / 2), Trunc(Pos.Y - (Size.Y * 0.8) / 2 * Rev));
     456  Result.Points[2] := Point(Trunc(Pos.X), Trunc(Pos.Y + (Size.Y * 1.2) / 2 * Rev));
    457457end;
    458458
Note: See TracChangeset for help on using the changeset viewer.