1. ## Rectangle Intersection

Hello All,

I'm doing a coding challenge in which I have to write a formula that returns the intersection of two rectangles. But before I do that, I want to attempt this mathematically. Can anyone provide resources to familiarize myself with on how to calculate the intersection of two rectangles? Any resources or information will be helpful.

Thanks

ETA: So one way of determining an intersection is to see if one corner (top left) of rectangle A is above the corner (top left) of rectangle B.
Any other way?

2. ## Re: Rectangle Intersection

Originally Posted by noork85
Hello All,

I'm doing a coding challenge in which I have to write a formula that returns the intersection of two rectangles. But before I do that, I want to attempt this mathematically. Can anyone provide resources to familiarize myself with on how to calculate the intersection of two rectangles? Any resources or information will be helpful.

Thanks

ETA: So one way of determining an intersection is to see if one corner (top left) of rectangle A is above the corner (top left) of rectangle B.
Any other way?
what exactly do you mean by "the intersection"?

a boolean that indicates the the two rectangles overlap?

two corner points of the rectangle (or line, or point) that is the intersection?

something else?

3. ## Re: Rectangle Intersection

points, coordinates.

4. ## Re: Rectangle Intersection

Originally Posted by noork85
points, coordinates.
Why are you so "mysterious"?
Will the 4 coordinates of each rectangle be givens?
If so, then:
1: do rectangles intersect?
If so:
2: calculate intersection points.

5. ## Re: Rectangle Intersection

Originally Posted by DenisB
Why are you so "mysterious"?
Will the 4 coordinates of each rectangle be givens?
If so, then:
1: do rectangles intersect?
If so:
2: calculate intersection points.
oh sorry, i wasn't trying to be mysterious lol
I'm not sure I understand the problem myself.
Yes, the coordinates will be given for the two rectangles.
How do i calculate the intersection points? i mean, if i draw it out on a graph, i can do it. But without the aid of a graph, how do I go about attempting this problem?

6. ## Re: Rectangle Intersection

Well, can take years to explain by typing back and forth...

Go hunt:

7. ## Re: Rectangle Intersection

Originally Posted by DenisB
Well, can take years to explain by typing back and forth...

Go hunt:
I was looking for something more mathematical.

8. ## Re: Rectangle Intersection

Originally Posted by noork85
I was looking for something more mathematical.
More mathematical than what?

You stated you're trying to code a "formula".
Forget it: you'll need much more than that...

As example, you'll need coding that uses the given coordinates
and does comparisons to determine if the rectangles actually
intersect each other.
Like: is the highest x-coordinate of one rectangle lower than
the lowest x-coordinate of the other rectangle...
that's take a bit of tricky programming; something like:
get rectangle A's 4 x-coordinates; u = highest
get rectangle B's 4 x-coordinates; v = lowest
is u < v?

9. ## Re: Rectangle Intersection

Hey noork85.

You can parameterize the edges of the rectangle and solve for where and when the lines intersect to get the new rectangle.

10. ## Re: Rectangle Intersection

Originally Posted by chiro
You can parameterize the edges of the rectangle and solve for where and when the lines intersect to get the new rectangle.
But there is no "new rectangle" as such; both are givens.

There will be 3 cases:
1: the given rectangles do not intersect
2: they intersect at 2 points
3: they intersect at 4 points
(not possible to intersect at 1 point or at 3 points)

Not sure, but intersecting at 6 points may be possible.

11. ## Re: Rectangle Intersection

This site kinda discusses this:

https://www.careercup.com/question?id=3870800