// (C) 2013-2015, Sergei Zaychenko, KNURE, Kharkiv, Ukraine
#include "rectangle.hpp"
#include <stdexcept>
/*****************************************************************************/
// ... TODO ...
/*****************************************************************************/
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");
}
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;
}
};