Changeset 168
Legend:
- Unmodified
- Added
- Removed
-
trunk/Packages/Common/UGeometry.pas
r167 r168 38 38 function Create(const Points: TPointArray): TPolygon; overload; 39 39 function Create(const Rect: TRect): TPolygon; overload; 40 function GetRect: TRect; 40 41 procedure AddPoint(const P: TPoint); 41 42 procedure Clear; … … 63 64 function SubAngle(A1, A2: Double): Double; 64 65 66 65 67 implementation 66 68 … … 291 293 Result.Points[2] := Point(Rect.Right, Rect.Bottom); 292 294 Result.Points[3] := Point(Rect.Left, Rect.Bottom); 295 end; 296 297 function TPolygon.GetRect: TRect; 298 var 299 I: Integer; 300 begin 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; 293 314 end; 294 315 -
trunk/UGame.pas
r167 r168 35 35 end; 36 36 37 TPointArray = array of TPoint;38 39 37 TTerrainType = (ttVoid, ttNormal, ttCity); 40 38 … … 51 49 Id: Integer; 52 50 PosPx: TPoint; 53 Polygon: TPo intArray;51 Polygon: TPolygon; 54 52 Terrain: TTerrainType; 55 53 PlayerId: Integer; … … 564 562 end; 565 563 566 function RectEquals(A, B: TRect): Boolean;567 begin568 Result := (A.Left = B.Left) and (A.Top = B.Top) and569 (A.Right = B.Right) and (A.Bottom = B.Bottom);570 end;571 572 function PtInRect(const Rect: TRect; Pos: TPoint): Boolean;573 begin574 Result := (Pos.X >= Rect.Left) and (Pos.Y >= Rect.Top) and575 (Pos.X <= Rect.Right) and (Pos.Y <= Rect.Bottom);576 end;577 578 function PtInPoly(const Points: array of TPoint; Pos: TPoint): Boolean;579 var580 Count, K, J : Integer;581 begin582 Result := False;583 Count := Length(Points) ;584 J := Count - 1;585 for K := 0 to Count - 1 do begin586 if ((Points[K].Y <= Pos.Y) and (Pos.Y < Points[J].Y)) or587 ((Points[J].Y <= Pos.Y) and (Pos.Y < Points[K].Y)) then588 begin589 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) then592 Result := not Result;593 end;594 J := K;595 end;596 end;597 598 function GetPolygonRect(Polygon: array of TPoint): TRect;599 var600 I: Integer;601 begin602 Result := Rect(High(Integer), High(Integer),603 Low(Integer), Low(Integer));604 for I := 0 to Length(Polygon) - 1 do605 with Polygon[I] do begin606 if X > Result.Right then607 Result.Right := X;608 if X < Result.Left then609 Result.Left := X;610 if Y > Result.Bottom then611 Result.Bottom := Y;612 if Y < Result.Top then613 Result.Top := Y;614 end;615 end;616 617 564 function HalfColor(Color: TColor): TColor; 618 565 begin … … 1571 1518 for I := 0 to Cells.Count - 1 do 1572 1519 if TCell(Cells[I]).Terrain <> ttVoid then begin 1573 if PtInPoly(TCell(Cells[I]).Polygon,Pos) then begin1520 if TCell(Cells[I]).Polygon.IsPointInside(Pos) then begin 1574 1521 Result := TCell(Cells[I]); 1575 1522 Exit; … … 1641 1588 end; 1642 1589 // Transform view 1643 SetLength(Points, Length(Cell.Polygon ));1590 SetLength(Points, Length(Cell.Polygon.Points)); 1644 1591 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]); 1646 1593 Brush.Style := bsSolid; 1647 1594 //Polygon(Points, False, 0, Length(Points)); … … 1695 1642 NewCell.PosPx := Point(X * DefaultCellSize.X, Y * DefaultCellSize.Y); 1696 1643 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; 1699 1646 Cells[Y * FSize.X + X] := NewCell; 1700 1647 end; … … 1812 1759 // This is generic algorithm to determine pixel size of entire map 1813 1760 for I := 0 to Cells.Count - 1 do begin 1814 CellRect := GetPolygonRect(TCell(Cells[I]).Polygon);1761 CellRect := TCell(Cells[I]).Polygon.GetRect; 1815 1762 if I = 0 then Result := CellRect 1816 1763 else begin … … 2034 1981 Node3 := Node.FindNode('Polygon'); 2035 1982 if Assigned(Node3) then begin 2036 SetLength(Polygon, 0);1983 Polygon.Clear; 2037 1984 Node2 := Node3.FirstChild; 2038 1985 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))); 2042 1987 Node2 := Node2.NextSibling; 2043 1988 end; … … 2068 2013 NewNode := Node.OwnerDocument.CreateElement('Polygon'); 2069 2014 Node.AppendChild(NewNode); 2070 for I := 0 to Length(Polygon ) - 1 do begin2015 for I := 0 to Length(Polygon.Points) - 1 do begin 2071 2016 NewNode2 := NewNode.OwnerDocument.CreateElement('Point'); 2072 2017 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); 2075 2020 end; 2076 2021 end; … … 2091 2036 RectA, RectB: TRect; 2092 2037 begin 2093 RectA := GetPolygonRect(Polygon);2038 RectA := Polygon.GetRect; 2094 2039 RectB := View.SourceRect; 2095 2040 Result := ((RectA.Left < RectB.Right) and (RectA.Right > RectB.Left) and -
trunk/UMap.pas
r167 r168 21 21 function IsCellsPosNeighbor(CellPos1, CellPos2: TPoint): Boolean; 22 22 procedure GetCellPosNeighbors(CellPos: TPoint; Neighbours: TCells); 23 function GetHexagonPolygon(Pos: TPoint; Size: TPoint): TPo intArray;23 function GetHexagonPolygon(Pos: TPoint; Size: TPoint): TPolygon; 24 24 public 25 25 procedure LoadFromFile(FileName: string); override; … … 33 33 TSquareMap = class(TMap) 34 34 private 35 function GetSquarePolygon(Pos: TPoint; Size: TPoint): TPo intArray;35 function GetSquarePolygon(Pos: TPoint; Size: TPoint): TPolygon; 36 36 public 37 37 function IsValidIndex(Index: TPoint): Boolean; override; … … 43 43 TTriangleMap = class(TMap) 44 44 private 45 function GetTrianglePolygon(Pos: TPoint; Size: TPoint; Reverse: Boolean): TPo intArray;45 function GetTrianglePolygon(Pos: TPoint; Size: TPoint; Reverse: Boolean): TPolygon; 46 46 public 47 47 function IsValidIndex(Index: TPoint): Boolean; override; … … 62 62 { THexMap } 63 63 64 function THexMap.GetHexagonPolygon(Pos: TPoint; Size: TPoint): TPo intArray;64 function THexMap.GetHexagonPolygon(Pos: TPoint; Size: TPoint): TPolygon; 65 65 var 66 66 Shift: TFloatPoint; 67 67 begin 68 68 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)); 76 76 end; 77 77 … … 228 228 end; 229 229 230 function TSquareMap.GetSquarePolygon(Pos: TPoint; Size: TPoint): TPo intArray;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));230 function TSquareMap.GetSquarePolygon(Pos: TPoint; Size: TPoint): TPolygon; 231 begin 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)); 237 237 end; 238 238 … … 311 311 NewCell.PosPx := Point(Trunc(Random * Size.X * DefaultCellSize.X), 312 312 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; 315 315 NewCell.Id := GetNewCellId; 316 316 Cells[Y * Size.X + X] := NewCell; … … 435 435 Polygon.CutLine(L1, Cell.PosPx); 436 436 end; 437 Cell.Polygon := Polygon .Points;438 end else SetLength(Cell.Polygon, 0);437 Cell.Polygon := Polygon; 438 end else Cell.Polygon.Clear; 439 439 end; 440 440 … … 445 445 446 446 function TTriangleMap.GetTrianglePolygon(Pos: TPoint; Size: TPoint; 447 Reverse: Boolean): TPo intArray;447 Reverse: Boolean): TPolygon; 448 448 var 449 449 Rev: Integer; … … 451 451 if Reverse then Rev := -1 452 452 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)); 457 457 end; 458 458
Note:
See TracChangeset
for help on using the changeset viewer.