Changeset 168 for trunk


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

Legend:

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

    r167 r168  
    3838    function Create(const Points: TPointArray): TPolygon; overload;
    3939    function Create(const Rect: TRect): TPolygon; overload;
     40    function GetRect: TRect;
    4041    procedure AddPoint(const P: TPoint);
    4142    procedure Clear;
     
    6364function SubAngle(A1, A2: Double): Double;
    6465
     66
    6567implementation
    6668
     
    291293  Result.Points[2] := Point(Rect.Right, Rect.Bottom);
    292294  Result.Points[3] := Point(Rect.Left, Rect.Bottom);
     295end;
     296
     297function TPolygon.GetRect: TRect;
     298var
     299  I: Integer;
     300begin
     301  Result := Rect(High(Integer), High(Integer),
     302    Low(Integer), Low(Integer));
     303  for I := 0 to Length(Points) - 1 do
     304  with Points[I] do begin
     305    if X > Result.Right then
     306      Result.Right := X;
     307    if X < Result.Left then
     308      Result.Left := X;
     309    if Y > Result.Bottom then
     310      Result.Bottom := Y;
     311    if Y < Result.Top then
     312      Result.Top := Y;
     313  end;
    293314end;
    294315
  • trunk/UGame.pas

    r167 r168  
    3535  end;
    3636
    37   TPointArray = array of TPoint;
    38 
    3937  TTerrainType = (ttVoid, ttNormal, ttCity);
    4038
     
    5149    Id: Integer;
    5250    PosPx: TPoint;
    53     Polygon: TPointArray;
     51    Polygon: TPolygon;
    5452    Terrain: TTerrainType;
    5553    PlayerId: Integer;
     
    564562end;
    565563
    566 function RectEquals(A, B: TRect): Boolean;
    567 begin
    568   Result := (A.Left = B.Left) and (A.Top = B.Top) and
    569     (A.Right = B.Right) and (A.Bottom = B.Bottom);
    570 end;
    571 
    572 function PtInRect(const Rect: TRect; Pos: TPoint): Boolean;
    573 begin
    574   Result := (Pos.X >= Rect.Left) and (Pos.Y >= Rect.Top) and
    575     (Pos.X <= Rect.Right) and (Pos.Y <= Rect.Bottom);
    576 end;
    577 
    578 function PtInPoly(const Points: array of TPoint; Pos: TPoint): Boolean;
    579 var
    580   Count, K, J : Integer;
    581 begin
    582   Result := False;
    583   Count := Length(Points) ;
    584   J := Count - 1;
    585   for K := 0 to Count - 1 do begin
    586   if ((Points[K].Y <= Pos.Y) and (Pos.Y < Points[J].Y)) or
    587     ((Points[J].Y <= Pos.Y) and (Pos.Y < Points[K].Y)) then
    588     begin
    589     if (Pos.X < (Points[j].X - Points[K].X) *
    590        (Pos.Y - Points[K].Y) /
    591        (Points[j].Y - Points[K].Y) + Points[K].X) then
    592         Result := not Result;
    593     end;
    594     J := K;
    595   end;
    596 end;
    597 
    598 function GetPolygonRect(Polygon: array of TPoint): TRect;
    599 var
    600   I: Integer;
    601 begin
    602   Result := Rect(High(Integer), High(Integer),
    603     Low(Integer), Low(Integer));
    604   for I := 0 to Length(Polygon) - 1 do
    605   with Polygon[I] do begin
    606     if X > Result.Right then
    607       Result.Right := X;
    608     if X < Result.Left then
    609       Result.Left := X;
    610     if Y > Result.Bottom then
    611       Result.Bottom := Y;
    612     if Y < Result.Top then
    613       Result.Top := Y;
    614   end;
    615 end;
    616 
    617564function HalfColor(Color: TColor): TColor;
    618565begin
     
    15711518  for I := 0 to Cells.Count - 1 do
    15721519  if TCell(Cells[I]).Terrain <> ttVoid then begin
    1573     if PtInPoly(TCell(Cells[I]).Polygon, Pos) then begin
     1520    if TCell(Cells[I]).Polygon.IsPointInside(Pos) then begin
    15741521      Result := TCell(Cells[I]);
    15751522      Exit;
     
    16411588    end;
    16421589    // Transform view
    1643     SetLength(Points, Length(Cell.Polygon));
     1590    SetLength(Points, Length(Cell.Polygon.Points));
    16441591    for I := 0 to Length(Points) - 1 do
    1645       Points[I] := View.CellToCanvasPos(Cell.Polygon[I]);
     1592      Points[I] := View.CellToCanvasPos(Cell.Polygon.Points[I]);
    16461593    Brush.Style := bsSolid;
    16471594    //Polygon(Points, False, 0, Length(Points));
     
    16951642    NewCell.PosPx := Point(X * DefaultCellSize.X, Y * DefaultCellSize.Y);
    16961643    NewCell.Id := GetNewCellId;
    1697     SetLength(NewCell.Polygon, 1);
    1698     NewCell.Polygon[0] := NewCell.PosPx;
     1644    SetLength(NewCell.Polygon.Points, 1);
     1645    NewCell.Polygon.Points[0] := NewCell.PosPx;
    16991646    Cells[Y * FSize.X + X] := NewCell;
    17001647  end;
     
    18121759  // This is generic algorithm to determine pixel size of entire map
    18131760  for I := 0 to Cells.Count - 1 do begin
    1814     CellRect := GetPolygonRect(TCell(Cells[I]).Polygon);
     1761    CellRect := TCell(Cells[I]).Polygon.GetRect;
    18151762    if I = 0 then Result := CellRect
    18161763      else begin
     
    20341981  Node3 := Node.FindNode('Polygon');
    20351982  if Assigned(Node3) then begin
    2036     SetLength(Polygon, 0);
     1983    Polygon.Clear;
    20371984    Node2 := Node3.FirstChild;
    20381985    while Assigned(Node2) and (Node2.NodeName = 'Point') do begin
    2039       SetLength(Polygon, Length(Polygon) + 1);
    2040       Polygon[High(Polygon)].X := ReadInteger(Node2, 'X', 0);
    2041       Polygon[High(Polygon)].Y := ReadInteger(Node2, 'Y', 0);
     1986      Polygon.AddPoint(Point(ReadInteger(Node2, 'X', 0), ReadInteger(Node2, 'Y', 0)));
    20421987      Node2 := Node2.NextSibling;
    20431988    end;
     
    20682013  NewNode := Node.OwnerDocument.CreateElement('Polygon');
    20692014  Node.AppendChild(NewNode);
    2070   for I := 0 to Length(Polygon) - 1 do begin
     2015  for I := 0 to Length(Polygon.Points) - 1 do begin
    20712016    NewNode2 := NewNode.OwnerDocument.CreateElement('Point');
    20722017    NewNode.AppendChild(NewNode2);
    2073     WriteInteger(NewNode2, 'X', Polygon[I].X);
    2074     WriteInteger(NewNode2, 'Y', Polygon[I].Y);
     2018    WriteInteger(NewNode2, 'X', Polygon.Points[I].X);
     2019    WriteInteger(NewNode2, 'Y', Polygon.Points[I].Y);
    20752020  end;
    20762021end;
     
    20912036  RectA, RectB: TRect;
    20922037begin
    2093   RectA := GetPolygonRect(Polygon);
     2038  RectA := Polygon.GetRect;
    20942039  RectB := View.SourceRect;
    20952040  Result := ((RectA.Left < RectB.Right) and (RectA.Right > RectB.Left) and
  • 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.