Message Boards Message Boards

0
|
4132 Views
|
3 Replies
|
2 Total Likes
View groups...
Share
Share this post:

any idea how to put this list of rectangles?

Posted 10 years ago

How is everybody?, Now I am writing to ask your help to do this. I have this problem, given the following list:

rectangles = {{25,22}, {40,28}, {25,22}, {31,23},{31,23}, {40,28}, {40,28}, {25,22}, {25,22}, {31,23}, {31,23}, {31,23}, {40 28}};

where the first element of each ordered pair is the length of a rectangle and the second element of each ordered pair is the same width rectangle, I need to find a way to place them within a rectangle of 130 long by 100 width, so:

colocation of rectangles

marked with red rectangles are the {25,22} in green the rectangles {40,28} and finally {31.23} rectangles in yellow, I should mention that should be placed according to the list, I've tried resolving a cycle "while" but also images overlap there are many unnecessary spaces, any idea is welcome, greetings to all.

POSTED BY: Luis Ledesma
3 Replies
Posted 10 years ago

there any way to do what you ask using the functions, fold, map and apply ? or other functions of mathematica?, hopefully help me finish my problem

POSTED BY: Luis Ledesma
Posted 10 years ago

Here is an example function which divides the list of rectangles into rows, the total width of each row being <= 130.

In[1]:= rectangles={{25,22},{40,28},{25,22},{31,23},{31,23},{40,28},{40,28},{25,22},{25,22},{31,23},{31,23},{31,23},{40,28}};

splitIntoRows[rows_, row_, rects_] :=  If[rects == {}, Append[rows, row],(*Done*)
(*Otherwise continue constructing a row*)
If[First[Total[row]] + First[First[rects]] <= 130,
splitIntoRows[rows, Append[row, First[rects]], Rest[rects]],(*Then we can add another rectangle to the row*)
splitIntoRows[Append[rows, row], {First[rects]}, Rest[rects]]]];(*Else we must start a new row*)

rows = splitIntoRows[{}, {First[rectangles]}, Rest[rectangles]]

Out[2]= {
{{25, 22}, {40, 28}, {25, 22}, {31, 23}},
{{31, 23}, {40, 28}, {40, 28}},
{{25, 22}, {25, 22}, {31, 23}, {31, 23}},
{{31, 23}, {40, 28}}}

Study that function. Try to understand the reasoning. See how the parameters control the function and construct the result. Make certain that it is correct for all possible input. Note: It silently fails to warn you if a single rectangle is wider than your box of if you give it an empty list of rectangles. It might miss other border cases that I haven't thought of, but I tried to keep this simple so you would be able to understand the strategy rather than wading through all the possible error checks that might otherwise be included. This seems a little like a homework problem for a first functional programming class.

Now could you consider constructing a function, perhaps using Piecewise or some other way of determining the minimal position for a rectangle, which would take the heights of the upper edge of a previous row of rectangles and correctly dealing with a starting state of an empty box, which gives the height of the upper edge of each new rectangle in a row?

I do not know what options you have for the positioning of the next rectangle in the box, but if I just do left-to-right and then bottom-to-top with each as closely packed to the lower left as possible then I don't think I see those rectangles fitting into your box.

POSTED BY: Bill Simpson
Posted 10 years ago

Or a completely different approach.

In[1]:= rectangles={{25,22},{40,28},{25,22},{31,23},{31,23},{40,28},{40,28},{25,22},{25,22},{31,23},{31,23},{31,23},{40,28}};
splitIntoRows[rows_, {}] := rows;
splitIntoRows[rows_,{Longest[row__]/;First[Total[{row}]]<=130,rects___}]:=splitIntoRows[Append[rows,{row}],{rects}];
rows = splitIntoRows[{}, rectangles]

Out[4]= {
{{25, 22}, {40, 28}, {25, 22}, {31, 23}},
{{31, 23}, {40, 28}, {40, 28}},
{{25, 22}, {25, 22}, {31, 23}, {31, 23}},
{{31, 23}, {40, 28}}}

Same study and testing.

POSTED BY: Bill Simpson
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract