Advertisement
Janilabo

ExplodeBox Snippet

Dec 14th, 2013
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 2.17 KB | None | 0 0
  1. const
  2.   BITMAP_WIDTH = 500;
  3.   BITMAP_HEIGHT = 500;
  4.  
  5.   MAIN_BOX_X1 = 100;
  6.   MAIN_BOX_Y1 = 200;
  7.   MAIN_BOX_X2 = 400;
  8.   MAIN_BOX_Y2 = 300;
  9.  
  10.   EXPLOSION_ROWS = 10;
  11.   EXPLOSION_COLUMNS = 30;
  12.  
  13.   RANDOM_COLORS = True;
  14.  
  15. function ExplodeBox(bx: TBox; rows, columns: integer): TBoxArray;
  16. var
  17.   r, c, w, h, ew, eh, ow, oh, i, x, y: integer;
  18. begin
  19.   if ((rows > 0) and (columns > 0) and (bx.X1 <= bx.X2) and (bx.Y1 <= bx.Y2)) then
  20.   begin
  21.     w := ((bx.X2 - bx.X1) + 1);
  22.     h := ((bx.Y2 - bx.Y1) + 1);
  23.     if (rows > h) then
  24.       rows := h;
  25.     if (columns > w) then
  26.       columns := w;
  27.     w := (w div columns);
  28.     h := (h div rows);
  29.     ew := (((bx.X2 - bx.X1) + 1) - (w * columns));
  30.     eh := (((bx.Y2 - bx.Y1) + 1) - (h * rows));
  31.     SetLength(Result, (rows * columns));
  32.     y := bx.Y1;
  33.     for r := 0 to (rows - 1) do
  34.     begin
  35.       x := bx.X1;
  36.       if ((eh > 0) and (r < eh)) then
  37.         oh := 1
  38.       else
  39.         oh := 0;
  40.       for c := 0 to (columns - 1) do
  41.       begin
  42.         if ((ew > 0) and (c < ew)) then
  43.           ow := 1
  44.         else
  45.           ow := 0;
  46.         i := ((r * columns) + c);
  47.         Result[i].X1 := x;
  48.         Result[i].X2 := (x + (w - 1) + ow);
  49.         Result[i].Y1 := y;
  50.         Result[i].Y2 := (y + (h - 1) + oh);
  51.         x := (Result[i].X2 + 1);
  52.       end;
  53.       y := (Result[i].Y2 + 1);
  54.     end;
  55.   end else
  56.     SetLength(Result, 0);
  57. end;
  58.  
  59. var
  60.   b: TBoxArray;
  61.   h, i, m, bmp: integer;
  62.   p, e: TPointArray;
  63.  
  64. begin
  65.   bmp := CreateBitmap(BITMAP_WIDTH, BITMAP_HEIGHT);
  66.   DisplayDebugImgWindow(BITMAP_WIDTH, BITMAP_HEIGHT);
  67.   DrawBitmapDebugImg(bmp);
  68.   b := ExplodeBox(IntToBox(MAIN_BOX_X1, MAIN_BOX_Y1, MAIN_BOX_X2, MAIN_BOX_Y2), EXPLOSION_ROWS, EXPLOSION_COLUMNS);
  69.   h := High(b);
  70.   if RANDOM_COLORS then
  71.     m := 1;
  72.   case m of
  73.     0:
  74.     for i := 0 to h do
  75.     begin
  76.       p := TPAFromBox(b[i]);
  77.       FindTPAEdgesWrap(p, e);
  78.       DrawTPABitmap(bmp, e, 255);
  79.       SetLength(e, 0);
  80.     end;
  81.     1:
  82.     for i := 0 to h do
  83.     begin
  84.       p := TPAFromBox(b[i]);
  85.       FindTPAEdgesWrap(p, e);
  86.       DrawTPABitmap(bmp, e, Random(16777216));
  87.       SetLength(e, 0);
  88.     end;
  89.   end;
  90.   DrawBitmapDebugImg(bmp);
  91. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement