To determine if two squares overlap

Asked 2 years ago, Updated 2 years ago, 138 views

We are making a program that determines whether squares overlap each other by receiving rectangular information (x-pos, y-pos, height .

All inputs are int type, and only rectangles will be entered.

I tried to make it in my own way, but it didn't work. Can anyone briefly explain what to do?

algorithm c++ geometry overlap rectangle

2022-09-22 15:49

2 Answers

if (RectA.Left < RectB.Right && RectA.Right > RectB.Left &&
     RectA.Top > RectB.Bottom && RectA.Bottom < RectB.Top )

Or if you use Cartesion coordinate,

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

You must inspect with .

Let's divide the types For Rect A, and Rect B, if either of the following four cases is true, the two squares cannot overlap.

If (Cond1 Or Cond2 Or Cond3 Or Cond4) == true the two squares can never overlap If False, overlap.

Expressing this as a formula


2022-09-22 15:49

http://nerdparadise.com/tech/interview/intersectrectangles/ It would be helpful.


2022-09-22 15:49

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.