Bugfixes, performance fixes, tears.
[ipdf/code.git] / src / rect.h
1 #ifndef _RECT_H
2 #define _RECT_H
3
4 #include "common.h"
5 #include "real.h"
6
7 namespace IPDF
8 {
9         template <class T = IPDF::Real>
10         struct TRect
11         {
12                 T x; T y; T w; T h;
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)) {}
16                 
17                 std::string Str() const
18                 {
19                         std::stringstream s;
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 <<")}";
22                         return s.str();
23                 }
24                 inline bool PointIn(T pt_x, T pt_y) const
25                 {
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;
30                         return true;
31                 }
32
33                 inline bool Intersects(const TRect& other) const
34                 {
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;
39                         return true;
40                 }
41                 
42                 template <class B> TRect<B> Convert() const {return TRect<B>(B(x), B(y), B(w), B(h));}
43                 
44                 template <class B> TRect<T> & operator=(const TRect<B> & equ)
45                 {
46                         x = T(equ.x);
47                         y = T(equ.y);
48                         w = T(equ.w);
49                         h = T(equ.h);
50                         return *this;
51                 }
52         };
53
54
55
56         template <class T = IPDF::Real>
57         inline TRect<T> TransformRectCoordinates(const TRect<T> & view, const TRect<T> & r)
58         {
59                 TRect<T> out;
60                 T w = (view.w == T(0))?T(1):view.w;
61                 T h = (view.h == T(0))?T(1):view.h;
62                 out.x = (r.x - view.x) / w; //r.x = out.x *w + view.x
63                 out.y = (r.y - view.y) / h; // r.y = out.y*h + view.y
64                 out.w = r.w / w; // r.w = out.w * w
65                 out.h = r.h / h; // r.h = out.h * h
66                 return out;
67         }
68
69
70         template <class T = IPDF::Real>
71         inline Vec2 TransformPointCoordinates(const TRect<T> & view, const Vec2& v)
72         {
73                 Vec2 out;
74                 out.x = (v.x - view.x) / view.w;
75                 out.y = (v.y - view.y) / view.h;
76                 return out;
77         }
78         
79         typedef TRect<Real> Rect;
80         
81
82 }
83
84 #endif //_RECT_H

UCC git Repository :: git.ucc.asn.au