0
$\begingroup$

Say 2 axis aligned rectangles, A and B, are positioned at the same point. I want to move B in direction D so that A and B are no longer overlapping. How far do I need to move B in direction D?

Right now I'm just doing an approximation, but trying to figure out a closed form solution.

1 Answers 1

0

So you have two identical rectangles that occupy the same coordinates.

For the sake of simplicity, let's assume the coordinates of the rectangles are $A(0, 0)$, $B(a, 0)$, $C(a, b)$ and $D(0,b)$. Then if $a

Of course, if $b

Now, if you cannot choose direction $D$, then the most you would have to move the second rectangle is the largest of $(a, b)$.

  • 0
    Sorry if it wasn't clear, but the rectangles are not identical. They are just at the same position (their centers are at the same coordinate).2017-02-24
  • 0
    The answer can be adapted slightly. If $A$ has a width of $x$ units and a length of $y$ units, and $B$ has a width of $w$ units and a length of $z$ units, then the shortest movement required is the smallest of ${w, x, y, z}$.2017-02-24
  • 0
    If A has a width of 500 and a height of 5, and B has a width of 5 and a height of 500, you would have to move B more than 5 units to stop it from overlapping, regardless of the required direction.2017-02-24
  • 0
    You're right, of course. And since you're reckoning from the center of the rectangle, that changes my thinking slightly as well. I would guess if $A$ is $w_1$ units wide and $B$ is $w_2$ units wide, then you would have to move the center of $B$ $\frac{w_1+w_2}{2}$ units in that direction. If the sum of the lengths divided by 2 is shorter, prefer that direction instead.2017-02-24
  • 0
    Yes, but I don't choose the direction. The direction could be (sqrt(2)/2, -sqrt(2)/2) or (1,0) or (-1/2, sqrt(3)/2), any angle.2017-02-24
  • 0
    So, given an angle $\theta$, find the distance from the center of each rectangle to the edge along that angle. Sum the two distances and divide by 2. That should do it.2017-02-24
  • 0
    Doesn't seem to work. Testing in the simplest case where direction is (0, 1). A has width 3 height 8. B has width 5 height 2. A distance to its edge is 4. B distance to its edge is 1. (4 + 1) / 2 = 2.5. The actual distance B has to move is 5. Removing the division by 2 makes it work in this case (because it turns into the formula you had a couple comments above in the cardinal direction case) but doesn't work in a case where direction is something like (sqrt(2)/2, sqrt(2)/2)2017-02-24
  • 0
    I think the best you can do with an angle is to move horizontally to clear the edges of the rectangles, then vertically to match the angle.2017-02-24