fork download
  1. #ifndef _RECTANGLE_HPP_
  2. #define _RECTANGLE_HPP_
  3.  
  4. /*****************************************************************************/
  5.  
  6. #include "point.hpp"
  7.  
  8. /*****************************************************************************/
  9.  
  10.  
  11. class Rectangle
  12. {
  13. Point TopLeft, TopRight,BottomLeft,BottomRight;
  14. double Width, Height;
  15. public:
  16. Rectangle(Point _TopLeft, Point _BottomRight)
  17. {
  18. if ((_TopLeft.m_x > _BottomRight.m_x) || (_TopLeft.m_y > _BottomRight.m_y))
  19. throw std::logic_error("Invalid rectangle coordinates");
  20. }
  21. Rectangle(Point TopLeft, double Width, double Height)
  22. {
  23. if(Width <=0|| Height <=0)
  24. throw std::logic_error("Invalid rectangle coordinates");
  25. }
  26. Point getTopLeft()
  27. {
  28. return TopLeft;
  29. }
  30. Point getTopRight()
  31. {
  32. return TopRight;
  33. }
  34. Point getBottomLeft()
  35. {
  36. return BottomLeft;
  37. }
  38. Point getBottomRight()
  39. {
  40. return BottomRight;
  41. }
  42. double getWidth()
  43. {
  44. return Width;
  45. }
  46. double getHeight()
  47. {
  48. return Height;
  49. }
  50. double getPerimeter()
  51. {
  52. return (Width+Height)*2;
  53. }
  54. double getArea()
  55. {
  56. return Width*Height;
  57. }
  58. bool operator == (Rectangle const& _p) const
  59. {
  60. return this->TopLeft == _p.TopLeft &&
  61. this->BottomLeft == _p.BottomLeft &&
  62. this->TopRight == _p.TopRight &&
  63. this->BottomRight == _p.BottomRight;
  64. }
  65. bool operator != (Rectangle const& _p) const
  66. {
  67. return !(*this == _p);
  68. }
  69. bool contains(Point _p)
  70. {
  71. 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;
  72.  
  73. }
  74. bool contains(Point _p1, Point _p2)
  75. {
  76. return contains(_p1) && contains(_p2);
  77. }
  78. bool intersects(const Rectangle & _r)
  79. {
  80. if (_r.TopRight == this->TopRight
  81. ||_r.TopLeft==this->TopLeft
  82. ||_r.BottomLeft==this->BottomLeft
  83. ||_r.BottomRight==this->BottomRight)
  84. return true;
  85. }
  86. bool covers(const Rectangle & _r)
  87. {
  88. if (_r.TopRight.m_x > this->TopRight.m_x
  89. &&_r.TopRight.m_y > this->TopRight.m_y
  90. && _r.TopLeft.m_x > this->TopLeft.m_x
  91. &&_r.TopLeft.m_y > this->TopLeft.m_y
  92. && _r.BottomLeft.m_x > this->BottomLeft.m_x
  93. &&_r.BottomLeft.m_y > this->BottomLeft.m_y
  94. && _r.BottomRight.m_x > this->BottomRight.m_x
  95. && _r.BottomRight.m_y > this->BottomRight.m_y
  96. )
  97. return true;
  98. }
  99. // ... TODO
  100.  
  101. /*------------------------------------------------------------------*/
  102.  
  103. };
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:6:21: fatal error: point.hpp: No such file or directory
compilation terminated.
stdout
Standard output is empty