Some really horrible utility Quadtree functions.
[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         struct Rect
10         {
11                 Real x; Real y; Real w; Real h;
12                 Rect() = default; // Needed so we can fread/fwrite this struct
13                 Rect(Real _x, Real _y, Real _w, Real _h) : x(_x), y(_y), w(_w), h(_h) {}
14                 std::string Str() const
15                 {
16                         std::stringstream s;
17                         // float conversion needed because it is fucking impossible to get ostreams working with template classes
18                         s << "{" << Float(x) << ", " << Float(y) << ", " << Float(x + w) << ", " << Float(y + h) << " (w: " << Float(w) <<", h: " << Float(h) <<")}";
19                         return s.str();
20                 }
21                 inline bool PointIn(Real pt_x, Real pt_y) const
22                 {
23                         if (pt_x < x) return false;
24                         if (pt_y < y) return false;
25                         if (pt_x > x + w) return false;
26                         if (pt_y > y + h) return false;
27                         return true;
28                 }
29         };
30
31         inline Rect TransformRectCoordinates(const Rect& view, const Rect& r)
32         {
33                 Rect out;
34                 Real w = (view.w == Real(0))?Real(1):view.w;
35                 Real h = (view.h == Real(0))?Real(1):view.h;
36                 out.x = (r.x - view.x) / w;
37                 out.y = (r.y - view.y) / h;
38                 out.w = r.w / w;
39                 out.h = r.h / h;
40                 return out;
41         }
42
43         inline Vec2 TransformPointCoordinates(const Rect& view, const Vec2& v)
44         {
45                 Vec2 out;
46                 out.x = (v.x - view.x) / view.w;
47                 out.y = (v.y - view.y) / view.h;
48                 return out;
49         }
50
51
52 }
53
54 #endif //_RECT_H

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