Advertisement
Janilabo

Untitled

Aug 8th, 2014
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 6.37 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, mr, mc, p, q: Integer;
  7.   a, b: Boolean;
  8. begin
  9.   case (((rows > 0) and (columns > 0)) and True) of
  10.     True:
  11.     begin
  12.       w := ((bx.X2 - bx.X1) + 1);
  13.       h := ((bx.Y2 - bx.Y1) + 1);
  14.       mr := rows;
  15.       mc := columns;
  16.       if (mr > h) then
  17.         mr := h;
  18.       if (mc > w) then
  19.         mc := w;
  20.       w := ((w div mc) - 1);
  21.       h := ((h div mr) - 1);
  22.       ew := (((bx.X2 - bx.X1) + 1) - ((w + 1) * mc));
  23.       eh := (((bx.Y2 - bx.Y1) + 1) - ((h + 1) * mr));
  24.       SetLength(Result, (mr * mc));
  25.       y := bx.Y1;
  26.       a := (eh > 0);
  27.       b := (ew > 0);
  28.       p := (mr - 1);
  29.       q := (mc - 1);
  30.       case ((a or b) and True) of
  31.         True:
  32.         case ((a and b) and True) of
  33.           False:
  34.           case (a and True) of
  35.             True:
  36.             for r := 0 to p do
  37.             begin
  38.               x := bx.X1;
  39.               case (((eh > 0) and (r < eh)) and True) of
  40.                 True: oh := 1;
  41.                 False: oh := 0;
  42.               end;
  43.               for c := 0 to q do
  44.               begin
  45.                 i := ((r * mc) + c);
  46.                 Result[i].X1 := x;
  47.                 Result[i].X2 := (x + w);
  48.                 Result[i].Y1 := y;
  49.                 Result[i].Y2 := (y + (h + oh));
  50.                 x := (Result[i].X2 + 1);
  51.               end;
  52.               y := (Result[i].Y2 + 1);
  53.             end;
  54.             False:
  55.             for r := 0 to p do
  56.             begin
  57.               x := bx.X1;
  58.               for c := 0 to q do
  59.               begin
  60.                 if ((ew > 0) and (c < ew)) then
  61.                   ow := 1
  62.                 else
  63.                   ow := 0;
  64.                 i := ((r * mc) + c);
  65.                 Result[i].X1 := x;
  66.                 Result[i].X2 := (x + (w + ow));
  67.                 Result[i].Y1 := y;
  68.                 Result[i].Y2 := (y + h);
  69.                 x := (Result[i].X2 + 1);
  70.               end;
  71.               y := (Result[i].Y2 + 1);
  72.             end;
  73.           end;
  74.           True:
  75.           for r := 0 to p do
  76.           begin
  77.             x := bx.X1;
  78.             if ((eh > 0) and (r < eh)) then
  79.               oh := 1
  80.             else
  81.               oh := 0;
  82.             for c := 0 to q do
  83.             begin
  84.               if ((ew > 0) and (c < ew)) then
  85.                 ow := 1
  86.               else
  87.                 ow := 0;
  88.               i := ((r * mc) + c);
  89.               Result[i].X1 := x;
  90.               Result[i].X2 := (x + (w + ow));
  91.               Result[i].Y1 := y;
  92.               Result[i].Y2 := (y + (h + oh));
  93.               x := (Result[i].X2 + 1);
  94.             end;
  95.             y := (Result[i].Y2 + 1);
  96.           end;
  97.         end;
  98.         False:
  99.         for r := 0 to p do
  100.         begin
  101.           x := bx.X1;
  102.           for c := 0 to q do
  103.           begin
  104.             i := ((r * mc) + c);
  105.             Result[i].X1 := x;
  106.             Result[i].X2 := (x + w);
  107.             Result[i].Y1 := y;
  108.             Result[i].Y2 := (y + h);
  109.             x := (Result[i].X2 + 1);
  110.           end;
  111.           y := (Result[i].Y2 + 1);
  112.         end;
  113.       end;
  114.     end;
  115.     False: SetLength(Result, 0);
  116.   end;
  117. end;
  118.  
  119. {==============================================================================]
  120.   Partitions bx to array of TBoxes by width and height
  121. [==============================================================================}
  122. function BoxPartition(bx: TBox; width, height: Integer): TBoxArray;
  123. var
  124.   r, c, x, y, i, q, p: Integer;
  125.   er, ec: Extended;
  126.   a, b: Boolean;
  127. begin
  128.   case (((width > 0) and (height > 0)) and True) of
  129.     True:
  130.     begin
  131.       er := (((bx.Y2 - bx.Y1) + 1) / height);
  132.       ec := (((bx.X2 - bx.X1) + 1) / width);
  133.       r := Ceil(er);
  134.       c := Ceil(ec);
  135.       SetLength(Result, (r * c));
  136.       a := (er <> r);
  137.       b := (ec <> c);
  138.       q := (r - 1);
  139.       p := (c - 1);
  140.       case ((a or b) and True) of
  141.         True:
  142.         case ((a and b) and True) of
  143.           False:
  144.           case a of
  145.             True:
  146.             for y := 0 to q do
  147.               for x := 0 to p do
  148.               begin
  149.                 i := ((y * c) + x);
  150.                 Result[i].X1 := (bx.X1 + (width * x));
  151.                 Result[i].Y1 := (bx.Y1 + (height * y));
  152.                 Result[i].X2 := (Result[i].X1 + (width - 1));
  153.                 Result[i].Y2 := (Result[i].Y1 + (height - 1));
  154.                 if (y = q) then
  155.                   Result[i].Y2 := bx.Y2;
  156.               end;
  157.             False:
  158.             for y := 0 to q do
  159.               for x := 0 to p do
  160.               begin
  161.                 i := ((y * c) + x);
  162.                 Result[i].X1 := (bx.X1 + (width * x));
  163.                 Result[i].Y1 := (bx.Y1 + (height * y));
  164.                 Result[i].X2 := (Result[i].X1 + (width - 1));
  165.                 Result[i].Y2 := (Result[i].Y1 + (height - 1));
  166.                 if (x = p) then
  167.                   Result[i].X2 := bx.X2;
  168.               end;
  169.           end;
  170.           True:
  171.           for y := 0 to q do
  172.             for x := 0 to p do
  173.             begin
  174.               i := ((y * c) + x);
  175.               Result[i].X1 := (bx.X1 + (width * x));
  176.               Result[i].Y1 := (bx.Y1 + (height * y));
  177.               Result[i].X2 := (Result[i].X1 + (width - 1));
  178.               Result[i].Y2 := (Result[i].Y1 + (height - 1));
  179.               if (y = q) then
  180.                 Result[i].Y2 := bx.Y2;
  181.               if (x = p) then
  182.                 Result[i].X2 := bx.X2;
  183.             end;
  184.         end;
  185.         False:
  186.         for y := 0 to q do
  187.           for x := 0 to p do
  188.           begin
  189.             i := ((y * c) + x);
  190.             Result[i].X1 := (bx.X1 + (width * x));
  191.             Result[i].Y1 := (bx.Y1 + (height * y));
  192.             Result[i].X2 := (Result[i].X1 + (width - 1));
  193.             Result[i].Y2 := (Result[i].Y1 + (height - 1));
  194.           end;
  195.       end;
  196.     end;
  197.     False: SetLength(Result, 0);
  198.   end;
  199. end;
  200.  
  201. var
  202.   bx: TBox;
  203.   boxes: TBoxArray;
  204.  
  205. begin
  206.   ClearDebug;
  207.   bx := IntToBox(0, 0, 99, 99);
  208.   boxes := BoxExplode(bx, 2, 2);
  209.   WriteLn(boxes);
  210.   SetLength(boxes, 0);
  211.   boxes := BoxPartition(bx, 50, 50);
  212.   WriteLn(boxes);
  213. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement