Rect.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "Types.h"
  25. #include "Vec2.h"
  26. namespace crown
  27. {
  28. class Circle;
  29. /// 2D rectangle.
  30. ///
  31. /// Used mainly for collision detection and intersection tests.
  32. class Rect
  33. {
  34. public:
  35. /// Does nothing for efficiency.
  36. Rect();
  37. /// Constructs from @a min and @a max
  38. Rect(const Vec2& min, const Vec2& max);
  39. Rect(const Rect& rect);
  40. const Vec2& min() const;
  41. const Vec2& max() const;
  42. void set_min(const Vec2& min);
  43. void set_max(const Vec2& max);
  44. Vec2 center() const;
  45. float radius() const;
  46. float area() const;
  47. /// Returns the diagonal of the rect.
  48. Vec2 size() const;
  49. /// Returns whether @a point point is contained into the rect.
  50. bool contains_point(const Vec2& point) const;
  51. /// Returns whether the rect intersects @a r.
  52. bool intersects_rect(const Rect& rect) const;
  53. /// Sets the Rect from a @a center and a @a width - @a height
  54. void set_from_center_and_dimensions(Vec2 center, float width, float height);
  55. /// Returns the four vertices of the rect.
  56. void vertices(Vec2 v[4]) const;
  57. /// Returns the @a index -th vetex of the rect.
  58. Vec2 vertex(uint32_t index) const;
  59. /// Returns the equivalent circle.
  60. Circle to_circle() const;
  61. /// Ensures that min and max aren't swapped.
  62. void fix();
  63. private:
  64. Vec2 m_min;
  65. Vec2 m_max;
  66. };
  67. //-----------------------------------------------------------------------------
  68. inline Rect::Rect()
  69. {
  70. }
  71. //-----------------------------------------------------------------------------
  72. inline Rect::Rect(const Vec2& min, const Vec2& max) : m_min(min), m_max(max)
  73. {
  74. }
  75. //-----------------------------------------------------------------------------
  76. inline Rect::Rect(const Rect& rect) : m_min(rect.m_min), m_max(rect.m_max)
  77. {
  78. }
  79. //-----------------------------------------------------------------------------
  80. inline const Vec2& Rect::min() const
  81. {
  82. return m_min;
  83. }
  84. //-----------------------------------------------------------------------------
  85. inline const Vec2& Rect::max() const
  86. {
  87. return m_max;
  88. }
  89. //-----------------------------------------------------------------------------
  90. inline void Rect::set_min(const Vec2& min)
  91. {
  92. m_min = min;
  93. }
  94. //-----------------------------------------------------------------------------
  95. inline void Rect::set_max(const Vec2& max)
  96. {
  97. m_max = max;
  98. }
  99. } // namespace crown