Advertisement
WarPie90

Recursive clustering of points

Sep 16th, 2024 (edited)
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 0.98 KB | None | 0 0
  1. function SplitTPAEx(const pts: TPointArray; w,h: single): T2DPointArray; cdecl;
  2. var
  3.   lo,hi: Int32;
  4.   sqx,sqy,sqxy: single;
  5.   procedure ConnectCluster(p: TPoint);
  6.   var i,top: Int32;
  7.   begin
  8.     top := hi;
  9.     for i:=hi downto lo+1 do
  10.       //if (Abs(p.x-pts[i].x) <= w) and (Abs(p.y-pts[i].y) <= h) then
  11.       if (Sqr(p.x-list[i].x)*sqy)+(Sqr(p.y-list[i].y)*sqx) <= sqxy then
  12.       begin
  13.         Swap(pts[i], pts[hi]); // move towards end
  14.         Dec(hi);               // reduce upper bound
  15.       end;
  16.  
  17.     for i:=hi+1 to top do
  18.       ConnectCluster(pts[i]);
  19.   end;
  20.  
  21. var top,n: Int32;
  22. begin
  23.   sqx := Sqr(w);
  24.   sqy := Sqr(h);
  25.   sqxy := sqx*sqy;
  26.        
  27.   lo := 0;
  28.   hi := High(pts);
  29.   while lo <= hi do
  30.   begin
  31.     top := hi;
  32.     ConnectCluster(pts[lo]);
  33.  
  34.     // add the connected range
  35.     n := Length(Result);
  36.     SetLength(Result, n+1);
  37.     Result[n] := Copy(pts, hi+1, (top-hi));
  38.     Append(Result[n], pts[lo]);
  39.  
  40.     Inc(lo); // increase lower bound
  41.   end;
  42. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement