mRect.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _MRECT_H_
  23. #define _MRECT_H_
  24. //Includes
  25. #ifndef _MPOINT_H_
  26. #include "math/mPoint.h"
  27. #endif
  28. class RectI
  29. {
  30. public:
  31. Point2I point;
  32. Point2I extent;
  33. public:
  34. RectI() { }
  35. RectI(const Point2I& in_rMin,
  36. const Point2I& in_rExtent);
  37. RectI(const S32 in_left, const S32 in_top,
  38. const S32 in_width, const S32 in_height);
  39. void set(const Point2I& in_rMin, const Point2I& in_rExtent);
  40. void set(const S32 in_left, const S32 in_top,
  41. const S32 in_width, const S32 in_height);
  42. bool intersect(const RectI& clipRect);
  43. bool pointInRect(const Point2I& pt) const;
  44. bool contains(const RectI& R) const;
  45. bool overlaps(RectI R) const;
  46. void inset(S32 x, S32 y);
  47. void unionRects(const RectI&);
  48. Point2I centre( void ) const;
  49. S32 len_x() const;
  50. S32 len_y() const;
  51. bool operator==(const RectI&) const;
  52. bool operator!=(const RectI&) const;
  53. bool isValidRect() const { return (extent.x > 0 && extent.y > 0); }
  54. };
  55. class RectF
  56. {
  57. public:
  58. Point2F point;
  59. Point2F extent;
  60. public:
  61. RectF() { }
  62. RectF(const Point2F& in_rMin,
  63. const Point2F& in_rExtent);
  64. RectF(const F32 in_left, const F32 in_top,
  65. const F32 in_width, const F32 in_height);
  66. void inset(F32 x, F32 y);
  67. bool intersect(const RectF& clipRect);
  68. bool overlaps(const RectF&) const;
  69. Point2F centre( void ) const;
  70. F32 len_x() const;
  71. F32 len_y() const;
  72. bool isValidRect() const { return (extent.x > 0 && extent.y > 0); }
  73. };
  74. class RectD
  75. {
  76. public:
  77. Point2D point;
  78. Point2D extent;
  79. public:
  80. RectD() { }
  81. RectD(const Point2D& in_rMin,
  82. const Point2D& in_rExtent);
  83. RectD(const F64 in_left, const F64 in_top,
  84. const F64 in_width, const F64 in_height);
  85. void inset(F64 x, F64 y);
  86. bool intersect(const RectD& clipRect);
  87. Point2D centre( void ) const;
  88. F64 len_x() const;
  89. F64 len_y() const;
  90. bool isValidRect() const { return (extent.x > 0 && extent.y > 0); }
  91. };
  92. //------------------------------------------------------------------------------
  93. //-------------------------------------- INLINES (RectI)
  94. //
  95. inline
  96. RectI::RectI(const Point2I& in_rMin,
  97. const Point2I& in_rExtent)
  98. : point(in_rMin),
  99. extent(in_rExtent)
  100. {
  101. //
  102. }
  103. inline
  104. RectI::RectI(const S32 in_left, const S32 in_top,
  105. const S32 in_width, const S32 in_height)
  106. : point(in_left, in_top),
  107. extent(in_width, in_height)
  108. {
  109. //
  110. }
  111. inline void RectI::set(const Point2I& in_rMin, const Point2I& in_rExtent)
  112. {
  113. point = in_rMin;
  114. extent = in_rExtent;
  115. }
  116. inline void RectI::set(const S32 in_left, const S32 in_top,
  117. const S32 in_width, const S32 in_height)
  118. {
  119. point.set(in_left, in_top);
  120. extent.set(in_width, in_height);
  121. }
  122. inline bool RectI::intersect(const RectI& clipRect)
  123. {
  124. Point2I bottomL;
  125. bottomL.x = getMin(point.x + extent.x - 1, clipRect.point.x + clipRect.extent.x - 1);
  126. bottomL.y = getMin(point.y + extent.y - 1, clipRect.point.y + clipRect.extent.y - 1);
  127. point.x = getMax(point.x, clipRect.point.x);
  128. point.y = getMax(point.y, clipRect.point.y);
  129. extent.x = bottomL.x - point.x + 1;
  130. extent.y = bottomL.y - point.y + 1;
  131. return isValidRect();
  132. }
  133. inline bool RectI::pointInRect(const Point2I &pt) const
  134. {
  135. return (pt.x >= point.x && pt.x < point.x + extent.x && pt.y >= point.y && pt.y < point.y + extent.y);
  136. }
  137. inline bool RectI::contains(const RectI& R) const
  138. {
  139. if (point.x <= R.point.x && point.y <= R.point.y)
  140. if (R.point.x + R.extent.x <= point.x + extent.x)
  141. if (R.point.y + R.extent.y <= point.y + extent.y)
  142. return true;
  143. return false;
  144. }
  145. inline bool RectI::overlaps(RectI R) const
  146. {
  147. return R.intersect (* this);
  148. }
  149. inline void RectI::inset(S32 x, S32 y)
  150. {
  151. point.x += x;
  152. point.y += y;
  153. extent.x -= 2 * x;
  154. extent.y -= 2 * y;
  155. }
  156. inline void RectF::inset(F32 x, F32 y)
  157. {
  158. point.x += x;
  159. point.y += y;
  160. extent.x -= 2 * x;
  161. extent.y -= 2 * y;
  162. }
  163. inline void RectD::inset(F64 x, F64 y)
  164. {
  165. point.x += x;
  166. point.y += y;
  167. extent.x -= 2 * x;
  168. extent.y -= 2 * y;
  169. }
  170. inline void RectI::unionRects(const RectI& u)
  171. {
  172. S32 minx = point.x < u.point.x ? point.x : u.point.x;
  173. S32 miny = point.y < u.point.y ? point.y : u.point.y;
  174. S32 maxx = (point.x + extent.x) > (u.point.x + u.extent.x) ? (point.x + extent.x) : (u.point.x + u.extent.x);
  175. S32 maxy = (point.y + extent.y) > (u.point.y + u.extent.y) ? (point.y + extent.y) : (u.point.y + u.extent.y);
  176. point.x = minx;
  177. point.y = miny;
  178. extent.x = maxx - minx;
  179. extent.y = maxy - miny;
  180. }
  181. inline Point2I RectI::centre( void ) const
  182. {
  183. return Point2I( point.x + (S32)(extent.x * 0.5f), point.y + (S32)(extent.y * 0.5f) );
  184. }
  185. inline S32
  186. RectI::len_x() const
  187. {
  188. return extent.x;
  189. }
  190. inline S32
  191. RectI::len_y() const
  192. {
  193. return extent.y;
  194. }
  195. inline bool
  196. RectI::operator==(const RectI& in_rCompare) const
  197. {
  198. return (point == in_rCompare.point) && (extent == in_rCompare.extent);
  199. }
  200. inline bool
  201. RectI::operator!=(const RectI& in_rCompare) const
  202. {
  203. return (operator==(in_rCompare) == false);
  204. }
  205. //------------------------------------------------------------------------------
  206. //-------------------------------------- INLINES (RectF)
  207. //
  208. inline
  209. RectF::RectF(const Point2F& in_rMin,
  210. const Point2F& in_rExtent)
  211. : point(in_rMin),
  212. extent(in_rExtent)
  213. {
  214. //
  215. }
  216. inline
  217. RectF::RectF(const F32 in_left, const F32 in_top,
  218. const F32 in_width, const F32 in_height)
  219. : point(in_left, in_top),
  220. extent(in_width, in_height)
  221. {
  222. //
  223. }
  224. inline Point2F RectF::centre( void ) const
  225. {
  226. return Point2F( point.x + (extent.x * 0.5f), point.y + (extent.y * 0.5f) );
  227. }
  228. inline F32
  229. RectF::len_x() const
  230. {
  231. return extent.x;
  232. }
  233. inline F32
  234. RectF::len_y() const
  235. {
  236. return extent.y;
  237. }
  238. inline bool RectF::intersect(const RectF& clipRect)
  239. {
  240. Point2F bottomL;
  241. bottomL.x = getMin(point.x + extent.x, clipRect.point.x + clipRect.extent.x);
  242. bottomL.y = getMin(point.y + extent.y, clipRect.point.y + clipRect.extent.y);
  243. point.x = getMax(point.x, clipRect.point.x);
  244. point.y = getMax(point.y, clipRect.point.y);
  245. extent.x = bottomL.x - point.x;
  246. extent.y = bottomL.y - point.y;
  247. return isValidRect();
  248. }
  249. inline bool RectF::overlaps(const RectF& clipRect) const
  250. {
  251. RectF test = *this;
  252. return test.intersect(clipRect);
  253. }
  254. //------------------------------------------------------------------------------
  255. //-------------------------------------- INLINES (RectD)
  256. //
  257. inline
  258. RectD::RectD(const Point2D& in_rMin,
  259. const Point2D& in_rExtent)
  260. : point(in_rMin),
  261. extent(in_rExtent)
  262. {
  263. //
  264. }
  265. inline
  266. RectD::RectD(const F64 in_left, const F64 in_top,
  267. const F64 in_width, const F64 in_height)
  268. : point(in_left, in_top),
  269. extent(in_width, in_height)
  270. {
  271. //
  272. }
  273. inline Point2D RectD::centre( void ) const
  274. {
  275. return Point2D( point.x + (F64)(extent.x * 0.5), point.y + (F64)(extent.y * 0.5) );
  276. }
  277. inline F64
  278. RectD::len_x() const
  279. {
  280. return extent.x;
  281. }
  282. inline F64
  283. RectD::len_y() const
  284. {
  285. return extent.y;
  286. }
  287. inline bool RectD::intersect(const RectD& clipRect)
  288. {
  289. Point2D bottomL;
  290. bottomL.x = getMin(point.x + extent.x, clipRect.point.x + clipRect.extent.x);
  291. bottomL.y = getMin(point.y + extent.y, clipRect.point.y + clipRect.extent.y);
  292. point.x = getMax(point.x, clipRect.point.x);
  293. point.y = getMax(point.y, clipRect.point.y);
  294. extent.x = bottomL.x - point.x;
  295. extent.y = bottomL.y - point.y;
  296. return isValidRect();
  297. }
  298. #endif //_RECT_H_