1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
#include <iostream>
#include <algorithm>
using namespace std;
struct Rectangle
{
double x1, y1, x2, y2;
Rectangle( double x1_, double y1_, double x2_, double y2_ ) : x1( x1_ ), y1( y1_ ), x2( x2_ ), y2( y2_ )
{
if ( x1 > x2 ) swap( x1, x2 ); // Force storage as bottom-left / top-right
if ( y1 > y2 ) swap( y1, y2 );
}
};
bool overlay( Rectangle r, Rectangle s ) // FWIW, take as true if there is a non-zero area of intersection
{
if ( r.x1 >= s.x2 || r.x2 <= s.x1 || r.y1 >= s.y2 || r.y2 <= s.y1 ) return false;
return true;
}
int main()
{
Rectangle A{ 0, 0, 1, 1 }, B{ 0.5, 0.5, 0.6, 0.8 };
cout << boolalpha << overlay( A, B ) <<'\n';
}
|