Rect2.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 pos;
  13. Size2 size;
  14. inline const Vector2& get_pos() const { return pos; }
  15. inline void set_pos(const Vector2& p_pos) { pos=p_pos; }
  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 ( pos.x >= (p_rect.pos.x + p_rect.size.width) )
  21. return false;
  22. if ( (pos.x+size.width) <= p_rect.pos.x )
  23. return false;
  24. if ( pos.y >= (p_rect.pos.y + p_rect.size.height) )
  25. return false;
  26. if ( (pos.y+size.height) <= p_rect.pos.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_pos=nullptr, Point2* r_normal=nullptr) const;
  33. inline bool encloses(const Rect2& p_rect) const {
  34. return (p_rect.pos.x>=pos.x) && (p_rect.pos.y>=pos.y) &&
  35. ((p_rect.pos.x+p_rect.size.x)<(pos.x+size.x)) &&
  36. ((p_rect.pos.y+p_rect.size.y)<(pos.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 < pos.x)
  45. return false;
  46. if (p_point.y < pos.y)
  47. return false;
  48. if (p_point.x >= (pos.x+size.x) )
  49. return false;
  50. if (p_point.y >= (pos.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 pos==p_rect.pos && size==p_rect.size; }
  56. inline bool operator!=(const Rect2& p_rect) const { return pos!=p_rect.pos || size!=p_rect.size; }
  57. inline Rect2 grow(real_t p_by) const {
  58. Rect2 g=*this;
  59. g.pos.x-=p_by;
  60. g.pos.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=pos;
  72. Vector2 end=pos+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. pos=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) { pos=Point2(p_x,p_y); size=Size2( p_width, p_height ); }
  87. inline Rect2( const Point2& p_pos, const Size2& p_size ) { pos=p_pos; size=p_size; }
  88. };
  89. }
  90. #endif // RECT2_H