#ifndef RECT2_H #define RECT2_H #include "Vector2.h" #include namespace godot { typedef Vector2 Size2; typedef Vector2 Point2; class Transform2D; #ifndef MAX #define MAX(a, b) (a > b ? a : b) #endif #ifndef MIN #define MIN(a, b) (a < b ? a : b) #endif struct Rect2 { Point2 pos; Size2 size; const Vector2& get_pos() const { return pos; } void set_pos(const Vector2& p_pos) { pos=p_pos; } const Vector2& get_size() const { return size; } void set_size(const Vector2& p_size) { size=p_size; } real_t get_area() const { return size.width*size.height; } inline bool intersects(const Rect2& p_rect) const { if ( pos.x >= (p_rect.pos.x + p_rect.size.width) ) return false; if ( (pos.x+size.width) <= p_rect.pos.x ) return false; if ( pos.y >= (p_rect.pos.y + p_rect.size.height) ) return false; if ( (pos.y+size.height) <= p_rect.pos.y ) return false; return true; } inline real_t distance_to(const Vector2& p_point) const { real_t dist = 1e20; if (p_point.x < pos.x) { dist=MIN(dist,pos.x-p_point.x); } if (p_point.y < pos.y) { dist=MIN(dist,pos.y-p_point.y); } if (p_point.x >= (pos.x+size.x) ) { dist=MIN(p_point.x-(pos.x+size.x),dist); } if (p_point.y >= (pos.y+size.y) ) { dist=MIN(p_point.y-(pos.y+size.y),dist); } if (dist==1e20) return 0; else return dist; } bool intersects_transformed(const Transform2D& p_xform, const Rect2& p_rect) const; bool intersects_segment(const Point2& p_from, const Point2& p_to, Point2* r_pos=NULL, Point2* r_normal=NULL) const; inline bool encloses(const Rect2& p_rect) const { return (p_rect.pos.x>=pos.x) && (p_rect.pos.y>=pos.y) && ((p_rect.pos.x+p_rect.size.x)<(pos.x+size.x)) && ((p_rect.pos.y+p_rect.size.y)<(pos.y+size.y)); } inline bool has_no_area() const { return (size.x<=0 || size.y<=0); } inline Rect2 clip(const Rect2& p_rect) const { /// return a clipped rect Rect2 new_rect=p_rect; if (!intersects( new_rect )) return Rect2(); new_rect.pos.x = MAX( p_rect.pos.x , pos.x ); new_rect.pos.y = MAX( p_rect.pos.y , pos.y ); Point2 p_rect_end=p_rect.pos+p_rect.size; Point2 end=pos+size; new_rect.size.x=MIN(p_rect_end.x,end.x) - new_rect.pos.x; new_rect.size.y=MIN(p_rect_end.y,end.y) - new_rect.pos.y; return new_rect; } inline Rect2 merge(const Rect2& p_rect) const { ///< return a merged rect Rect2 new_rect; new_rect.pos.x=MIN( p_rect.pos.x , pos.x ); new_rect.pos.y=MIN( p_rect.pos.y , pos.y ); new_rect.size.x = MAX( p_rect.pos.x+p_rect.size.x , pos.x+size.x ); new_rect.size.y = MAX( p_rect.pos.y+p_rect.size.y , pos.y+size.y ); new_rect.size = new_rect.size - new_rect.pos; //make relative again return new_rect; } inline bool has_point(const Point2& p_point) const { if (p_point.x < pos.x) return false; if (p_point.y < pos.y) return false; if (p_point.x >= (pos.x+size.x) ) return false; if (p_point.y >= (pos.y+size.y) ) return false; return true; } inline bool no_area() const { return (size.width<=0 || size.height<=0 ); } bool operator==(const Rect2& p_rect) const { return pos==p_rect.pos && size==p_rect.size; } bool operator!=(const Rect2& p_rect) const { return pos!=p_rect.pos || size!=p_rect.size; } inline Rect2 grow(real_t p_by) const { Rect2 g=*this; g.pos.x-=p_by; g.pos.y-=p_by; g.size.width+=p_by*2; g.size.height+=p_by*2; return g; } inline Rect2 expand(const Vector2& p_vector) const { Rect2 r = *this; r.expand_to(p_vector); return r; } inline void expand_to(const Vector2& p_vector) { //in place function for speed Vector2 begin=pos; Vector2 end=pos+size; if (p_vector.xend.x) end.x=p_vector.x; if (p_vector.y>end.y) end.y=p_vector.y; pos=begin; size=end-begin; } operator String() const { return String(pos)+", "+String(size); } Rect2() {} 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 ); } Rect2( const Point2& p_pos, const Size2& p_size ) { pos=p_pos; size=p_size; } }; bool Rect2::intersects_segment(const Point2& p_from, const Point2& p_to, Point2* r_pos,Point2* r_normal) const { real_t min=0,max=1; int axis=0; real_t sign=0; for(int i=0;i<2;i++) { real_t seg_from=p_from[i]; real_t seg_to=p_to[i]; real_t box_begin=pos[i]; real_t box_end=box_begin+size[i]; real_t cmin,cmax; real_t csign; if (seg_from < seg_to) { if (seg_from > box_end || seg_to < box_begin) return false; real_t length=seg_to-seg_from; cmin = (seg_from < box_begin)?((box_begin - seg_from)/length):0; cmax = (seg_to > box_end)?((box_end - seg_from)/length):1; csign=-1.0; } else { if (seg_to > box_end || seg_from < box_begin) return false; real_t length=seg_to-seg_from; cmin = (seg_from > box_end)?(box_end - seg_from)/length:0; cmax = (seg_to < box_begin)?(box_begin - seg_from)/length:1; csign=1.0; } if (cmin > min) { min = cmin; axis=i; sign=csign; } if (cmax < max) max = cmax; if (max < min) return false; } Vector2 rel=p_to-p_from; if (r_normal) { Vector2 normal; normal[axis]=sign; *r_normal=normal; } if (r_pos) *r_pos=p_from+rel*min; return true; } } #include "Transform2D.h" namespace godot { bool Rect2::intersects_transformed(const Transform2D& p_xform, const Rect2& p_rect) const { //SAT intersection between local and transformed rect2 Vector2 xf_points[4]={ p_xform.xform(p_rect.pos), p_xform.xform(Vector2(p_rect.pos.x+p_rect.size.x,p_rect.pos.y)), p_xform.xform(Vector2(p_rect.pos.x,p_rect.pos.y+p_rect.size.y)), p_xform.xform(Vector2(p_rect.pos.x+p_rect.size.x,p_rect.pos.y+p_rect.size.y)), }; real_t low_limit; //base rect2 first (faster) if (xf_points[0].y>pos.y) goto next1; if (xf_points[1].y>pos.y) goto next1; if (xf_points[2].y>pos.y) goto next1; if (xf_points[3].y>pos.y) goto next1; return false; next1: low_limit=pos.y+size.y; if (xf_points[0].ypos.x) goto next3; if (xf_points[1].x>pos.x) goto next3; if (xf_points[2].x>pos.x) goto next3; if (xf_points[3].x>pos.x) goto next3; return false; next3: low_limit=pos.x+size.x; if (xf_points[0].x maxb ) return false; if ( minb > maxa ) return false; maxa=p_xform.elements[1].dot(xf_points2[0]); mina=maxa; dp = p_xform.elements[1].dot(xf_points2[1]); maxa=MAX(dp,maxa); mina=MIN(dp,mina); dp = p_xform.elements[1].dot(xf_points2[2]); maxa=MAX(dp,maxa); mina=MIN(dp,mina); dp = p_xform.elements[1].dot(xf_points2[3]); maxa=MAX(dp,maxa); mina=MIN(dp,mina); maxb=p_xform.elements[1].dot(xf_points[0]); minb=maxb; dp = p_xform.elements[1].dot(xf_points[1]); maxb=MAX(dp,maxb); minb=MIN(dp,minb); dp = p_xform.elements[1].dot(xf_points[2]); maxb=MAX(dp,maxb); minb=MIN(dp,minb); dp = p_xform.elements[1].dot(xf_points[3]); maxb=MAX(dp,maxb); minb=MIN(dp,minb); if ( mina > maxb ) return false; if ( minb > maxa ) return false; return true; } } #endif // RECT2_H