# how to code this?

Hi,

I want to write an application that given the size of a rectangle from a user, fill the rectangle randomly with 3 different sized squares and 2 different size rectangles.

What I need to know is how to represent this in code, how do I draw the rectangle then add one shape then another until it is all filled up without any spaces?

I dont want the actual code, but some idea of how to model it in code.

any thoughts?

see http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=progsoftgen&qid=20312622 for what it is I am trying to end up with.
LVL 1
###### Who is Participating?

Commented:

The easiest would be to use a multi-dimensional array to represent the large rectangle

Dim lRec(4,6) as Integer

then fill the array with numbers representing the filling shapes as follows, use different numbers to represent different shapes and zero for blanks spaces

0 0 0 0 0 0 0
0 0 0 2 2 0 0
0 0 0 2 2 0 0
1 1 1 2 2 0 0
1 1 1 2 2 0 0

may be slow cycling through the array depending on your machine bu it should work

I have have used this same technique on a 3D space and it worked

0

Commented:
assuming you mean EXACTLY fill the given rectangle with exactly 3 square (of different sizes) and 2 rectangles (of different sizes), here's the algorithm:

make one square have one side equal to the shorter side of the rectangle.  This will leave you with a small rectangle.  Make the next square have one side equal to the short side of that rectangle.

... if you can't take it from there, I can't help you.

0

Author Commented:
I am working on the logic side of things thats cool, but how do I actually in code say: put one square here then next to that put another square then another then put another square"

do you see what I mean?

how do I actually represent the shapes in code and then fill an object (the rectangle) with the shapes?
0

Commented:
just use the shape objects.  go to the toolbar & look for the thing that's a colored triangle and a colored circle on top of it with a colored rectangle in the background.
0

Commented:
sorry, it's a yellow circle on a red square on a blue rectangle
0

Commented:
sorry, it's a yellow circle on a red square on a blue rectangle
0

Commented:
Look oat recursive routines...

That way, you can set rules for how many objects / types are allowed, and let the routine work against those rules to fill the available space.   You may like to use two sepraate functions (one for squares/rectangles, and one for triangles) for simplicity in coding.

e.g. Rule1: largest piece no larger than 30% of area, Rule 2: smallest piece no less than 10%... etc.  (optionally excluding the last piece from the rule (which will leave a rectabgle or triangle uncovered) - or notifying that the 'remaining' uncovered area is xxx%)

To do the drawing, I would prefer to use the line()function, (make sure they are closed!) and flood-fill later - unless there was a specific need to move the pieces after they were drawn... trying to rotate triangles would complicate the matter too much.

0

Author Commented:
I think this is going in the right direction, but what I need to work out is how to decide which shape to put where.

Say I start from the bottom left I choose shape1 and I fill the rest of the row with different shapes.  Then I go to the next row, because some of them are different sizes, say the fist one I put down was 300 x 300 - a 400 x 400 wont fit so I need to move it one to the left and cut a bit off which is fine, but what happens if the next one I put in doesn't fit?  this picture should explain it a little better:

http://www.gingerpeople.f9.co.uk/rectangles.bmp

or maybe it wont :(
0

Author Commented:
this is what I am doing.

cheers
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.