9 template <class T = IPDF::Real>
13 //TRect() = default; // Needed so we can fread/fwrite this struct
14 TRect(T _x=0, T _y=0, T _w=1, T _h=1) : x(_x), y(_y), w(_w), h(_h) {}
15 template <class B> TRect(const TRect<B> & cpy) : x(T(cpy.x)), y(T(cpy.y)), w(T(cpy.w)), h(T(cpy.h)) {}
17 std::string Str() const
20 // float conversion needed because it is fucking impossible to get ostreams working with template classes
21 s << "{" << x << ", " << y << ", " << (x + w) << ", " << (y + h) << " (w: " << w <<", h: " << h <<")}";
24 inline bool PointIn(T pt_x, T pt_y) const
26 if (pt_x <= x) return false;
27 if (pt_y <= y) return false;
28 if (pt_x >= x + w) return false;
29 if (pt_y >= y + h) return false;
33 inline bool Intersects(const TRect& other) const
35 if (x + w < other.x) return false;
36 if (y + h < other.y) return false;
37 if (x > other.x + other.w) return false;
38 if (y > other.y + other.h) return false;
42 inline bool Contains(const TRect& other) const
44 return PointIn(other.x, other.y) && PointIn(other.x + other.w, other.y + other.h);
47 template <class B> TRect<B> Convert() const {return TRect<B>(B(x), B(y), B(w), B(h));}
49 template <class B> TRect<T> & operator=(const TRect<B> & equ)
58 // Clips "other" to "this"
59 inline TRect Clip(const TRect& other) const
61 TRect clipped = other;
64 clipped.w += clipped.x - x;
69 clipped.h += clipped.y - y;
72 if (clipped.x + clipped.w > x + w)
74 clipped.w += ((x + w) - (clipped.x + clipped.w));
76 if (clipped.y + clipped.h > y + h)
78 clipped.h += ((y + h) - (clipped.y + clipped.h));
86 template <class T = IPDF::Real>
87 inline TRect<T> TransformRectCoordinates(const TRect<T> & view, const TRect<T> & r)
90 T w = (view.w == T(0))?T(1):view.w;
91 T h = (view.h == T(0))?T(1):view.h;
92 out.x = (r.x - view.x) / w; //r.x = out.x *w + view.x
93 out.y = (r.y - view.y) / h; // r.y = out.y*h + view.y
94 out.w = r.w / w; // r.w = out.w * w
95 out.h = r.h / h; // r.h = out.h * h
100 template <class T = IPDF::Real>
101 inline Vec2 TransformPointCoordinates(const TRect<T> & view, const Vec2& v)
104 out.x = (v.x - view.x) / view.w;
105 out.y = (v.y - view.y) / view.h;
109 typedef TRect<Real> Rect;