0

I have a big rectangle of size 12*12. Now I have 6 rectangles already placed on the floor of that rectangle. I know the center coordinate of that pre-placed module. Now I have few another 14 rectangles to place upon that floor of that rectangle. How to do so?enter image description here

here all my pre placed block those having center coordinate as say (2,5),(5,7),(9,2),(7,8),(11,9),(3,11).

Now how could I place 14 another rectangle in this floor so that it would not over lap with any preplaced block. I would like to code in MATLAB..but what approach should I follow?

user38375
  • 31
  • 6
  • 2
    Search for "two dimensional bin packing". It's a very well-known problem. – Ben Voigt May 03 '15 at 04:34
  • 2
    look here [bin packing and shape nesting](http://stackoverflow.com/a/18693107/2521214) for some ideas how to deal with this, also what are your solution criteria? can you move all the rectangles? what you want to minimize area, gaps,... what is min distance between objects, ... need any special things like clear path to each object ... and more ... – Spektre May 03 '15 at 07:24

3 Answers3

2

If a nice even placement is important, I suggest you look up simulated force-based graph layout. In this problem, you'll use simulated forces pushing the rectangles apart and also away from the border rectangle according to Coulomb's law. The initial configuration is randomly selected. You'll want to give the rectangles mass proportional to their area, I think. You don't have any spring forces due to edges, which makes it easier. The iteration to solve the differential equations of motion will be easy in Matlab. Or there may well be a toolkit to do it for you. Animations of these algorithms are fun.

Unfortunately with constrained problems like this, the fixed rectangles can form barriers that prevent the moving rectangles from getting to a non-overlapping solution. (Think of the case where the fixed rectangles are in a line down the middle and all the moving ones get "trapped" on one side or the other. The same thing happens in graph layout if some nodes have fixed locations.) There are various strategies for overcoming these bad cases. One is to start with no fixed objects at all, let the moving rectangles come to an equilibrium, then add the fixed ones one at a time, largest first, allowing the system regain equilibrium each time. Another, simpler one is just to start from different random initial conditions until you find one that works. There are also approaches related to simulated annealing, which is too big a topic to discuss here.

Gene
  • 46,253
  • 4
  • 58
  • 96
1

Here is a function to check overlap for two rectangles. you could loop it to check for more number of rectangles based on @Dov's idea.

For two rectangles Ri, i = 1,2, with centers (xi,yi) and half-lengths of their sides ai,bi > 0 (assuming that the sides are aligned with the coordinate axes).

enter image description here

Here is my implementation based on above equation:

In my code i've taken xcPosition and ycPosition as the center position of the rectangle.

Also length and breadth are the magnitude of sides of the rectangle.

function [ overLap, pivalue ] = checkOverlap( xcPosition1,ycPosition1,xcPosition2,ycPosition2,length1,breadth1,length2,breadth2 )

pix = max((xcPosition2 - xcPosition1 -(length1/2)-(length2/2)),(xcPosition1 -xcPosition2 -(length2/2)-(length1/2)));
piy = max((ycPosition2 - ycPosition1 -(breadth1/2)-(breadth2/2)),(ycPosition1 -ycPosition2 -(breadth2/2)-(breadth1/2))); 
pivalue = max(pix, piy); 

if (pivalue < 0)
    overLap = 1;  %// Overlap exists 
else
    overLap = 0;  %// No overlap
end
end

You could also use the pivalue to know the degree of overlap or Non-overlap

The Pseudo-code for looping would be something like this:

for i = 1 : 14
    for j = 1 : i-1 + 6 already placed parts
        %// check for overlap using the above function here
        %// place the part if there is no overlap
    end
end
Santhan Salai
  • 3,888
  • 19
  • 29
  • Hi, is x1 and x2 in eqn..is center or bottom left? So, I would take each of 30 variable rectangle and shall check whether it is overlapping with any 10 preplaced rectangles or not? right? So in next iteration, while placing the 2nd variable rectangle, I have to check the overlap conditions with 10 preplaced rectangles+ one already placed variable rectangle? – user38375 May 03 '15 at 04:28
  • @user38375 u are right. go head and let me know when you complete it – Santhan Salai May 03 '15 at 04:35
0

With such a small number, put each rectangle in a list. Each time you add a new rectangle, make sure the new one does not overlap with any of the existing ones.

This is O(n^2), so if you plan to increase to 10^3 or more rectangles you will need a better algorithm, but otherwise you're fine.

Now if your problem specifies that you might not be able to fit them all, then you will have to backtrack and keep trying different places. That is an N! problem, but if you have a lot of open space, many solutions will be possible.

Dov
  • 8,000
  • 8
  • 46
  • 75
  • Hi, How this overlap condition will be checked? can you suggest a better algorithm for large no of input – user38375 May 03 '15 at 03:21
  • Either your rectangle contains other one, or it contains yours. think of each case. If your x > their x + width, there is no interaction. Similarly if rect.x + rect.width < other.x then there is no chance for overlap. The better algorithm is to sort in the x or y, so that you can check only the ones in the right neighborhood – Dov May 03 '15 at 03:23
  • @user38375, how large is your actual number? – Santhan Salai May 03 '15 at 03:30
  • @ Santhan Preplaced block could be 10...and other variable block could be upto 30 – user38375 May 03 '15 at 03:42
  • @user38375, 30 is a very small number. check my answer and it should work fine. – Santhan Salai May 03 '15 at 03:55