How to check if two rectangles overlap with each other in C++

C++
x
20
#include <iostream>
using namespace std;
struct Point {
int x, y;
};
// Returns true if two rectangles (l1, r1) and (l2, r2) overlap
bool doOverlap(Point l1, Point r1, Point l2, Point r2)
{
// If one rectangle is on left side of other
if (l1.x > r2.x || l2.x > r1.x)
return false;
// If one rectangle is above other
if (l1.y < r2.y || l2.y < r1.y)
return false;
return true;
}
🤖 Code Explanation
This code checks to see if two rectangles overlap. It does this by checking if any of the corners of one rectangle are inside the other rectangle.

More problems solved in C++




















