Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Algorithm to divide a rectangle to smaller retangles?

Tags:

c

algorithm

What's the algorithm to divide a rectangle (c struct with 4 ints) to a random number of smaller rectangles (return a list of structs)? Even better if the max and min dimension of the smaller rectangles can be controlled by a parameter.

e.g.

+----------+            +-------+--+
|          |            |       |  |
|          |            |       |  |
|          |    -->     |---+---+--| (good)
|          |            |   |      |
|          |            +---+      |
|          |            |   |      |
+----------+            +---+------+

smaller shapes should be 4-sided, the following is not good:

+----------+            +-------+--+
|          |            |       |  |
|          |            |       |  |
|          |    -->     |---+---+--| (not good)
|          |            |          |
|          |            +---+      |
|          |            |   |      |
+----------+            +---+------+

Thanks!

Appendix: (rectangle for Moron's discussion)

  +----+--------+
  |    |        |
  |    +---+----+
  |    |   |    | (rectangle-chase)
  +----+---+    |
  |        |    |
  +--------+----+
like image 293
ohho Avatar asked Jul 08 '10 02:07

ohho


People also ask

How do you divide rectangles?

We could divide our rectangle into two equal parts, or halves. Then, we can halve each half again until we have four equal parts. If we take two-halves and halve them again, we get quarters. So, this is the rectangle that is divided into quarters.

How do you divide a rectangle into 5 rectangles?

Let's say the rectangle measures 10 by 3 inches. Draw four lines perpendicular to the long dimension at two-inch increments. You will end up with five equal rectangles which measure two by three inches. Done!

Can you divide any rectangle into squares?

For example: A rectangle with a width of 360m and a height of 592m cannot be divided into equal squares. A rectangle with a width of 100m and height of 300m can be divided into squares with sides 10m. 10 across and 30 down.

How do you divide a rectangle into 6 equal parts?

Note: In order to solve this type of question the key is to understand the meaning of dividing the rectangle into 6 equal parts. It is important to divide it only w.r.t its length of its breadth. If we divide it w.r.t to the length and breadth at once, we obtain 36 equal rectangles.


1 Answers

Split the rectangle into two. Recurse.

like image 112
Axel Gneiting Avatar answered Sep 24 '22 02:09

Axel Gneiting