Two intersections of rectangles

I have two rectangles, each of which has 4 values:

Left position X , upper position Y , width W and height H :

 X1, Y1, H1, W1 X2, Y2, H2, W2 

Rectangles do not rotate, for example:

 +--------------------> X axis | | (X,Y) (X+W, Y) | +--------------+ | | | | | | | | | | +--------------+ v (X, Y+H) (X+W,Y+H) Y axis 

What is the best solution to determine if the intersection of two rectangles is empty or not?

+51
math algorithm shapes pseudocode
Nov 15 '12 at 1:37
source share
7 answers
 if (X1+W1<X2 or X2+W2<X1 or Y1+H1<Y2 or Y2+H2<Y1): Intersection = Empty else: Intersection = Not Empty 

If you have four coordinates - ((X,Y),(A,B)) and ((X1,Y1),(A1,B1)) - and not two plus width and height, it will look like this:

 if (A<X1 or A1<X or B<Y1 or B1<Y): Intersection = Empty else: Intersection = Not Empty 
+86
Nov 15 :
source share

The best example ..

 /** * Check if two rectangles collide * x_1, y_1, width_1, and height_1 define the boundaries of the first rectangle * x_2, y_2, width_2, and height_2 define the boundaries of the second rectangle */ boolean rectangle_collision(float x_1, float y_1, float width_1, float height_1, float x_2, float y_2, float width_2, float height_2) { return !(x_1 > x_2+width_2 || x_1+width_1 < x_2 || y_1 > y_2+height_2 || y_1+height_1 < y_2); } 

as well as another way to see the link and register it yourself ..

+4
May 26 '14 at 11:50
source share

If two rectangles are the same size, you can do:

 if (abs (x1 - x2) < w && abs (y1 - y2) < h) { // overlaps } 
+2
Jul 07 '17 at 6:25
source share

I just tried the c program and wrote below.

 #include<stdio.h> int check(int i,int j,int i1,int j1, int a, int b,int a1,int b1){ return (\ (((i>a) && (i<a1)) && ((j>b)&&(j<b1))) ||\ (((a>i) && (a<i1)) && ((b>j)&&(b<j1))) ||\ (((i1>a) && (i1<a1)) && ((j1>b)&&(j1<b1))) ||\ (((a1>i) && (a1<i1)) && ((b1>j)&&(b1<j1)))\ ); } int main(){ printf("intersection test:(0,0,100,100),(10,0,1000,1000) :is %s\n",check(0,0,100,100,10,0,1000,1000)?"intersecting":"Not intersecting"); printf("intersection test:(0,0,100,100),(101,101,1000,1000) :is %s\n",check(0,0,100,100,101,101,1000,1000)?"intersecting":"Not intersecting"); return 0; } 
0
Sept. 15 '13 at 14:57
source share

Using the coordinate system, where (0, 0) is the upper left corner.

I thought about it in terms of vertical and horizontal sliding windows and come up with this:

(B. Bottom> A.Top & B.Top <A..Bottom) && (B.Right> A.Left & B.Left <A.Right)

This is what you get if you apply the DeMorgans law to the following:

Not (B. Bottom <A.Top || B.Top> A. Bottom || B.Right <A.Left || B.Left> A.Right)

  • B above A
  • B below A
  • B left from A
  • B is entitled from A
0
Jul 22 '15 at 1:02
source share

If the coordinates of the rectangles of the lower left corner and the upper right corner are: (r1x1, r1y1), (r1x2, r1y2) for rect1 and
(r2x1, r2y1), (r2x2, r2y2) for rect2
(Python code below)

  intersect = False for x in [r1x1, r1x2]: if (r2x1<=x<=r2x2): for y in [r1y1, r1y2]: if (r2y1<=y<=r2y2): intersect = True return intersect else: for Y in [r2y1, r2y2]: if (r1y1<=Y<=r1y2): intersect = True return intersect else: for X in [r2x1, r2x2]: if (r1x1<=X<=r1x2): for y in [r2y1, r2y2]: if (r1y1<=y<=r1y2): intersect = True return intersect else: for Y in [r1y1, r1y2]: if (r2y1<=Y<=r2y2): intersect = True return intersect return intersect 
0
Nov 23 '17 at 17:34 on
source share

if (X1 <= X2 + W2 && X2 <= X1 + W1 & Y1> = Y2-H2 & Y2> = Y1 + H1) Cross

Question Y is at the top.

Note. This solution only works if the rectangle is aligned with the X / Y axes.

-one
Oct 02 '16 at 23:48
source share



All Articles