Janilabo

Untitled

Aug 13th, 2013
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 4.35 KB | None | 0 0
  1. {==============================================================================]
  2. @action: Sorts TPA using Heatmap methods for analyzing/detecting "hot" & "cold" points in TPA.
  3.          Hot points = a lot of neighbour points | Cold points = less/none neighbour points.
  4. @note: Supports custom order with TSortOrder!
  5. @contributors: Janilabo, slacky
  6. [==============================================================================}
  7. procedure TPASortByHeatmapEx(var TPA: TPointArray; order: TSortOrder); callconv
  8.   procedure ShellSort(var arr: TPointArray; heatmap: T2DExtendedArray; area: TBox; order: TSortOrder);
  9.   var
  10.     x, a, b, l: Integer;
  11.     t: TPoint;
  12.   begin
  13.     l := Length(arr);
  14.     if (l > 1) then
  15.     begin
  16.       x := 0;
  17.       while (x < (l div 3)) do
  18.         x := ((x * 3) + 1);
  19.       case order of
  20.         so_HighToLow:
  21.         if (l > 2) then
  22.         begin
  23.           while (x >= 1) do
  24.           begin
  25.             for a := x to (l - 1) do
  26.             begin
  27.               b := a;
  28.               while ((b >= x) and (heatmap[(arr[b].X - area.X1)][(arr[b].Y - area.Y1)] > heatmap[(arr[(b - x)].X - area.X1)][(arr[(b - x)].Y - area.Y1)])) do
  29.               begin
  30.                 t := arr[b];
  31.                 arr[b] := arr[(b - x)];
  32.                 arr[(b - x)] := t;
  33.                 b := (b - x);
  34.               end;
  35.             end;
  36.             x := (x div 3);
  37.           end;
  38.         end else
  39.           if (heatmap[(arr[0].X - area.X1)][(arr[0].Y - area.Y1)] < heatmap[(arr[1].X - area.X1)][(arr[1].Y - area.Y1)]) then
  40.           begin
  41.             t := arr[0];
  42.             arr[0] := arr[1];
  43.             arr[1] := t;
  44.           end;
  45.         so_LowToHigh:
  46.         if (l > 2) then
  47.         begin
  48.           while (x >= 1) do
  49.           begin
  50.             for a := x to (l - 1) do
  51.             begin
  52.               b := a;
  53.               while ((b >= x) and (heatmap[(arr[b].X - area.X1)][(arr[b].Y - area.Y1)] < heatmap[(arr[(b - x)].X - area.X1)][(arr[(b - x)].Y - area.Y1)])) do
  54.               begin
  55.                 t := arr[b];
  56.                 arr[b] := arr[(b - x)];
  57.                 arr[(b - x)] := t;
  58.                 b := (b - x);
  59.               end;
  60.             end;
  61.             x := (x div 3);
  62.           end;
  63.         end else
  64.           if (heatmap[(arr[0].X - area.X1)][(arr[0].Y - area.Y1)] > heatmap[(arr[1].X - area.X1)][(arr[1].Y - area.Y1)]) then
  65.           begin
  66.             t := arr[0];
  67.             arr[0] := arr[1];
  68.             arr[1] := t;
  69.           end;
  70.       end;
  71.     end;
  72.   end;
  73. var
  74.   x, y, l, i, s: Integer;
  75.   c: T2DIntegerArray;
  76.   p: T2DExtendedArray;
  77.   t: TPointArray;
  78.   b: TBox;
  79.   d: Extended;
  80. begin
  81.   l := Length(TPA);
  82.   if (l > 1) then
  83.   begin
  84.     b := TPABounds(TPA);
  85.     SetLength(p, ((b.X2 - b.X1) + 1));
  86.     SetLength(c, ((b.X2 - b.X1) + 1));
  87.     for x := 0 to (b.X2 - b.X1) do
  88.     begin
  89.       SetLength(p[x], ((b.Y2 - b.Y1) + 1));
  90.       SetLength(c[x], ((b.Y2 - b.Y1) + 1));
  91.       for y := 0 to (b.Y2 - b.Y1) do
  92.       begin
  93.         p[x][y] := 0.0;
  94.         c[x][y] := 0;
  95.       end;
  96.     end;
  97.     SetLength(t, l);
  98.     s := 0;
  99.     for i := 0 to (l - 1) do
  100.     begin
  101.       if (c[(TPA[i].X - b.X1)][(TPA[i].Y - b.Y1)] = 0) then
  102.       begin
  103.         t[s] := TPA[i];
  104.         s := (s + 1);
  105.         p[(TPA[i].X - b.X1)][(TPA[i].Y - b.Y1)] := 1;
  106.       end;
  107.       Inc(c[(TPA[i].X - b.X1)][(TPA[i].Y - b.Y1)]);
  108.     end;
  109.     SetLength(t, s);
  110.     for i := 0 to (s - 1) do
  111.       for x := ((t[i].X - b.X1) - 5) to ((t[i].X - b.X1) + 5) do
  112.         for y := ((t[i].Y - b.Y1) - 5) to ((t[i].Y - b.Y1) + 5) do
  113.           if ((x >= 0) and (y >= 0) and (x <= (b.X2 - b.X1)) and (y <= (b.Y2 - b.Y1))) then
  114.             if (c[x][y] > 0) then
  115.               if ((x <> (t[i].X - b.X1)) or (y <> (t[i].Y - b.Y1))) then
  116.               begin
  117.                 d := Sqrt(Sqr((t[i].X - b.X1) - x) + Sqr((t[i].Y - b.Y1) - y));
  118.                 if (d <= 5.5) then
  119.                   p[(t[i].X - b.X1)][(t[i].Y - b.Y1)] := (p[(t[i].X - b.X1)][(t[i].Y - b.Y1)] + (0.5 / d));
  120.               end else
  121.                 p[(t[i].X - b.X1)][(t[i].Y - b.Y1)] := (p[(t[i].X - b.X1)][(t[i].Y - b.Y1)] + 1);
  122.     ShellSort(t, p, b, order);
  123.     l := 0;
  124.     for x := 0 to (s - 1) do
  125.     begin
  126.       for y := 0 to (c[(t[x].X - b.X1)][(t[x].Y - b.Y1)] - 1) do
  127.         TPA[(l + y)] := t[x];
  128.       l := (l + c[(t[x].X - b.X1)][(t[x].Y - b.Y1)]);
  129.     end;
  130.   end;
  131. end;
Advertisement
Add Comment
Please, Sign In to add comment