Advertisement
WarPie90

Untitled

Mar 22nd, 2014
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.04 KB | None | 0 0
  1. type
  2.   PTreeData = ^TTreeData;
  3.   TTreeData = record
  4.     Split: TPoint;
  5.     L, R : PTreeData;
  6.   end;
  7.  
  8.   T2DTree = Array of TTreeData;
  9.  
  10.   TInfoPoint = record
  11.     Dist:Single;
  12.     PT:  TPoint;
  13.   end;
  14.  
  15.  
  16. function TPointArray.Slice(Left, Right:Integer): TPointArray;
  17. var i:Integer;
  18. begin
  19.   SetLength(Result, Right-Left);
  20.   for i:=Left to Right-1 do
  21.     Result[i-left] := Self[i];
  22. end;
  23.  
  24.  
  25. //Based on KDTree base, but only to work with 2D trees.
  26. function PointTree(var Nodes: T2DTree; TPA:TPointArray; depth:Integer=0): PTreeData;
  27. var
  28.   hi,mid: Integer;
  29.   TMP: TTreeData;
  30. begin
  31.   hi := Length(TPA);
  32.   if (hi=0) then
  33.     Exit(nil);
  34.  
  35.   case (depth mod 2 = 0) and True of
  36.     True : SortTPAByX(TPA, False);
  37.     False: SortTPAByY(TPA, False);
  38.   end;
  39.  
  40.   mid := hi div 2;
  41.   TMP := [
  42.     {split} TPA[mid],
  43.     {left } PointTree(Nodes, TPA.Slice(0, mid),   depth+1),
  44.     {right} PointTree(Nodes, TPA.Slice(mid+1,hi), depth+1)
  45.   ];
  46.  
  47.   hi := Length(Nodes);
  48.   SetLength(Nodes, hi+1);
  49.   Nodes[hi] := TMP;
  50.  
  51.   Result := @Nodes[hi];
  52. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement