Advertisement
WarPie90

Untitled

Mar 22nd, 2014
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.32 KB | None | 0 0
  1. program new;
  2.  
  3. type
  4.   PTreeData = ^TTreeData;
  5.   TTreeData = record
  6.     Split: TPoint;
  7.     L, R : PTreeData;
  8.   end;
  9.  
  10.   T2DTree = Array of TTreeData;
  11.  
  12.   TInfoPoint = record
  13.     Dist:Single;
  14.     PT:  TPoint;
  15.   end;
  16.  
  17.  
  18. function TPointArray.Slice(Left, Right:Integer): TPointArray;
  19. var i:Integer;
  20. begin
  21.   SetLength(Result, Right-Left);
  22.   for i:=Left to Right-1 do
  23.     Result[i-left] := Self[i];
  24. end;
  25.  
  26.  
  27.  
  28. function PointTree(var Nodes: T2DTree; TPA:TPointArray; depth:Integer=0): PTreeData;
  29. var
  30.   hi,mid: Integer;
  31.   TMP: TTreeData;
  32. begin
  33.   hi := Length(TPA);
  34.   if (hi=0) then
  35.     Exit(nil);
  36.  
  37.   case (depth mod 2 = 0) and True of
  38.     True : SortTPAByX(TPA, False);
  39.     False: SortTPAByY(TPA, False);
  40.   end;
  41.  
  42.   mid := hi div 2;
  43.   TMP := [
  44.     {split} TPA[mid],
  45.     {left } PointTree(Nodes, TPA.Slice(0, mid),   depth+1),
  46.     {right} PointTree(Nodes, TPA.Slice(mid+1,hi), depth+1)
  47.   ];
  48.  
  49.   hi := Length(Nodes);
  50.   SetLength(Nodes, hi+1);
  51.   Nodes[hi] := TMP;
  52.  
  53.   Result := @Nodes[hi];
  54. end;
  55.  
  56.  
  57. (*
  58.  Recurse down the tree, and print element - will fail if somthing that is not nil should be nil.
  59. *)
  60. procedure PrintTree(Tree: PTreeData; Depth:Integer=-1; Side:String=' [root]');
  61. var
  62.   str: String;
  63.   i: Integer;
  64. begin
  65.   for i:=0 to depth+depth do
  66.     Str := Str + '-';
  67.   Str := Str + ' ';
  68.  
  69.   WriteLn(Str, '(', Tree^.Split.x, ', ', Tree^.Split.y, ')', side);
  70.   if Tree^.L <> nil then
  71.     PrintTree(Tree^.L, Depth+1, ' [left]');
  72.   if Tree^.R <> nil then
  73.   PrintTree(Tree^.R, Depth+1, ' [right]');
  74. end;
  75.  
  76.  
  77. (*
  78.  Recurse down the tree, will fail if somthing that is not nil should be nil.
  79. *)
  80. procedure RecurseTree(Tree: PTreeData; Depth:Integer=0);
  81. begin
  82.   if Tree^.L <> nil then
  83.     RecurseTree(Tree^.L, Depth+1);
  84.   if Tree^.R <> nil then
  85.     RecurseTree(Tree^.R, Depth+1);
  86. end;
  87.  
  88.  
  89.  
  90. (*
  91.  Simple random tpa, with some extra parameters compared to what SCAR offers.
  92. *)
  93. function RandomTPA(Amount:Integer; MinX,MinY,MaxX,MaxY:Integer): TPointArray; //StdCall;
  94. var i:Integer;
  95. begin
  96.   SetLength(Result, Amount);
  97.   for i:=0 to Amount-1 do
  98.     Result[i] := Point(RandomRange(MinX, MaxX), RandomRange(MinY, MaxY));
  99. end;
  100.  
  101.  
  102. var
  103.   TPA:TPointArray;
  104.   PTree: PTreeData;
  105.   Tree: T2DTree;
  106.   t,i:Integer;
  107. begin
  108.   TPA := RandomTPA(5000,0,0,500,500);
  109.   PTree := PointTree(Tree, TPA);
  110.  
  111.   RecurseTree(PTree);
  112. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement