Advertisement
WarPie90

Split Rect to N equal squares

Aug 9th, 2014
472
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.85 KB | None | 0 0
  1. {$I SimbaExt/SimbaExt.simba}
  2.  
  3. (* Meh *)
  4. function SquareBoxes(Box:TBox; n:Int32): TBoxArray;
  5. var
  6.   W,H,bW,bH,i,j,l,size:Int32;
  7.   wmax,hmax,sx,sy,x,y:Double;
  8. begin
  9.   W := Box.x2 - Box.x1;
  10.   H := Box.y2 - Box.y1;
  11.   wMax := Ceil(Sqrt(n * W/H));
  12.   hMax := Ceil(Sqrt(n * H/W));
  13.  
  14.   if (Trunc(wMax*H/W)*wMax < n) then sx := H/Ceil(wMax*H/W)
  15.   else sx := W/wMax;
  16.   if (Trunc(hMax*W/H)*hMax < n) then sy := W/Ceil(hMax*W/H)
  17.   else sy := H/hMax;
  18.   size := Trunc(MaxE(sx,sy));
  19.  
  20.   SetLength(Result,n);
  21.   if W > H then begin
  22.     bH := H div size;
  23.     bW := Ceil(n / bH);
  24.     sx := (W / bW) - size;
  25.     sy := (H / bH) - size;
  26.     x := Box.x1 + sx/2;
  27.     for i:=0 to bW-1 do
  28.     begin
  29.       y := Box.y1 + sy/2;
  30.       for j:=0 to bH-1 do
  31.       begin
  32.         Result[l] := [Round(x),Round(y),Round(x)+size,Round(y)+size];
  33.         inc(l);
  34.         if (l = n) then Exit();
  35.         y += size + sy;
  36.       end;
  37.       x += size + sx;
  38.     end;
  39.  
  40.   end else begin
  41.     bW := W div size;
  42.     bH := Ceil(n / bW);
  43.     sx := (W / bW) - size;
  44.     sy := (H / bH) - size;
  45.     y := Box.Y1 + sy/2;
  46.     for i:=0 to bH-1 do
  47.     begin
  48.       x := Box.X1 + sx/2;
  49.       for j:=0 to bW-1 do
  50.       begin
  51.         Result[l] := [Round(x),Round(y),Round(x)+size,Round(y)+size];
  52.         inc(l);
  53.         if (l = n) then Exit();
  54.         x += size + sx;
  55.       end;
  56.       y += size + sy;;
  57.     end;
  58.   end;
  59. end;
  60.  
  61.  
  62.  
  63. (*---| test |------------------------------------*)
  64. var
  65.   B:TBox;
  66.   Arr:TBoxArray;
  67.   BMP:TRafBitmap;
  68.   i:Int32;
  69. begin
  70.   B := [100,100,430,199];
  71.   Arr := SquareBoxes(B,23);
  72.  
  73.   BMP.Create(500,500,$FFFFFF);
  74.   BMP.SetPixels(TPAFromBox(B), $FFAA55);
  75.   for i:=0 to High(Arr) do
  76.   begin
  77.     BMP.SetPixels(TPAFromBox(Arr[i]), $22AAFF);
  78.     BMP.SetPixels(EdgeFromBox(Arr[i]), $FFFFFF);
  79.   end;
  80.   BMP.SetPixels(EdgeFromBox(B), $000000);
  81.   BMP.Debug();
  82.   BMP.Free();
  83. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement