Rectangle.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #ifndef RECTANGLE_H_
  2. #define RECTANGLE_H_
  3. namespace gameplay
  4. {
  5. /**
  6. * Defines a rectangle.
  7. */
  8. class Rectangle
  9. {
  10. public:
  11. /**
  12. * Specifies the x-coordinate of the rectangle.
  13. */
  14. float x;
  15. /**
  16. * Specifies the y-coordinate of the rectangle.
  17. */
  18. float y;
  19. /**
  20. * Specifies the width of the rectangle.
  21. */
  22. unsigned int width;
  23. /**
  24. * Specifies the height of the rectangle.
  25. */
  26. unsigned int height;
  27. /**
  28. * Constructs a new rectangle initialized to all zeros.
  29. */
  30. Rectangle();
  31. /**
  32. * Constructs a new rectangle with the x = 0, y = 0 and the specified width and height.
  33. *
  34. * @param width The width of the rectangle.
  35. * @param height The height of the rectangle.
  36. */
  37. Rectangle(unsigned int width, unsigned int height);
  38. /**
  39. * Constructs a new rectangle with the specified x, y, width and height.
  40. *
  41. * @param x The x-coordinate of the rectangle.
  42. * @param y The y-coordinate of the rectangle.
  43. * @param width The width of the rectangle.
  44. * @param height The height of the rectangle.
  45. */
  46. Rectangle(float x, float y, unsigned int width, unsigned int height);
  47. /**
  48. * Constructs a new rectangle that is a copy of the specified rectangle.
  49. *
  50. * @param copy The rectangle to copy.
  51. */
  52. Rectangle(const Rectangle& copy);
  53. /**
  54. * Destructor.
  55. */
  56. ~Rectangle();
  57. /**
  58. * Returns a rectangle with all of its values set to zero.
  59. *
  60. * @return The empty rectangle with all of its values set to zero.
  61. */
  62. static const Rectangle& empty();
  63. /**
  64. * Gets a value that indicates whether the rectangle is empty.
  65. *
  66. * @return true if the rectangle is empty, false otherwise.
  67. */
  68. bool isEmpty() const;
  69. /**
  70. * Sets the values of this rectangle to the specified values.
  71. *
  72. * @param x The x-coordinate of the rectangle.
  73. * @param y The y-coordinate of the rectangle.
  74. * @param width The width of the rectangle.
  75. * @param height The height of the rectangle.
  76. */
  77. void set(float x, float y, unsigned int width, unsigned int height);
  78. /**
  79. * Sets the values of this rectangle to those in the specified rectangle.
  80. *
  81. * @param r The rectangle to copy.
  82. */
  83. void set(const Rectangle& r);
  84. /**
  85. * Sets the x-coordinate and y-coordinate values of this rectangle to the specified values.
  86. *
  87. * @param x The x-coordinate of the rectangle.
  88. * @param y The y-coordinate of the rectangle.
  89. */
  90. void setPosition(float x, float y);
  91. /**
  92. * Returns the x-coordinate of the left side of the rectangle.
  93. *
  94. * @return The x-coordinate of the left side of the rectangle.
  95. */
  96. float left() const;
  97. /**
  98. * Returns the y-coordinate of the top of the rectangle.
  99. *
  100. * @return The y-coordinate of the top of the rectangle.
  101. */
  102. float top() const;
  103. /**
  104. * Returns the x-coordinate of the right side of the rectangle.
  105. *
  106. * @return The x-coordinate of the right side of the rectangle.
  107. */
  108. float right() const;
  109. /**
  110. * Returns the y-coordinate of the bottom of the rectangle.
  111. *
  112. * @return The y-coordinate of the bottom of the rectangle.
  113. */
  114. float bottom() const;
  115. /**
  116. * Determines whether this rectangle contains a specified point.
  117. *
  118. * @param x The x-coordinate of the point.
  119. * @param y The y-coordinate of the point.
  120. *
  121. * @return true if the rectangle contains the point, false otherwise.
  122. */
  123. bool contains(float x, float y) const;
  124. /**
  125. * Determines whether this rectangle contains a specified rectangle.
  126. *
  127. * @param x The x-coordinate of the rectangle.
  128. * @param y The y-coordinate of the rectangle.
  129. * @param width The width of the rectangle.
  130. * @param height The height of the rectangle.
  131. *
  132. * @return true if the rectangle contains the specified rectangle, false
  133. * otherwise.
  134. */
  135. bool contains(float x, float y, unsigned int width, unsigned int height) const;
  136. /**
  137. * Determines whether this rectangle contains a specified rectangle.
  138. *
  139. * @param r The rectangle.
  140. *
  141. * @return true if the rectangle contains the specified rectangle, false
  142. * otherwise.
  143. */
  144. bool contains(const Rectangle& r) const;
  145. /**
  146. * Determines whether a specified rectangle intersects with this rectangle.
  147. *
  148. * @param x The x-coordinate of the rectangle.
  149. * @param y The y-coordinate of the rectangle.
  150. * @param width The width of the rectangle.
  151. * @param height The height of the rectangle.
  152. *
  153. * @return true if the specified Rectangle intersects with this one, false otherwise.
  154. */
  155. bool intersects(float x, float y, unsigned int width, unsigned int height) const;
  156. /**
  157. * Determines whether a specified rectangle intersects with this rectangle.
  158. *
  159. * @param r The rectangle.
  160. *
  161. * @return true if the specified rectangle intersects with this one, false
  162. * otherwise.
  163. */
  164. bool intersects(const Rectangle& r) const;
  165. /**
  166. * Returns a new rectangle that exactly contains two other rectangles.
  167. *
  168. * @param r1 The first rectangle to contain.
  169. * @param r2 The second rectangle to contain.
  170. * @param dst A rectangle to store the union of the two rectangle parameters.
  171. */
  172. static void combine(const Rectangle& r1, const Rectangle& r2, Rectangle* dst);
  173. /**
  174. * Pushes the edges of the Rectangle out by the horizontal and vertical values specified.
  175. *
  176. * Each corner of the Rectangle is pushed away from the center of the rectangle
  177. * by the specified amounts. This results in the width and height of the Rectangle
  178. * increasing by twice the values provided.
  179. *
  180. * @param horizontalAmount The value to push the sides out by.
  181. * @param verticalAmount The value to push the top and bottom out by.
  182. */
  183. void inflate(float horizontalAmount, float verticalAmount);
  184. /**
  185. * operator =
  186. */
  187. const Rectangle& operator = (const Rectangle& r);
  188. /**
  189. * operator ==
  190. */
  191. bool operator == (const Rectangle& r) const;
  192. /**
  193. * operator !=
  194. */
  195. bool operator != (const Rectangle& r) const;
  196. };
  197. }
  198. #endif