Advertisement
WarPie90

Untitled

May 24th, 2016
438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 3.53 KB | None | 0 0
  1. {$R+}
  2. type
  3.   TBox = record x1,y1,x2,y2:Int32; end;
  4.   TPoint = record x,y:Int32; end;
  5.   TPointArray = array of TPoint;
  6.  
  7.   TQuadNode = record
  8.     Bounds: TBox;
  9.     Points: array of TPoint;
  10.     NorthWest, NorthEast: Int32;  //Holds indices of the pos in the Tree-Array
  11.     SouthWest, SouthEast: Int32;
  12.   end;
  13.  
  14.   TQuadTree = record
  15.     NODE_CAPACITY: UInt8;
  16.     Data: array of TQuadNode;
  17.   end;
  18.  
  19.  
  20.  
  21. function TQuadTree.AddBox(x1,y1,x2,y2:Int32): Int32;
  22. begin
  23.   Result := Length(self.Data);
  24.   SetLength(self.Data, Result+1);
  25.   with self.Data[Result] do
  26.   begin
  27.     Bounds.x1 := x1;
  28.     Bounds.y1 := y1;
  29.     Bounds.x2 := x2;
  30.     Bounds.y2 := y2;
  31.  
  32.     NorthWest := -1;
  33.     NorthEast := -1;
  34.     SouthWest := -1;
  35.     SouthEast := -1;
  36.   end;
  37. end;
  38.  
  39.  
  40. procedure TQuadTree.Init(area:TBox; capacity:Int32=2);
  41. begin
  42.   self.AddBox(area.x1, area.y1, area.x2, area.y2);
  43.   self.NODE_CAPACITY := capacity;
  44. end;
  45.  
  46.  
  47. // Insert a point into the QuadTree
  48. function TQuadTree.Insert(p:TPoint; idx:Int32 = 0): Boolean;
  49. begin
  50.   try
  51.     WriteLn('Range check: (index:' + ToString(idx) + ', size:' + ToString(Length(self.data))+')');
  52.     //outside this box?
  53.       if (self.Data[idx].Bounds.x1 > p.x) or (self.Data[idx].Bounds.x2 < p.x) or (self.Data[idx].Bounds.y1 > p.y) or (self.Data[idx].Bounds.y2 < p.y) then
  54.         Exit(False);
  55.  
  56.       //can we fit it here?
  57.       if (Length(self.Data[idx].Points) < NODE_CAPACITY) then
  58.       begin
  59.         self.Data[idx].Points += p;
  60.         Exit(True);
  61.       end;
  62.  
  63.       // Otherwise, subdivide and then add the point to whichever node will accept it
  64.       if (self.Data[idx].northWest = -1) then
  65.       begin
  66.         WriteLn('Dividing! (',self.Data[idx].northWest, ', ', self.Data[idx].northEast, ', ', self.Data[idx].southWest, ', ', self.Data[idx].southEast, ')');
  67.         self.SubDivide(idx);
  68.       end;
  69.       WriteLn(self.Data[idx].northWest, ', ', self.Data[idx].northEast, ', ', self.Data[idx].southWest, ', ', self.Data[idx].southEast);
  70.  
  71.       if self.Insert(p, self.Data[idx].northWest) then Exit(True);
  72.       if self.Insert(p, self.Data[idx].northEast) then Exit(True);
  73.       if self.Insert(p, self.Data[idx].southWest) then Exit(True);
  74.       if self.Insert(p, self.Data[idx].southEast) then Exit(True);
  75.   except
  76.     // This should _never_ happen!
  77.     RaiseException('Range check: (index:' + ToString(idx) + ', size:' + ToString(Length(self.data))+')');
  78.   end;
  79. end;
  80.  
  81.  
  82. // Insert a point into the QuadTree
  83. procedure TQuadTree.SubDivide(idx:Int32);
  84. var
  85.   mid:TPoint;
  86. begin
  87.   WriteLn('Divide got idx: ', idx);
  88.  
  89.   mid.X := (self.Data[idx].Bounds.X1 + self.Data[idx].Bounds.X2) shr 1;
  90.   mid.Y := (self.Data[idx].Bounds.Y1 + self.Data[idx].Bounds.Y2) shr 1;
  91.  
  92.   self.Data[idx].northWest := self.AddBox(self.Data[idx].bounds.x1, self.Data[idx].bounds.y1, mid.x, mid.y);
  93.   self.Data[idx].northEast := self.AddBox(mid.x, self.Data[idx].bounds.y1, self.Data[idx].bounds.x2, mid.y);
  94.   self.Data[idx].southWest := self.AddBox(self.Data[idx].bounds.x1, mid.y, mid.x, self.Data[idx].bounds.y2);
  95.   self.Data[idx].southEast := self.AddBox(mid.x, mid.y, self.Data[idx].bounds.x2, self.Data[idx].bounds.y2);
  96. end;
  97.  
  98.  
  99. function TPAFromBox(B:TBox): TPointArray;
  100. var x,y:Int32;
  101. begin
  102.   for y:=B.x1 to B.y2 do
  103.     for x:=B.x1 to B.x2 do
  104.       Result += TPoint([x,y]);
  105. end;
  106.  
  107.  
  108. var
  109.   Tree : TQuadTree;
  110.   TPA: TPointArray;
  111.   i:Int32;
  112. begin
  113.   TPA := TPAFromBox([0,0,10,10]);
  114.   Tree.Init([0,0,11,11]);
  115.  
  116.   for i:=0 to High(TPA) do
  117.   begin
  118.     Tree.Insert(TPA[i]);
  119.     WriteLn('----------------------');
  120.   end;
  121. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement