root / trunk / tbeta / OSX / apps / tbeta app / Configapp / src / Calibration / rect2d.h @ 5

View | Annotate | Download (5 KB)

1 5 ss
#ifndef __TOUCHLIB_RECT2D__
2 5 ss
#define __TOUCHLIB_RECT2D__
3 5 ss
4 5 ss
#include "vector2d.h"
5 5 ss
6 5 ss
        // The following code was originally written by Nikolaus Gebhardt as part of Irrlicht.
7 5 ss
        // See www.irrlicht3d.org
8 5 ss
9 5 ss
    // The Irrlicht Engine License
10 5 ss
    // Copyright © 2002-2005 Nikolaus Gebhardt
11 5 ss
    // This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held
12 5 ss
    // liable for any damages arising from the use of this software.
13 5 ss
    //
14 5 ss
    // Permission is granted to anyone to use this software for any purpose, including commercial applications, and to
15 5 ss
    // alter it and redistribute it freely, subject to the following restrictions:
16 5 ss
    //
17 5 ss
    // 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software.
18 5 ss
    //    If you use this software in a product, an acknowledgment in the product documentation would be appreciated but
19 5 ss
    //    is not required.
20 5 ss
    // 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
21 5 ss
    // 3. This notice may not be removed or altered from any source distribution.
22 5 ss
23 5 ss
        //!        Rectangle template.
24 5 ss
25 5 ss
26 5 ss
27 5 ss
    template <class T>
28 5 ss
        class rect
29 5 ss
        {
30 5 ss
        public:
31 5 ss
32 5 ss
                rect()
33 5 ss
                        : upperLeftCorner(0,0), lowerRightCorner(0,0) {};
34 5 ss
35 5 ss
36 5 ss
                rect(T x, T y, T x2, T y2)
37 5 ss
                        : upperLeftCorner(x,y), lowerRightCorner(x2,y2) {};
38 5 ss
39 5 ss
40 5 ss
                rect(const vector2d<T>& upperLeft, const vector2d<T>& lowerRight)
41 5 ss
                        : upperLeftCorner(upperLeft), lowerRightCorner(lowerRight) {};
42 5 ss
43 5 ss
                inline void addPoint(const vector2d<T> &pt)
44 5 ss
                {
45 5 ss
                        if(upperLeftCorner == vector2d<T>(0,0) && lowerRightCorner == vector2d<T>(0,0))
46 5 ss
                        {
47 5 ss
                                upperLeftCorner = pt;
48 5 ss
                                lowerRightCorner = pt;
49 5 ss
                                return;
50 5 ss
                        }
51 5 ss
52 5 ss
                        if(pt.X < upperLeftCorner.X)
53 5 ss
                                upperLeftCorner.X = pt.X;
54 5 ss
                        if(pt.Y < upperLeftCorner.Y)
55 5 ss
                                upperLeftCorner.Y = pt.Y;
56 5 ss
57 5 ss
                        if(pt.X > lowerRightCorner.X)
58 5 ss
                                lowerRightCorner.X = pt.X;
59 5 ss
                        if(pt.Y > lowerRightCorner.Y)
60 5 ss
                                lowerRightCorner.Y = pt.Y;
61 5 ss
                };
62 5 ss
63 5 ss
                inline void addPoint(T x, T y)
64 5 ss
                {
65 5 ss
                        if(upperLeftCorner == vector2d<T>(0,0) && lowerRightCorner == vector2d<T>(0,0))
66 5 ss
                        {
67 5 ss
                                upperLeftCorner.X = x;
68 5 ss
                                upperLeftCorner.Y = y;
69 5 ss
                                lowerRightCorner.X = x;
70 5 ss
                                lowerRightCorner.Y = y;
71 5 ss
                                return;
72 5 ss
                        }
73 5 ss
74 5 ss
                        if(x < upperLeftCorner.X)
75 5 ss
                                upperLeftCorner.X = x;
76 5 ss
                        if(y < upperLeftCorner.Y)
77 5 ss
                                upperLeftCorner.Y = y;
78 5 ss
79 5 ss
                        if(x > lowerRightCorner.X)
80 5 ss
                                lowerRightCorner.X = x;
81 5 ss
                        if(y > lowerRightCorner.Y)
82 5 ss
                                lowerRightCorner.Y = y;
83 5 ss
                };
84 5 ss
85 5 ss
86 5 ss
                void reset(const vector2d<T> &pt)
87 5 ss
                {
88 5 ss
                        upperLeftCorner = pt;
89 5 ss
                        lowerRightCorner = pt;
90 5 ss
                }
91 5 ss
92 5 ss
                bool isPointInside(const vector2d<T>& pos) const
93 5 ss
                {
94 5 ss
                        return upperLeftCorner.X <= pos.X && upperLeftCorner.Y <= pos.Y &&
95 5 ss
                                lowerRightCorner.X >= pos.X && lowerRightCorner.Y >= pos.Y;
96 5 ss
                }
97 5 ss
98 5 ss
                //! Returns if the rectangle collides with an other rectangle.
99 5 ss
                bool isRectCollided(const rect<T>& other) const
100 5 ss
                {
101 5 ss
                        return (lowerRightCorner.Y > other.upperLeftCorner.Y && upperLeftCorner.Y < other.lowerRightCorner.Y &&
102 5 ss
                                        lowerRightCorner.X > other.upperLeftCorner.X && upperLeftCorner.X < other.lowerRightCorner.X);
103 5 ss
                }
104 5 ss
105 5 ss
                //! Returns if the rectangle collides with an other rectangle.
106 5 ss
                bool isBoxInside(const rect<T>& other) const
107 5 ss
                {
108 5 ss
                        //return (this->isPointInside(other.upperLeftCorner) && this->isPointInside(other.lowerRightCorner));
109 5 ss
                        return (other.upperLeftCorner.Y >= upperLeftCorner.Y && other.upperLeftCorner.X >= upperLeftCorner.X &&
110 5 ss
                                other.lowerRightCorner.Y <= lowerRightCorner.Y && other.lowerRightCorner.X <= lowerRightCorner.X);
111 5 ss
                }
112 5 ss
113 5 ss
                //! Returns width of rectangle.
114 5 ss
                T getWidth() const
115 5 ss
                {
116 5 ss
                        return lowerRightCorner.X - upperLeftCorner.X;
117 5 ss
                }
118 5 ss
119 5 ss
                //! Returns height of rectangle.
120 5 ss
                T getHeight() const
121 5 ss
                {
122 5 ss
                        return lowerRightCorner.Y - upperLeftCorner.Y;
123 5 ss
                }
124 5 ss
125 5 ss
                //! Returns the center of the rectangle
126 5 ss
                vector2d<T> getCenter() const
127 5 ss
                {
128 5 ss
                        return vector2d<T>((upperLeftCorner.X + lowerRightCorner.X) / 2,
129 5 ss
                                (upperLeftCorner.Y + lowerRightCorner.Y) / 2);
130 5 ss
                }
131 5 ss
132 5 ss
                T getArea() const
133 5 ss
                {
134 5 ss
                        return getHeight() * getWidth();
135 5 ss
                }
136 5 ss
137 5 ss
138 5 ss
                //! Clips this rectangle with another one.
139 5 ss
                void clipAgainst(const rect<T>& other)
140 5 ss
                {
141 5 ss
                        if (other.lowerRightCorner.X < lowerRightCorner.X)
142 5 ss
                                lowerRightCorner.X = other.lowerRightCorner.X;
143 5 ss
                        if (other.lowerRightCorner.Y < lowerRightCorner.Y)
144 5 ss
                                lowerRightCorner.Y = other.lowerRightCorner.Y;
145 5 ss
146 5 ss
                        if (other.upperLeftCorner.X > upperLeftCorner.X)
147 5 ss
                                upperLeftCorner.X = other.upperLeftCorner.X;
148 5 ss
                        if (other.upperLeftCorner.Y > upperLeftCorner.Y)
149 5 ss
                                upperLeftCorner.Y = other.upperLeftCorner.Y;
150 5 ss
151 5 ss
                        // correct possible invalid rect resulting from clipping
152 5 ss
            if (upperLeftCorner.Y > lowerRightCorner.Y)
153 5 ss
                upperLeftCorner.Y = lowerRightCorner.Y;
154 5 ss
            if (upperLeftCorner.X > lowerRightCorner.X)
155 5 ss
                upperLeftCorner.X = lowerRightCorner.X;
156 5 ss
                }
157 5 ss
158 5 ss
159 5 ss
160 5 ss
                vector2d<T> upperLeftCorner;
161 5 ss
                vector2d<T> lowerRightCorner;
162 5 ss
        };
163 5 ss
164 5 ss
        //! Typedef for float 2d vector.
165 5 ss
        typedef rect<float> rect2df;
166 5 ss
167 5 ss
#endif