Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {==============================================================================]
- Explodes bx to array of TBoxes by rows and columns
- [==============================================================================}
- function BoxExplode(bx: TBox; rows, columns: Integer): TBoxArray;
- var
- r, c, w, h, ew, eh, ow, oh, i, x, y, mr, mc, p, q: Integer;
- a, b: Boolean;
- begin
- case (((rows > 0) and (columns > 0)) and True) of
- True:
- begin
- w := ((bx.X2 - bx.X1) + 1);
- h := ((bx.Y2 - bx.Y1) + 1);
- mr := rows;
- mc := columns;
- if (mr > h) then
- mr := h;
- if (mc > w) then
- mc := w;
- w := ((w div mc) - 1);
- h := ((h div mr) - 1);
- ew := (((bx.X2 - bx.X1) + 1) - ((w + 1) * mc));
- eh := (((bx.Y2 - bx.Y1) + 1) - ((h + 1) * mr));
- SetLength(Result, (mr * mc));
- y := bx.Y1;
- a := (eh > 0);
- b := (ew > 0);
- p := (mr - 1);
- q := (mc - 1);
- case ((a or b) and True) of
- True:
- case ((a and b) and True) of
- False:
- case (a and True) of
- True:
- for r := 0 to p do
- begin
- x := bx.X1;
- case (((eh > 0) and (r < eh)) and True) of
- True: oh := 1;
- False: oh := 0;
- end;
- for c := 0 to q do
- begin
- i := ((r * mc) + c);
- Result[i].X1 := x;
- Result[i].X2 := (x + w);
- Result[i].Y1 := y;
- Result[i].Y2 := (y + (h + oh));
- x := (Result[i].X2 + 1);
- end;
- y := (Result[i].Y2 + 1);
- end;
- False:
- for r := 0 to p do
- begin
- x := bx.X1;
- for c := 0 to q do
- begin
- if ((ew > 0) and (c < ew)) then
- ow := 1
- else
- ow := 0;
- i := ((r * mc) + c);
- Result[i].X1 := x;
- Result[i].X2 := (x + (w + ow));
- Result[i].Y1 := y;
- Result[i].Y2 := (y + h);
- x := (Result[i].X2 + 1);
- end;
- y := (Result[i].Y2 + 1);
- end;
- end;
- True:
- for r := 0 to p do
- begin
- x := bx.X1;
- if ((eh > 0) and (r < eh)) then
- oh := 1
- else
- oh := 0;
- for c := 0 to q do
- begin
- if ((ew > 0) and (c < ew)) then
- ow := 1
- else
- ow := 0;
- i := ((r * mc) + c);
- Result[i].X1 := x;
- Result[i].X2 := (x + (w + ow));
- Result[i].Y1 := y;
- Result[i].Y2 := (y + (h + oh));
- x := (Result[i].X2 + 1);
- end;
- y := (Result[i].Y2 + 1);
- end;
- end;
- False:
- for r := 0 to p do
- begin
- x := bx.X1;
- for c := 0 to q do
- begin
- i := ((r * mc) + c);
- Result[i].X1 := x;
- Result[i].X2 := (x + w);
- Result[i].Y1 := y;
- Result[i].Y2 := (y + h);
- x := (Result[i].X2 + 1);
- end;
- y := (Result[i].Y2 + 1);
- end;
- end;
- end;
- False: SetLength(Result, 0);
- end;
- end;
- {==============================================================================]
- Partitions bx to array of TBoxes by width and height
- [==============================================================================}
- function BoxPartition(bx: TBox; width, height: Integer): TBoxArray;
- var
- r, c, x, y, i, q, p: Integer;
- er, ec: Extended;
- a, b: Boolean;
- begin
- case (((width > 0) and (height > 0)) and True) of
- True:
- begin
- er := (((bx.Y2 - bx.Y1) + 1) / height);
- ec := (((bx.X2 - bx.X1) + 1) / width);
- r := Ceil(er);
- c := Ceil(ec);
- SetLength(Result, (r * c));
- a := (er <> r);
- b := (ec <> c);
- q := (r - 1);
- p := (c - 1);
- case ((a or b) and True) of
- True:
- case ((a and b) and True) of
- False:
- case a of
- True:
- for y := 0 to q do
- for x := 0 to p do
- begin
- i := ((y * c) + x);
- Result[i].X1 := (bx.X1 + (width * x));
- Result[i].Y1 := (bx.Y1 + (height * y));
- Result[i].X2 := (Result[i].X1 + (width - 1));
- Result[i].Y2 := (Result[i].Y1 + (height - 1));
- if (y = q) then
- Result[i].Y2 := bx.Y2;
- end;
- False:
- for y := 0 to q do
- for x := 0 to p do
- begin
- i := ((y * c) + x);
- Result[i].X1 := (bx.X1 + (width * x));
- Result[i].Y1 := (bx.Y1 + (height * y));
- Result[i].X2 := (Result[i].X1 + (width - 1));
- Result[i].Y2 := (Result[i].Y1 + (height - 1));
- if (x = p) then
- Result[i].X2 := bx.X2;
- end;
- end;
- True:
- for y := 0 to q do
- for x := 0 to p do
- begin
- i := ((y * c) + x);
- Result[i].X1 := (bx.X1 + (width * x));
- Result[i].Y1 := (bx.Y1 + (height * y));
- Result[i].X2 := (Result[i].X1 + (width - 1));
- Result[i].Y2 := (Result[i].Y1 + (height - 1));
- if (y = q) then
- Result[i].Y2 := bx.Y2;
- if (x = p) then
- Result[i].X2 := bx.X2;
- end;
- end;
- False:
- for y := 0 to q do
- for x := 0 to p do
- begin
- i := ((y * c) + x);
- Result[i].X1 := (bx.X1 + (width * x));
- Result[i].Y1 := (bx.Y1 + (height * y));
- Result[i].X2 := (Result[i].X1 + (width - 1));
- Result[i].Y2 := (Result[i].Y1 + (height - 1));
- end;
- end;
- end;
- False: SetLength(Result, 0);
- end;
- end;
- var
- bx: TBox;
- boxes: TBoxArray;
- begin
- ClearDebug;
- bx := IntToBox(0, 0, 99, 99);
- boxes := BoxExplode(bx, 2, 2);
- WriteLn(boxes);
- SetLength(boxes, 0);
- boxes := BoxPartition(bx, 50, 50);
- WriteLn(boxes);
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement