Janilabo

Untitled

Sep 10th, 2013
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 1.62 KB | None | 0 0
  1. {==============================================================================]
  2.   Explodes bx to array of TBoxes by rows and columns
  3. [==============================================================================}
  4. function BoxExplode(bx: TBox; rows, columns: Integer): TBoxArray;
  5. var
  6.   r, c, w, h, ew, eh, ow, oh, i, x, y: Integer;
  7. begin
  8.   if ((rows > 0) and (columns > 0) and (bx.X1 <= bx.X2) and (bx.Y1 <= bx.Y2)) then
  9.   begin
  10.     w := ((bx.X2 - bx.X1) + 1);
  11.     h := ((bx.Y2 - bx.Y1) + 1);
  12.     if (rows > h) then
  13.       rows := h;
  14.     if (columns > w) then
  15.       columns := w;
  16.     w := (w div columns);
  17.     h := (h div rows);
  18.     ew := (((bx.X2 - bx.X1) + 1) - (w * columns));
  19.     eh := (((bx.Y2 - bx.Y1) + 1) - (h * rows));
  20.     SetLength(Result, (rows * columns));
  21.     y := bx.Y1;
  22.     for r := 0 to (rows - 1) do
  23.     begin
  24.       x := bx.X1;
  25.       if ((eh > 0) and (r < eh)) then
  26.         oh := 1
  27.       else
  28.         oh := 0;
  29.       for c := 0 to (columns - 1) do
  30.       begin
  31.         if ((ew > 0) and (c < ew)) then
  32.           ow := 1
  33.         else
  34.           ow := 0;
  35.         i := ((r * columns) + c);
  36.         Result[i].X1 := x;
  37.         Result[i].X2 := (x + (w - 1) + ow);
  38.         Result[i].Y1 := y;
  39.         Result[i].Y2 := (y + (h - 1) + oh);
  40.         x := (Result[i].X2 + 1);
  41.       end;
  42.       y := (Result[i].Y2 + 1);
  43.     end;
  44.   end else
  45.     SetLength(Result, 0);
  46. end;
  47.  
  48. var
  49.   bx: TBox;
  50.   bxs: TBoxArray;
  51.   h, i: Integer;
  52.  
  53. begin
  54.   bx := Box(10, 10, 70, 70);
  55.   bxs := BoxExplode(bx, 5, 10);
  56.   h := High(bxs);
  57.   for i := 0 to h do
  58.     WriteLn('bxs[' + IntToStr(i) + '] = ' + BoxToStr(bxs[i]));
  59. end.
Advertisement
Add Comment
Please, Sign In to add comment