Rect.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. #include "Assert.h"
  24. #include "Rect.h"
  25. #include "Circle.h"
  26. #include "MathUtils.h"
  27. namespace crown
  28. {
  29. //-----------------------------------------------------------------------------
  30. bool Rect::contains_point(const Vector2& point) const
  31. {
  32. return (point.x >= m_min.x && point.y >= m_min.y &&
  33. point.x <= m_max.x && point.y <= m_max.y);
  34. }
  35. //-----------------------------------------------------------------------------
  36. bool Rect::intersects_rect(const Rect& rect) const
  37. {
  38. if (rect.m_min.x > m_max.x || rect.m_max.x < m_min.x || rect.m_min.y > m_max.y || rect.m_max.y < m_min.y)
  39. return false;
  40. return true;
  41. }
  42. //-----------------------------------------------------------------------------
  43. void Rect::set_from_center_and_dimensions(Vector2 center, float width, float height)
  44. {
  45. m_min.x = (float)(center.x - width / 2.0);
  46. m_min.y = (float)(center.y - height / 2.0);
  47. m_max.x = (float)(center.x + width / 2.0);
  48. m_max.y = (float)(center.y + height / 2.0);
  49. }
  50. //-----------------------------------------------------------------------------
  51. void Rect::vertices(Vector2 v[4]) const
  52. {
  53. // 3 ---- 2
  54. // | |
  55. // | |
  56. // 0 ---- 1
  57. v[0].x = m_min.x;
  58. v[0].y = m_min.y;
  59. v[1].x = m_max.x;
  60. v[1].y = m_min.y;
  61. v[2].x = m_max.x;
  62. v[2].y = m_max.y;
  63. v[3].x = m_min.x;
  64. v[3].y = m_max.y;
  65. }
  66. //-----------------------------------------------------------------------------
  67. Vector2 Rect::vertex(uint32_t index) const
  68. {
  69. CE_ASSERT(index < 4, "Index must be < 4");
  70. switch (index)
  71. {
  72. case 0:
  73. return Vector2(m_min.x, m_min.y);
  74. case 1:
  75. return Vector2(m_max.x, m_min.y);
  76. case 2:
  77. return Vector2(m_max.x, m_max.y);
  78. case 3:
  79. return Vector2(m_min.x, m_max.y);
  80. }
  81. return Vector2::ZERO;
  82. }
  83. //-----------------------------------------------------------------------------
  84. Vector2 Rect::center() const
  85. {
  86. return (m_min + m_max) * 0.5;
  87. }
  88. //-----------------------------------------------------------------------------
  89. float Rect::radius() const
  90. {
  91. return (m_max - (m_min + m_max) * 0.5).length();
  92. }
  93. //-----------------------------------------------------------------------------
  94. float Rect::area() const
  95. {
  96. return (m_max.x - m_min.x) * (m_max.y - m_min.y);
  97. }
  98. //-----------------------------------------------------------------------------
  99. Vector2 Rect::size() const
  100. {
  101. return (m_max - m_min);
  102. }
  103. //-----------------------------------------------------------------------------
  104. void Rect::fix()
  105. {
  106. if (m_min.x > m_max.x)
  107. {
  108. math::swap(m_min.x, m_max.x);
  109. }
  110. if (m_min.y > m_max.y)
  111. {
  112. math::swap(m_min.y, m_max.y);
  113. }
  114. }
  115. //-----------------------------------------------------------------------------
  116. Circle Rect::to_circle() const
  117. {
  118. return Circle(center(), radius());
  119. }
  120. } // namespace crown