#ifndef _RECTANGLE_HPP_
#define _RECTANGLE_HPP_
/*****************************************************************************/
#include "point.hpp"
/*****************************************************************************/
class Rectangle
{
Point TopLeft, TopRight,BottomLeft,BottomRight;
double Width, Height;
public:
Rectangle(Point _TopLeft, Point _BottomRight)
{
if ((_TopLeft.m_x > _BottomRight.m_x) || (_TopLeft.m_y > _BottomRight.m_y))
throw std::logic_error("Invalid rectangle coordinates");
}
Rectangle(Point TopLeft, double Width, double Height)
{
if(Width <=0|| Height <=0)
throw std::logic_error("Invalid rectangle coordinates");
}
Point getTopLeft()
{
return TopLeft;
}
Point getTopRight()
{
return TopRight;
}
Point getBottomLeft()
{
return BottomLeft;
}
Point getBottomRight()
{
return BottomRight;
}
double getWidth()
{
return Width;
}
double getHeight()
{
return Height;
}
double getPerimeter()
{
return (Width+Height)*2;
}
double getArea()
{
return Width*Height;
}
bool operator == (Rectangle const& _p) const
{
return this->TopLeft == _p.TopLeft &&
this->BottomLeft == _p.BottomLeft &&
this->TopRight == _p.TopRight &&
this->BottomRight == _p.BottomRight;
}
bool operator != (Rectangle const& _p) const
{
return !(*this == _p);
}
bool contains(Point _p)
{
return _p.m_x >= TopLeft.m_x&&_p.m_x<=BottomRight.m_x&&_p.m_y>=TopLeft.m_y&&_p.m_y <= BottomRight.m_y;
}
bool contains(Point _p1, Point _p2)
{
return contains(_p1) && contains(_p2);
}
bool intersects(const Rectangle & _r)
{
if (_r.TopRight == this->TopRight
||_r.TopLeft==this->TopLeft
||_r.BottomLeft==this->BottomLeft
||_r.BottomRight==this->BottomRight)
return true;
}
bool covers(const Rectangle & _r)
{
if (_r.TopRight.m_x > this->TopRight.m_x
&&_r.TopRight.m_y > this->TopRight.m_y
&& _r.TopLeft.m_x > this->TopLeft.m_x
&&_r.TopLeft.m_y > this->TopLeft.m_y
&& _r.BottomLeft.m_x > this->BottomLeft.m_x
&&_r.BottomLeft.m_y > this->BottomLeft.m_y
&& _r.BottomRight.m_x > this->BottomRight.m_x
&& _r.BottomRight.m_y > this->BottomRight.m_y
)
return true;
}
// ... TODO
/*------------------------------------------------------------------*/
};