Advertisement
WarPie90

Recursive simple clustering of 2d data based on distance.

Aug 28th, 2023
1,847
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 0.85 KB | None | 0 0
  1. function split(list: TPointArray; w,h: Int32): T2DPointArray;
  2. var
  3.   lo,hi: Int32;
  4.  
  5.   function extract_connections_of(p: TPoint): TPointArray;
  6.   var i: Int32;
  7.   begin
  8.     for i:=hi downto lo+1 do
  9.       // the distance parameter
  10.       if (Abs(p.x-list[i].x) <= w) and (Abs(p.y-list[i].y) <= h) then
  11.       begin
  12.         Result += list[i];
  13.         Swap(list[i], list[hi]); //move towards end
  14.         Dec(hi);                 //and ever look at it again
  15.       end;
  16.   end;
  17.  
  18.   function recurse_connections(p: TPoint): TPointArray;
  19.   var i: Int32;
  20.   begin
  21.     Result := extract_connections_of(p);
  22.     for i:=0 to High(result) do
  23.       Result += recurse_connections(result[i]); //extend
  24.   end;
  25. begin
  26.   lo := 0;
  27.   hi := High(list);
  28.   while lo <= hi do
  29.   begin
  30.     Result += recurse_connections(list[lo]) + [list[lo]]; //append
  31.     inc(lo);
  32.   end;
  33. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement