2

Let's say I have two rectangles overlapping each other like this...

alt text http://filebox.me/files/u8atnxd34_overlap1.png

And I want them to end up like this...

alt text http://filebox.me/files/jt8ef1t44_overlap2.png

How would I calculate the position I need to add so that the rectangles move out of each other?

Note: I did find this question but it doesn't tell me how to actually move the rectangles.

Everyone's assuming I want to move the rectangle downwards, but I actually want the rectangle to move in the direction which would be the most logical. So that if the rectangle is completely to the right of the first rectangle and moves 1 pixel to the left, that it instead of moving downwards, it would move to the right.

Community
  • 1
  • 1
Dlaor
  • 2,900
  • 3
  • 17
  • 14
  • Which direction do you want them to move? – Tim Robinson Jul 30 '10 at 10:51
  • Do you have some code which you're written to try and solve this? Or do you want us to write it all for you?? – Grant Crofton Jul 30 '10 at 10:52
  • @Tim Robinson: well, basically the direction between the centers of the two rectangles. @Grant Crofton: how would I write code if I have no single idea how to do this? Checking intersection is easy but moving them is a different story. – Dlaor Jul 30 '10 at 10:54
  • Please define "the most logical direction". – Pratik Deoghare Jul 30 '10 at 11:01
  • @Dlaor I think you'd find it helpful to ignore code for the time being, and think up various combinations of rectangles and how one of them should move. In other words, do the exercise on paper before starting to think how to code it. – Tim Robinson Jul 30 '10 at 11:10
  • Do you want to minimize the area required and avoid overlap? – Pratik Deoghare Jul 30 '10 at 11:13
  • @TheMachineCharmer: Yep, I just want 2 rectangles to stop overlapping and move in the direction which would require the least distance to be traveled. – Dlaor Jul 30 '10 at 11:23
  • @Dlaor to minimize the movement you will have to move horizontally or vertically and check which one costs less. – Pratik Deoghare Jul 30 '10 at 11:49

5 Answers5

2
__________
|    ____|____
| A |    |    |
|___|____|    |
    |      B  |
    |_________|

if [ 
     (TopLeftOfA.Y + A.Height - TopLeftOfB.Y)
     < 
     (TopLeftOfA.X + A.Width  - TopLeftOfB.X)
   ]
    TopLeftOfB.Y = TopLeftOfA.Y + A.Height
else 
    TopLeftOfB.X = TopLeftOfA.X + A.Width
Pratik Deoghare
  • 35,497
  • 30
  • 100
  • 146
  • I'd accept this as the best answer but it only works if B is to the right and/or beneath of A.. – Dlaor Aug 06 '10 at 10:29
  • Solution will be similar for all the other positions :) You can do that yourself. – Pratik Deoghare Aug 06 '10 at 10:40
  • I just... can't figure it out. I can't shape the possible coordinates in my head. Could you please post the code for the other 2 sides? – Dlaor Aug 06 '10 at 17:20
0

Just move first rectangle any direction from second rectangle.

nik
  • 875
  • 2
  • 12
  • 21
0

In the exact configuration you have shown:

where a = is the foreground rectangle and b = the background triangle.

a.Top = b.Bottom; // Add +1 to have it just past the bottom.
Adrian Regan
  • 2,240
  • 13
  • 11
0

rectangle2.top = rectangle1.bottom+1 (javascript)

Lloyd Powell
  • 18,270
  • 17
  • 87
  • 123
user192344
  • 1,274
  • 6
  • 22
  • 36
0

even easier:

set the Y coördinates of the first rectangle's bottom, to the y coördinates of the second rectangle's top

Nealv
  • 6,856
  • 8
  • 58
  • 89