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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
#include <iostream>
#include <sstream>
#include <algorithm>
using namespace std;
class Rectangle
{
double x1, x2, y1, y2;
void check() { if ( x1 > x2 || y1 > y2 ) { x1 = x2 = y1 = y2 = 0.0; } }
public:
Rectangle( double xbl = 0.0, double ybl = 0.0, double width = 1.0, double height = 1.0 )
{
x1 = xbl; x2 = x1 + width; y1 = ybl; y2 = y1 + height;
check();
}
double area() { return ( x2 - x1 ) * ( y2 - y1 ); }
Rectangle operator * ( const Rectangle & other )
{
double left = max( this->x1, other.x1 );
double right = min( this->x2, other.x2 );
double bottom = max( this->y1, other.y1 );
double top = min( this->y2, other.y2 );
return Rectangle( left, bottom, right - left, top - bottom );
}
friend istream & operator >> ( istream &in, Rectangle &rectangle )
{
double xbl, ybl, width, height;
in >> xbl >> ybl >> width >> height;
rectangle = Rectangle( xbl, ybl, width, height );
return in;
}
};
int main()
{
istringstream in( "0 0 5 3\n"
"4 1 3 3\n" );
Rectangle a, b;
in >> a >> b;
cout << "Overlap area is " << ( a * b ).area() << '\n';
}
| |