Rect2.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*************************************************************************/
  2. /* Rect2.hpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef RECT2_H
  31. #define RECT2_H
  32. #include "Vector2.hpp"
  33. #include <cmath>
  34. #include <cstdlib>
  35. namespace godot {
  36. class String;
  37. typedef Vector2 Size2;
  38. typedef Vector2 Point2;
  39. struct Transform2D;
  40. struct Rect2 {
  41. Point2 position;
  42. Size2 size;
  43. inline const Vector2 &get_position() const { return position; }
  44. inline void set_position(const Vector2 &p_position) { position = p_position; }
  45. inline const Vector2 &get_size() const { return size; }
  46. inline void set_size(const Vector2 &p_size) { size = p_size; }
  47. inline real_t get_area() const { return size.width * size.height; }
  48. inline bool intersects(const Rect2 &p_rect) const {
  49. if (position.x >= (p_rect.position.x + p_rect.size.width))
  50. return false;
  51. if ((position.x + size.width) <= p_rect.position.x)
  52. return false;
  53. if (position.y >= (p_rect.position.y + p_rect.size.height))
  54. return false;
  55. if ((position.y + size.height) <= p_rect.position.y)
  56. return false;
  57. return true;
  58. }
  59. real_t distance_to(const Vector2 &p_point) const;
  60. bool intersects_transformed(const Transform2D &p_xform, const Rect2 &p_rect) const;
  61. bool intersects_segment(const Point2 &p_from, const Point2 &p_to, Point2 *r_position = nullptr, Point2 *r_normal = nullptr) const;
  62. inline bool encloses(const Rect2 &p_rect) const {
  63. return (p_rect.position.x >= position.x) && (p_rect.position.y >= position.y) &&
  64. ((p_rect.position.x + p_rect.size.x) < (position.x + size.x)) &&
  65. ((p_rect.position.y + p_rect.size.y) < (position.y + size.y));
  66. }
  67. inline bool has_no_area() const {
  68. return (size.x <= 0 || size.y <= 0);
  69. }
  70. Rect2 clip(const Rect2 &p_rect) const;
  71. Rect2 merge(const Rect2 &p_rect) const;
  72. inline bool has_point(const Point2 &p_point) const {
  73. if (p_point.x < position.x)
  74. return false;
  75. if (p_point.y < position.y)
  76. return false;
  77. if (p_point.x >= (position.x + size.x))
  78. return false;
  79. if (p_point.y >= (position.y + size.y))
  80. return false;
  81. return true;
  82. }
  83. inline bool no_area() const { return (size.width <= 0 || size.height <= 0); }
  84. inline bool operator==(const Rect2 &p_rect) const { return position == p_rect.position && size == p_rect.size; }
  85. inline bool operator!=(const Rect2 &p_rect) const { return position != p_rect.position || size != p_rect.size; }
  86. inline Rect2 grow(real_t p_by) const {
  87. Rect2 g = *this;
  88. g.position.x -= p_by;
  89. g.position.y -= p_by;
  90. g.size.width += p_by * 2;
  91. g.size.height += p_by * 2;
  92. return g;
  93. }
  94. inline Rect2 expand(const Vector2 &p_vector) const {
  95. Rect2 r = *this;
  96. r.expand_to(p_vector);
  97. return r;
  98. }
  99. inline void expand_to(const Vector2 &p_vector) { //in place function for speed
  100. Vector2 begin = position;
  101. Vector2 end = position + size;
  102. if (p_vector.x < begin.x)
  103. begin.x = p_vector.x;
  104. if (p_vector.y < begin.y)
  105. begin.y = p_vector.y;
  106. if (p_vector.x > end.x)
  107. end.x = p_vector.x;
  108. if (p_vector.y > end.y)
  109. end.y = p_vector.y;
  110. position = begin;
  111. size = end - begin;
  112. }
  113. operator String() const;
  114. inline Rect2() {}
  115. inline Rect2(real_t p_x, real_t p_y, real_t p_width, real_t p_height) {
  116. position = Point2(p_x, p_y);
  117. size = Size2(p_width, p_height);
  118. }
  119. inline Rect2(const Point2 &p_position, const Size2 &p_size) {
  120. position = p_position;
  121. size = p_size;
  122. }
  123. };
  124. } // namespace godot
  125. #endif // RECT2_H