rectangle.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #include "rectangle.h"
  2. Rect::Rect( int x1, int y1, int x2, int y2 )
  3. {
  4. if ( x1 < x2 )
  5. {
  6. left = x1;
  7. right = x2;
  8. }
  9. else
  10. {
  11. left = x2;
  12. right = x1;
  13. }
  14. if ( y1 < y2 )
  15. {
  16. top = y1;
  17. bottom = y2;
  18. }
  19. else
  20. {
  21. top = y2;
  22. bottom = y1;
  23. }
  24. }
  25. Rect::Rect( const Point& p1, const Point& p2 )
  26. {
  27. if ( p1.x < p2.x )
  28. {
  29. left = p1.x;
  30. right = p2.x;
  31. }
  32. else
  33. {
  34. left = p2.x;
  35. right = p1.x;
  36. }
  37. if ( p1.y < p2.y )
  38. {
  39. top = p1.y;
  40. bottom = p2.y;
  41. }
  42. else
  43. {
  44. top = p2.y;
  45. bottom = p1.y;
  46. }
  47. }
  48. Rect::Rect( const Rect& other )
  49. : left( other.left ),
  50. top( other.top ),
  51. right( other.right ),
  52. bottom( other.bottom )
  53. {
  54. }
  55. Rect::~Rect()
  56. {
  57. }
  58. Point Rect::TopLeft()
  59. {
  60. Point p( left, top );
  61. return p;
  62. }
  63. Point Rect::BottomRight()
  64. {
  65. Point p( right, bottom );
  66. return p;
  67. }
  68. Point Rect::CenterPoint()
  69. {
  70. Point p( ( right - left ) / 2, ( bottom - top ) / 2 );
  71. return p;
  72. }
  73. int Rect::Width()
  74. {
  75. return right - left + 1;
  76. }
  77. int Rect::Height()
  78. {
  79. return bottom - top + 1;
  80. }
  81. void Rect::SetRectEmpty()
  82. {
  83. left = 0;
  84. right = 0;
  85. top = 0;
  86. bottom = 0;
  87. }
  88. bool Rect::IsRectNull()
  89. {
  90. return ((left == 0) && (right == 0) && (top == 0) && (bottom == 0));
  91. }