Advertisement
WarPie90

voronoi diagram

Jun 10th, 2022 (edited)
1,899
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 0.76 KB | None | 0 0
  1. program voronoi;
  2. {$I SRL/OSR.simba}
  3.  
  4. function GenerateVoronoi(TPA: TPointArray): TMufasaBitmap;
  5. var
  6.   w,h,x,y,i,d: Int32;
  7.   B: TBox;
  8.   p: TPoint;
  9.   Tree: TSlackTree;
  10. begin
  11.   B := TPA.Bounds();
  12.   W := B.x2+1;
  13.   H := B.y2+1;
  14.   Tree.Init(TPA);
  15.  
  16.   Result.Init();
  17.   Result.SetSize(W, H);
  18.  
  19.   for x:=0 to W-1 do
  20.     for y:=0 to H-1 do
  21.     begin
  22.       p := Tree.Nearest(Point(x,y));
  23.       d := Sqr(p.x-x) + Sqr(p.y-y);
  24.       i := UInt32((p.y * $0f0f1f1f) xor p.x) and $FFFFFF;
  25.       Result.SetPixel(x,y, i);
  26.     end;
  27.  
  28.   for i:=0 to High(TPA) do
  29.     Result.DrawCircleFilled(TPA[i], 2, 0);
  30. end;
  31.  
  32. var
  33.   TPA: TPointArray;
  34.   im: TMufasaBitmap;
  35. begin
  36.   TPA := RandomTPA(100, Box(0,0,500,500));
  37.  
  38.   im := GenerateVoronoi(TPA);
  39.   im.Show();
  40.   im.Free();
  41. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement