tags:

views:

173

answers:

3

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)
  +----+---+    |
  |        |    |
  +--------+----+
+6  A: 

Split the rectangle into two. Recurse.

Axel Gneiting
+1 Beautifully concise.
Jamie Wong
-1: You'll run out of memory. 1/2**inf != 0
amphetamachine
@amphetamachine: The end condition here is so obvious it doesn't really need to be spelled out - after all, you can't split '1' in half without having either '0' or something that can't be represented as an integer.
Anon.
@amphetamachine Agree with Anon. The end condition is obvious. The poster even specified the rectangle was specified by integer coordinates. This likely means that all the internal rectangles should also have integer coordinates.
Jamie Wong
Err.. This does not work. It misses some configurations. Something like the chase bank logo, but completed to form a rectangle. http://bp1.blogger.com/_YdwmIhUxTJY/R6PknAooqlI/AAAAAAAAAco/k6jOGsQOT48/s400/chase-logo.jpg. Sorry don't have a better description, but I suppose you get the idea: A square in the middle with each side of the square extended to touch exactly one side of the main rectangle.
Moron
@Moron I don't get your point. The chase bank logo does not start as a rectangle anyway.
ohho
@Horace: Think of a rectangle with four corners snipped off which looks like the chase bank logo. The white square in the middle + the 4 blue (if completed to form rectangles) cannot be formed with this solution. Sorry, hard to do geometry with text.
Moron
@Moron "a rectangle with four corners snipped off" is not a rectangle, right?
ohho
@Horace: Chase logo = rectangle with four corners snipped off. Take chase logo, get back the rectangle by adding back the four corners that were snipped off. If you read my comment: "Something like chase logo, but *completed* to form a rectangle". Is it clearer now?
Moron
@Moron I added an appendix with the logo I can think of. is that what you mean?
ohho
@Horace Ho You should have a look into kd-trees: http://en.wikipedia.org/wiki/Kd_tree What You're trying to achieve is basically a kd-tree with k=2
Dave
@Horace: Exactly! That is what I meant.
Moron
@Dave: No. Kd-trees basically give the same solution as this one. See my comment on that answer.
Moron
Yep, they give the same solution as this one and this one is the one that the op is looking for ;)
Dave
@Dave: This answer was accepted before @Moron's objection; it is indeed incorrect. Besides, just because OP accepted an answer doesn't mean we should stop looking for better (or correct) answers
BlueRaja - Danny Pflughoeft
+1  A: 

Choose a random point p on one edge and divide the rectangle there with a line to the opposite edge. You can then recurse on both halves, stopping the recursion randomly or at a specified limit.

Mandelbrot
+4  A: 

It's a bit odd to ask this question without specifying what conditions under which the rectangles are split.

However, I suspect that what you're looking for is a kd-tree. The kd-tree is a binary tree in which nodes are split with two resulting child nodes based on a condition. http://en.wikipedia.org/wiki/Kd-tree

There's also a quad-tree which can be slightly simpler to implement. It involves splitting nodes into 4 equal-sized children. Each child is recursively split this way until some stop condition. http://en.wikipedia.org/wiki/Quadtree

+1 for kd-trees
Dave
kd-trees have the same problem as the accepted solution. Consider the root node. If you split on x you have a vertical line dividing the rectangle. If you split on y, you have a horizontal line dividing the rectangle. In fact, this solution is not much different from the accepted one.
Moron
@Moron I don't see where the problem with this solution is. Please post an answer describing the problem with the suggested/accepted solutions and provide Your own one, omitting the problem. I fail to see the problem, because given the question I think the op never would want to create something that looks like the case You described. So kd-trees are sufficient for his purposes.
Dave
@Dave: The OP never said anything about not wanting rectangles like in the appendix (but I suppose we will know soon). In any case, this kd-tree solution is same as the accepted solution. You are in effect, splitting the rectangle into two and recursing. Sorry, unless I have an answer, I won't post one, hence the comments...
Moron
Every sane person knows that both answers mean the same. Since the op accepted this kind of solution, he seems to be satisfied with its limits. Stop complaining. My compliments to the choice of Your user name.
Dave
@Dave. Oh please... Anyway I will stop having this discussion with you. Pointless and a waste of time.
Moron