Find if a rectangle passes through another in cartesian plane

139 Views Asked by At

problem

I want to know how to prove or find out if the red big rectangle passes through one of these small rectangles i have the coordinates of the big rectangle (the top left) and i have it's width and length.

i have the coordinates of the small square (top left) and i have it's width and length.

if it passes through it , i want to know the width and length of the intersected part and it's coordinates.

would the conditions that the big one passes through one of these small ones change from one to another according to the way of intersection.

What are all the conditions that it passes through one rectangle.

i hope you guys understand me ,because i want to turn this into a computer program so i won't have this draw to find out from it.

1

There are 1 best solutions below

1
On

Given two rectangles $r_a$ and $r_b$. These consist of the following points:

  • $(x_{0_a},y_{0_a})$ is the upper left point of $r_a$
  • $(x_{1_a},y_{1_a})$ is the lower right point of $r_a$
  • $(x_{0_b},y_{0_b})$ is the upper left point of $r_b$
  • $(x_{1_b},y_{1_b})$ is the lower right point of $r_b$

(Note: The lower right point may be computed by adding the (width,height) of the rectangle to the upper left point. Similarly, you can compute the (width,height) of the rectangle by subtracting the upper left from the lower right point)

These two rectangles intersect when

$\neg((x_{1_a} < x_{0_b}) \lor (y_{1_a} < y_{0_b}) \lor (x_{0_a} > x_{1_b}) \lor (y_{0_a} > y_{1_b}))$

You mentioned that you want to implement this in a program. The pseudocode for an intersection check may be

bool intersect(Rectangle a, Rectangle b) {
    if (a.x1 < b.x0) return false;
    if (a.y1 < b.y0) return false;
    if (a.x0 > b.x1) return false;
    if (a.y0 > b.y1) return false;
    return true;
}

In order to compute the new rectangle $r_c$ which is the intersection between two others, you can define

  • $x_{0_c} = max(x_{0_a}, x_{0_b})$
  • $y_{0_c} = max(y_{0_a}, y_{0_b})$
  • $x_{1_c} = min(x_{1_a}, x_{1_b})$
  • $y_{1_c} = min(x_{1_a}, x_{1_b})$

Or again, as pseudocode:

void intersect(Rectangle a, Rectangle b, Rectangle c)
{
    c.x0 = max(a.x0, b.x0);
    c.y0 = max(a.y0, b.y0);
    c.x1 = min(a.x1, b.x1);
    c.y1 = min(a.y1, b.y1);
}