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

View | Annotate | Download (5 KB)

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