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

Changeset 170 for trunk/UMap.pas


Ignore:
Timestamp:
Nov 23, 2017, 5:02:49 PM (6 years ago)
Author:
chronos
Message:
  • Modified: UGeometry unit rewritten to use generics and define own TPoint and TRect types.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UMap.pas

    r168 r170  
    66
    77uses
    8   Classes, SysUtils, UGame, XMLRead, XMLWrite, DOM, Contnrs, UGeometry;
     8  Classes, SysUtils, UGame, XMLRead, XMLWrite, DOM, Contnrs, UGeometryClasses,
     9  UGeometry;
    910
    1011type
     
    5354  TVoronoiMap = class(TMap)
    5455  private
    55     function GetTrianglePolygon(Pos: TPoint; Size: TPoint; Reverse: Boolean): TPointArray;
    5656  public
    5757    procedure Generate; override;
     
    6868  Shift := FloatPoint(0.5 * cos(30 / 180 * Pi), 0.5 * sin(30 / 180 * Pi));
    6969  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));
     70  Result.Points[0] := TPoint.Create(Trunc(Pos.X + 0 * Size.X), Trunc(Pos.Y - 0.5 * Size.Y));
     71  Result.Points[1] := TPoint.Create(Trunc(Pos.X + Shift.X * Size.X), Trunc(Pos.Y - Shift.Y * Size.Y));
     72  Result.Points[2] := TPoint.Create(Trunc(Pos.X + Shift.X * Size.X), Trunc(Pos.Y + Shift.Y * Size.Y));
     73  Result.Points[3] := TPoint.Create(Trunc(Pos.X + 0 * Size.X), Trunc(Pos.Y + 0.5 * Size.Y));
     74  Result.Points[4] := TPoint.Create(Trunc(Pos.X - Shift.X * Size.X), Trunc(Pos.Y + Shift.Y * Size.Y));
     75  Result.Points[5] := TPoint.Create(Trunc(Pos.X - Shift.X * Size.X), Trunc(Pos.Y - Shift.Y * Size.Y));
    7676end;
    7777
     
    139139  for Y := -1 to 1 do
    140140  for X := -1 to 1 do
    141   if IsValidIndex(Point(CellPos.X + X, CellPos.Y + Y)) and
    142   IsCellsPosNeighbor(CellPos, Point((CellPos.X + X), (CellPos.Y + Y))) then begin
     141  if IsValidIndex(TPoint.Create(CellPos.X + X, CellPos.Y + Y)) and
     142  IsCellsPosNeighbor(CellPos, TPoint.Create((CellPos.X + X), (CellPos.Y + Y))) then begin
    143143    Neighbours.Add(TCell(Cells[(CellPos.Y + Y) * Size.X + (CellPos.X + X)]));
    144144  end;
     
    165165      //Y := Y + 0.5;
    166166    end;
    167     NewCell.PosPx := Point(Trunc(PX * DefaultCellSize.X / HexCellMulX),
     167    NewCell.PosPx := TPoint.Create(Trunc(PX * DefaultCellSize.X / HexCellMulX),
    168168      Trunc(PY * DefaultCellSize.Y / HexCellMulY));
    169169    NewCell.Polygon := GetHexagonPolygon(NewCell.PosPx, DefaultCellSize);
     
    176176  for X := 0 to Size.X - 1 do
    177177  with TCell(Cells[Y * Size.X + X]) do begin
    178     GetCellPosNeighbors(Point(X, Y), Neighbors);
     178    GetCellPosNeighbors(TPoint.Create(X, Y), Neighbors);
    179179  end;
    180180
     
    198198    NewCell := TCell.Create;
    199199    NewCell.Map := Self;
    200     NewCell.PosPx := Point(Trunc(X * DefaultCellSize.X * SquareCellMulX),
     200    NewCell.PosPx := TPoint.Create(Trunc(X * DefaultCellSize.X * SquareCellMulX),
    201201      Trunc(Y * DefaultCellSize.Y * SquareCellMulY));
    202202    NewCell.Polygon := GetSquarePolygon(NewCell.PosPx, DefaultCellSize);
     
    209209  for X := 0 to Size.X - 1 do
    210210  with TCell(Cells[Y * Size.X + X]) do begin
    211     if IsValidIndex(Point(X + 1, Y + 0)) then
     211    if IsValidIndex(TPoint.Create(X + 1, Y + 0)) then
    212212      Neighbors.Add(TCell(Cells[(Y + 0) * Size.X + (X + 1)]));
    213     if IsValidIndex(Point(X + 0, Y + 1)) then
     213    if IsValidIndex(TPoint.Create(X + 0, Y + 1)) then
    214214      Neighbors.Add(TCell(Cells[(Y + 1) * Size.X + (X + 0)]));
    215     if IsValidIndex(Point(X - 1, Y + 0)) then
     215    if IsValidIndex(TPoint.Create(X - 1, Y + 0)) then
    216216      Neighbors.Add(TCell(Cells[(Y + 0) * Size.X + (X - 1)]));
    217     if IsValidIndex(Point(X + 0, Y - 1)) then
     217    if IsValidIndex(TPoint.Create(X + 0, Y - 1)) then
    218218      Neighbors.Add(TCell(Cells[(Y - 1) * Size.X + (X + 0)]));
    219219  end;
     
    231231begin
    232232  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));
     233  Result.Points[0] := TPoint.Create(Trunc(Pos.X - Size.X / 2), Trunc(Pos.Y - Size.Y / 2));
     234  Result.Points[1] := TPoint.Create(Trunc(Pos.X + Size.X / 2), Trunc(Pos.Y - Size.Y / 2));
     235  Result.Points[2] := TPoint.Create(Trunc(Pos.X + Size.X / 2), Trunc(Pos.Y + Size.Y / 2));
     236  Result.Points[3] := TPoint.Create(Trunc(Pos.X - Size.X / 2), Trunc(Pos.Y + Size.Y / 2));
    237237end;
    238238
    239239{ TVoronoiMap }
    240 
    241 function TVoronoiMap.GetTrianglePolygon(Pos: TPoint; Size: TPoint;
    242   Reverse: Boolean): TPointArray;
    243 begin
    244   SetLength(Result, 0);
    245 end;
    246240
    247241function CompareDistance(C1, C2: Pointer): Integer;
     
    309303    NewCell := TCell.Create;
    310304    NewCell.Map := Self;
    311     NewCell.PosPx := Point(Trunc(Random * Size.X * DefaultCellSize.X),
     305    NewCell.PosPx := TPoint.Create(Trunc(Random * Size.X * DefaultCellSize.X),
    312306      Trunc(Random * Size.Y * DefaultCellSize.Y));
    313307    SetLength(NewCell.Polygon.Points, 1);
     
    324318    NewCellDist.Cell1 := Cells[I1];
    325319    NewCellDist.Cell2 := Cells[I2];
    326     NewCellDist.Distance := Distance(NewCellDist.Cell1.PosPx, NewCellDist.Cell2.PosPx);
     320    NewCellDist.Distance := TLine.Create(NewCellDist.Cell1.PosPx, NewCellDist.Cell2.PosPx).Distance;
    327321    CellsDistance.Add(NewCellDist);
    328322  end;
     
    344338      L2 := TLine.Create(TCellsDistance(SelectedCells[J]).Cell1.PosPx,
    345339        TCellsDistance(SelectedCells[J]).Cell2.PosPx);
    346       if LineIntersect(L1, L2, Intersection) then begin
    347         if PointInRect(Intersection, L1.ToRect) and
    348         PointInRect(Intersection, L2.ToRect) then begin
     340      if TLine.LineIntersect(L1, L2, Intersection) then begin
     341        if L1.ToRect.IsPointInside(Intersection) and
     342        L2.ToRect.IsPointInside(Intersection) then begin
    349343          Intersected := True;
    350344          Break;
     
    423417    // Use whole map first for cell polygon
    424418    if Cell.Neighbors.Count > 0 then begin
    425       Polygon := TPolygon.Create(Rect(0, 0,
    426         Size.X * DefaultCellSize.X, Size.Y * DefaultCellSize.Y));
     419      Polygon := TPolygon.Create(TRect.Create(TPoint.Create(0, 0),
     420        TPoint.Create(Size.X * DefaultCellSize.X, Size.Y * DefaultCellSize.Y)));
    427421      for NeighborCell in Cell.Neighbors do begin
    428422        LinkLine := TLine.Create(Cell.PosPx, NeighborCell.PosPx);
     
    430424        MP := LinkLine.GetMiddle;
    431425        // Create half plane vector
    432         L1 := TLine.Create(MP, Point(MP.X + LinkLine.GetSize.X,
     426        L1 := TLine.Create(MP, TPoint.Create(MP.X + LinkLine.GetSize.X,
    433427          MP.Y + LinkLine.GetSize.Y));
    434428
     
    452446    else Rev := 1;
    453447  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));
     448  Result.Points[0] := TPoint.Create(Trunc(Pos.X - Size.X / 2), Trunc(Pos.Y - (Size.Y * 0.8) / 2 * Rev));
     449  Result.Points[1] := TPoint.Create(Trunc(Pos.X + Size.X / 2), Trunc(Pos.Y - (Size.Y * 0.8) / 2 * Rev));
     450  Result.Points[2] := TPoint.Create(Trunc(Pos.X), Trunc(Pos.Y + (Size.Y * 1.2) / 2 * Rev));
    457451end;
    458452
     
    481475    if Reverse then Rev := -1
    482476      else Rev := 1;
    483     NewCell.PosPx := Point(Trunc(X * DefaultCellSize.X * TriangleCellMulX),
     477    NewCell.PosPx := TPoint.Create(Trunc(X * DefaultCellSize.X * TriangleCellMulX),
    484478      Trunc((Y * DefaultCellSize.Y * TriangleCellMulY) - (0.1 * Rev * DefaultCellSize.Y)));
    485479    NewCell.Polygon := GetTrianglePolygon(NewCell.PosPx, DefaultCellSize, Reverse);
     
    494488    if Boolean(X mod 2) xor Boolean(Y mod 2) then Rev := -1
    495489      else Rev := 1;
    496     if IsValidIndex(Point(X + 1, Y + 0)) then
     490    if IsValidIndex(TPoint.Create(X + 1, Y + 0)) then
    497491      Neighbors.Add(TCell(Cells[(Y + 0) * Size.X + (X + 1)]));
    498     if IsValidIndex(Point(X + 0, Y - 1 * Rev)) then
     492    if IsValidIndex(TPoint.Create(X + 0, Y - 1 * Rev)) then
    499493      Neighbors.Add(TCell(Cells[(Y - 1 * Rev) * Size.X + (X + 0)]));
    500     if IsValidIndex(Point(X - 1, Y + 0)) then
     494    if IsValidIndex(TPoint.Create(X - 1, Y + 0)) then
    501495      Neighbors.Add(TCell(Cells[(Y + 0) * Size.X + (X - 1)]));
    502496  end;
Note: See TracChangeset for help on using the changeset viewer.