Rect2.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #ifndef RECT2_H
  2. #define RECT2_H
  3. #include "Vector2.hpp"
  4. #include <cmath>
  5. #include <cstdlib>
  6. namespace godot {
  7. class String;
  8. typedef Vector2 Size2;
  9. typedef Vector2 Point2;
  10. struct Transform2D;
  11. struct Rect2 {
  12. Point2 position;
  13. Size2 size;
  14. inline const Vector2 &get_position() const { return position; }
  15. inline void set_position(const Vector2 &p_position) { position = p_position; }
  16. inline const Vector2 &get_size() const { return size; }
  17. inline void set_size(const Vector2 &p_size) { size = p_size; }
  18. inline real_t get_area() const { return size.width * size.height; }
  19. inline bool intersects(const Rect2 &p_rect) const {
  20. if (position.x >= (p_rect.position.x + p_rect.size.width))
  21. return false;
  22. if ((position.x + size.width) <= p_rect.position.x)
  23. return false;
  24. if (position.y >= (p_rect.position.y + p_rect.size.height))
  25. return false;
  26. if ((position.y + size.height) <= p_rect.position.y)
  27. return false;
  28. return true;
  29. }
  30. real_t distance_to(const Vector2 &p_point) const;
  31. bool intersects_transformed(const Transform2D &p_xform, const Rect2 &p_rect) const;
  32. bool intersects_segment(const Point2 &p_from, const Point2 &p_to, Point2 *r_position = nullptr, Point2 *r_normal = nullptr) const;
  33. inline bool encloses(const Rect2 &p_rect) const {
  34. return (p_rect.position.x >= position.x) && (p_rect.position.y >= position.y) &&
  35. ((p_rect.position.x + p_rect.size.x) < (position.x + size.x)) &&
  36. ((p_rect.position.y + p_rect.size.y) < (position.y + size.y));
  37. }
  38. inline bool has_no_area() const {
  39. return (size.x <= 0 || size.y <= 0);
  40. }
  41. Rect2 clip(const Rect2 &p_rect) const;
  42. Rect2 merge(const Rect2 &p_rect) const;
  43. inline bool has_point(const Point2 &p_point) const {
  44. if (p_point.x < position.x)
  45. return false;
  46. if (p_point.y < position.y)
  47. return false;
  48. if (p_point.x >= (position.x + size.x))
  49. return false;
  50. if (p_point.y >= (position.y + size.y))
  51. return false;
  52. return true;
  53. }
  54. inline bool no_area() const { return (size.width <= 0 || size.height <= 0); }
  55. inline bool operator==(const Rect2 &p_rect) const { return position == p_rect.position && size == p_rect.size; }
  56. inline bool operator!=(const Rect2 &p_rect) const { return position != p_rect.position || size != p_rect.size; }
  57. inline Rect2 grow(real_t p_by) const {
  58. Rect2 g = *this;
  59. g.position.x -= p_by;
  60. g.position.y -= p_by;
  61. g.size.width += p_by * 2;
  62. g.size.height += p_by * 2;
  63. return g;
  64. }
  65. inline Rect2 expand(const Vector2 &p_vector) const {
  66. Rect2 r = *this;
  67. r.expand_to(p_vector);
  68. return r;
  69. }
  70. inline void expand_to(const Vector2 &p_vector) { //in place function for speed
  71. Vector2 begin = position;
  72. Vector2 end = position + size;
  73. if (p_vector.x < begin.x)
  74. begin.x = p_vector.x;
  75. if (p_vector.y < begin.y)
  76. begin.y = p_vector.y;
  77. if (p_vector.x > end.x)
  78. end.x = p_vector.x;
  79. if (p_vector.y > end.y)
  80. end.y = p_vector.y;
  81. position = begin;
  82. size = end - begin;
  83. }
  84. operator String() const;
  85. inline Rect2() {}
  86. inline Rect2(real_t p_x, real_t p_y, real_t p_width, real_t p_height) {
  87. position = Point2(p_x, p_y);
  88. size = Size2(p_width, p_height);
  89. }
  90. inline Rect2(const Point2 &p_position, const Size2 &p_size) {
  91. position = p_position;
  92. size = p_size;
  93. }
  94. };
  95. } // namespace godot
  96. #endif // RECT2_H