10

I have two rectangles, the red rectangle (can move) and the blue rectangle. Both have: x, y, width, height.

How can I say in a programming language such as Java when there is a collision between the blue and the red rectangle?

Example of collision

ZygD
  • 22,092
  • 39
  • 79
  • 102
mikelplhts
  • 1,181
  • 3
  • 11
  • 32

5 Answers5

25
if (RectA.X1 < RectB.X2 && RectA.X2 > RectB.X1 &&
    RectA.Y1 < RectB.Y2 && RectA.Y2 > RectB.Y1) 

Say you have Rect A, and Rect B. Proof is by contradiction. Any one of four conditions guarantees that no overlap can exist:

Cond1. If A's left edge is to the right of the B's right edge, - then A is Totally to right Of B
Cond2. If A's right edge is to the left of the B's left edge, - then A is Totally to left Of B
Cond3. If A's top edge is below B's bottom edge, - then A is Totally below B
Cond4. If A's bottom edge is above B's top edge, - then A is Totally above B
So condition for Non-Overlap is

Cond1 Or Cond2 Or Cond3 Or Cond4

Therefore, a sufficient condition for Overlap is the opposite (De Morgan)

Not Cond1 And Not Cond2 And Not Cond3 And Not Cond4 This is equivalent to:

A's Left Edge to left of B's right edge, and
A's right edge to right of B's left edge, and
A's top above B's bottom, and
A's bottom below B's Top

Note 1: It is fairly obvious this same principle can be extended to any number of dimensions. Note 2: It should also be fairly obvious to count overlaps of just one pixel, change the < and/or the > on that boundary to a <= or a >=.

If you are having a hard time visualizing why it works, I made an example page at silentmatt.com/intersection.html where you can drag rectangles around and see the comparisons.

BufBills
  • 8,005
  • 12
  • 48
  • 90
  • 2
    @dopatraman BufBills' if-condition is just a pseudo code illustrating a solution. `RectA.X1` stands for the left edge of rectangle A, and `RectA.X1 < RectB.X2` means "A's left edge is to the left of B's right edge". How you implement it in Java is your decision. – SergiyKolesnikov May 09 '17 at 08:50
  • OP explicitly asked for an implementation in Java. At the very least the accepted answer should be runnable code. – dopatraman May 10 '17 at 17:49
  • 1
    @dopatraman If you decide to model rectangles with a class that has X1, X2, Y1, Y2 attributes for the coordinates of the rectangle's edges and if you use variables RectA and RectB to refer two rectangle objects, then you can use the if condition by BufBills as it stands. It is valid Java code and it is exactly what OP was asking for. Otherwise, the OP would not accepted the answer. – SergiyKolesnikov May 23 '17 at 12:26
  • 1
    This doesn't seem to work for rotated rectangles. On paper I have a large rectangle rotated by 45 degrees and a very small rectangle close to the large rectangle's edge line, it seems to meet all the conditions but isn't enclosed in the rectangle or intersecting it. – Enigma22134 Jun 30 '17 at 01:22
7

In java , to detect if two when two rectangles collide, you can use intersects() method

Sample Code:

Rectangle r1 = new Rectangle(x1,y1,x2,y2);
Rectangle r2 = new Rectangle(x1,y1,x2,y2);
if(r1.intersects(r2))
{
    //what to happen when collision occurs goes here
}
Pruthvi Raj
  • 3,016
  • 2
  • 22
  • 36
  • i mean, if you do interview for this question. no one expect you to use library to solve this algorithm question. Though, it's good to know there is a library for rectangle :D – BufBills Jun 24 '15 at 19:14
  • 4
    I approve of this answer, simply because it is not specified in the question that he is looking for an algorithm to solve this problem, simply finding a solution. – Sh4d0wsPlyr Jun 24 '15 at 19:16
  • @BufBills , Oh sorry I never noticed the algorithm tag until u mentioned, shall I take this answer down? – Pruthvi Raj Jun 24 '15 at 19:16
3
bool isIntersect(
  int Ax, int Ay, int Aw, int Ah,
  int Bx, int By, int Bw, int Bh)
{
  return
    Bx + Bw > Ax &&
    By + Bh > Ay &&
    Ax + Aw > Bx &&
    Ay + Ah > By;
}
Denis Petrov
  • 851
  • 7
  • 16
1

You have to check both the intersection along the x-axis and along the y-axis. If any of them is missing, there is no collision between rectangles.

Code for 1-D:

boolean overlaps(double point1, double length1, double point2, double length2)
{
    double highestStartPoint = Math.max(point1, point2);
    double lowestEndPoint = Math.min(point1 + length1, point2 + length2);

    return highestStartPoint < lowestEndPoint;
}

You have to call it for both x and y:

boolean collision(double x1, double x2, double y1, double y2, double width1, double width2, double height1, double height2)
{
    return overlaps(x1, width1, x2, width2) && overlaps (y1, height1, y2, height2);
}
Miljen Mikic
  • 14,765
  • 8
  • 58
  • 66
-1
  1. Calculate 4 points of first rectangle. Note them as pointA, pointB, pointC, pointD.
  2. calculate 4 points of second rectangle. Note them as pointE, F, G H.

  3. Iterate point E,F,G,H (1) if any of these is within area that A,B,C,D enclosed. return collision. (2) otherwise no collision.

For 3.(1) algorithm. you need something like this.

Xe >= Xa && Xc <= Xb. && Yc >= Yc && Ye <=Yc

BufBills
  • 8,005
  • 12
  • 48
  • 90