Rect2.hpp 3.1 KB

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