I want to create a two dimensional layout of rectangular shapes, a grid made up of random sized cubes. The cubed should fit together and have equal padding or margin (space between). Kind of like a comic book layout, or more like the image attached.

How could I do this procedurally?

Practically, I would probably be using Python and some graphic software to render an image, but I don't know the type of algorithm (or whatnot) I would need to use to generate the randomized grid.

Really, this isn't a programming problem. You have to come up with and then decide on the method - aka 'algorithm' - by which you want the squares; implementing it is then a programming problem. To come up with the method? Start with a blank piece of paper - preferably lots of pieces of paper - get drawing, and then describe what you're doing.
–
AakashMNov 21 '12 at 9:03

3 Answers
3

Start with a grid of 1x1 cells. Pick a random spot, merge cells a random amount or till it collides with a larger rectangle.

This will get you something similar to the image you provide.

There's a bigger issues if you don't want a number of smaller cells acting as padding between your bigger cells. A comic book for example, wants to minimize the amount of dead space and have at most ~9 cells. You could pick some points and draw some lines and call those your cells.

At the end of the process, you will have an array of randomly-sized squares. Note that you'll probably want to define a minimum size (at which point no more dividing is done) and a maximum size (if the square is larger, always split regardless of the boolean value).

(I used a similar algorithm for a recursive maze generator in college) This will have the side effect of always having lines that cut through the entire image.
–
MichaelTNov 20 '12 at 14:40

@MichaelT: Definitely an issue, but the simplicity and speed of the algorithm arguably makes up for it. It really depends on the use case whether or not this is an appropriate approach to the problem. The example image appears to have been generated with this algorithm as it suffers from the issue you highlighted.
–
AntNov 20 '12 at 15:17

Determine the size and geometry of the image. If you want this to be tiled, the underlying geometry is that of a torus.

Maintain a list of the upper left hand corner of all possible shapes. Initially, this is every possible spot.

Pick a random sized rectangle (within the constraints you have decided - in the example image they are squares and have a maximum size of 4). Place this rectangle in a random upper left hand corner spot.

If the rectangle is too large (overlaps an existing allocated spot), trim the dimension of the rectangle so that it fits.

Remove all of the locations that are covered by this rectangle from the list of possible upper left hand corners.

Repeat until the upper left hand corner list is empty.

Render the resulting array in your preferred way. This is where you would introduce a margin.

If you are unfamiliar with any particular graphics library, consider using the ppm format. The key advantage is that you could write out a text file and then use one of the converters (ppmto___) to convert the image to your chosen format (ppmtogif. ppmtojpeg, ...)

Thanks. In fact I am more interested in converting it into a set of center points (on an x,y grid) and scale descriptions (as in 1 or .5, or 2 for example). I will feed these numbers into a 3d app.
–
MrwolfyNov 22 '12 at 7:09