diff options
Diffstat (limited to '')
-rw-r--r-- | libraries/irrlicht-1.8/include/rect.h | 558 |
1 files changed, 279 insertions, 279 deletions
diff --git a/libraries/irrlicht-1.8/include/rect.h b/libraries/irrlicht-1.8/include/rect.h index 3f3883e..1e2572c 100644 --- a/libraries/irrlicht-1.8/include/rect.h +++ b/libraries/irrlicht-1.8/include/rect.h | |||
@@ -1,279 +1,279 @@ | |||
1 | // Copyright (C) 2002-2012 Nikolaus Gebhardt | 1 | // Copyright (C) 2002-2012 Nikolaus Gebhardt |
2 | // This file is part of the "Irrlicht Engine". | 2 | // This file is part of the "Irrlicht Engine". |
3 | // For conditions of distribution and use, see copyright notice in irrlicht.h | 3 | // For conditions of distribution and use, see copyright notice in irrlicht.h |
4 | 4 | ||
5 | #ifndef __IRR_RECT_H_INCLUDED__ | 5 | #ifndef __IRR_RECT_H_INCLUDED__ |
6 | #define __IRR_RECT_H_INCLUDED__ | 6 | #define __IRR_RECT_H_INCLUDED__ |
7 | 7 | ||
8 | #include "irrTypes.h" | 8 | #include "irrTypes.h" |
9 | #include "dimension2d.h" | 9 | #include "dimension2d.h" |
10 | #include "position2d.h" | 10 | #include "position2d.h" |
11 | 11 | ||
12 | namespace irr | 12 | namespace irr |
13 | { | 13 | { |
14 | namespace core | 14 | namespace core |
15 | { | 15 | { |
16 | 16 | ||
17 | //! Rectangle template. | 17 | //! Rectangle template. |
18 | /** Mostly used by 2D GUI elements and for 2D drawing methods. | 18 | /** Mostly used by 2D GUI elements and for 2D drawing methods. |
19 | It has 2 positions instead of position and dimension and a fast | 19 | It has 2 positions instead of position and dimension and a fast |
20 | method for collision detection with other rectangles and points. | 20 | method for collision detection with other rectangles and points. |
21 | 21 | ||
22 | Coordinates are (0,0) for top-left corner, and increasing to the right | 22 | Coordinates are (0,0) for top-left corner, and increasing to the right |
23 | and to the bottom. | 23 | and to the bottom. |
24 | */ | 24 | */ |
25 | template <class T> | 25 | template <class T> |
26 | class rect | 26 | class rect |
27 | { | 27 | { |
28 | public: | 28 | public: |
29 | 29 | ||
30 | //! Default constructor creating empty rectangle at (0,0) | 30 | //! Default constructor creating empty rectangle at (0,0) |
31 | rect() : UpperLeftCorner(0,0), LowerRightCorner(0,0) {} | 31 | rect() : UpperLeftCorner(0,0), LowerRightCorner(0,0) {} |
32 | 32 | ||
33 | //! Constructor with two corners | 33 | //! Constructor with two corners |
34 | rect(T x, T y, T x2, T y2) | 34 | rect(T x, T y, T x2, T y2) |
35 | : UpperLeftCorner(x,y), LowerRightCorner(x2,y2) {} | 35 | : UpperLeftCorner(x,y), LowerRightCorner(x2,y2) {} |
36 | 36 | ||
37 | //! Constructor with two corners | 37 | //! Constructor with two corners |
38 | rect(const position2d<T>& upperLeft, const position2d<T>& lowerRight) | 38 | rect(const position2d<T>& upperLeft, const position2d<T>& lowerRight) |
39 | : UpperLeftCorner(upperLeft), LowerRightCorner(lowerRight) {} | 39 | : UpperLeftCorner(upperLeft), LowerRightCorner(lowerRight) {} |
40 | 40 | ||
41 | //! Constructor with upper left corner and dimension | 41 | //! Constructor with upper left corner and dimension |
42 | template <class U> | 42 | template <class U> |
43 | rect(const position2d<T>& pos, const dimension2d<U>& size) | 43 | rect(const position2d<T>& pos, const dimension2d<U>& size) |
44 | : UpperLeftCorner(pos), LowerRightCorner(pos.X + size.Width, pos.Y + size.Height) {} | 44 | : UpperLeftCorner(pos), LowerRightCorner(pos.X + size.Width, pos.Y + size.Height) {} |
45 | 45 | ||
46 | //! move right by given numbers | 46 | //! move right by given numbers |
47 | rect<T> operator+(const position2d<T>& pos) const | 47 | rect<T> operator+(const position2d<T>& pos) const |
48 | { | 48 | { |
49 | rect<T> ret(*this); | 49 | rect<T> ret(*this); |
50 | return ret+=pos; | 50 | return ret+=pos; |
51 | } | 51 | } |
52 | 52 | ||
53 | //! move right by given numbers | 53 | //! move right by given numbers |
54 | rect<T>& operator+=(const position2d<T>& pos) | 54 | rect<T>& operator+=(const position2d<T>& pos) |
55 | { | 55 | { |
56 | UpperLeftCorner += pos; | 56 | UpperLeftCorner += pos; |
57 | LowerRightCorner += pos; | 57 | LowerRightCorner += pos; |
58 | return *this; | 58 | return *this; |
59 | } | 59 | } |
60 | 60 | ||
61 | //! move left by given numbers | 61 | //! move left by given numbers |
62 | rect<T> operator-(const position2d<T>& pos) const | 62 | rect<T> operator-(const position2d<T>& pos) const |
63 | { | 63 | { |
64 | rect<T> ret(*this); | 64 | rect<T> ret(*this); |
65 | return ret-=pos; | 65 | return ret-=pos; |
66 | } | 66 | } |
67 | 67 | ||
68 | //! move left by given numbers | 68 | //! move left by given numbers |
69 | rect<T>& operator-=(const position2d<T>& pos) | 69 | rect<T>& operator-=(const position2d<T>& pos) |
70 | { | 70 | { |
71 | UpperLeftCorner -= pos; | 71 | UpperLeftCorner -= pos; |
72 | LowerRightCorner -= pos; | 72 | LowerRightCorner -= pos; |
73 | return *this; | 73 | return *this; |
74 | } | 74 | } |
75 | 75 | ||
76 | //! equality operator | 76 | //! equality operator |
77 | bool operator==(const rect<T>& other) const | 77 | bool operator==(const rect<T>& other) const |
78 | { | 78 | { |
79 | return (UpperLeftCorner == other.UpperLeftCorner && | 79 | return (UpperLeftCorner == other.UpperLeftCorner && |
80 | LowerRightCorner == other.LowerRightCorner); | 80 | LowerRightCorner == other.LowerRightCorner); |
81 | } | 81 | } |
82 | 82 | ||
83 | //! inequality operator | 83 | //! inequality operator |
84 | bool operator!=(const rect<T>& other) const | 84 | bool operator!=(const rect<T>& other) const |
85 | { | 85 | { |
86 | return (UpperLeftCorner != other.UpperLeftCorner || | 86 | return (UpperLeftCorner != other.UpperLeftCorner || |
87 | LowerRightCorner != other.LowerRightCorner); | 87 | LowerRightCorner != other.LowerRightCorner); |
88 | } | 88 | } |
89 | 89 | ||
90 | //! compares size of rectangles | 90 | //! compares size of rectangles |
91 | bool operator<(const rect<T>& other) const | 91 | bool operator<(const rect<T>& other) const |
92 | { | 92 | { |
93 | return getArea() < other.getArea(); | 93 | return getArea() < other.getArea(); |
94 | } | 94 | } |
95 | 95 | ||
96 | //! Returns size of rectangle | 96 | //! Returns size of rectangle |
97 | T getArea() const | 97 | T getArea() const |
98 | { | 98 | { |
99 | return getWidth() * getHeight(); | 99 | return getWidth() * getHeight(); |
100 | } | 100 | } |
101 | 101 | ||
102 | //! Returns if a 2d point is within this rectangle. | 102 | //! Returns if a 2d point is within this rectangle. |
103 | /** \param pos Position to test if it lies within this rectangle. | 103 | /** \param pos Position to test if it lies within this rectangle. |
104 | \return True if the position is within the rectangle, false if not. */ | 104 | \return True if the position is within the rectangle, false if not. */ |
105 | bool isPointInside(const position2d<T>& pos) const | 105 | bool isPointInside(const position2d<T>& pos) const |
106 | { | 106 | { |
107 | return (UpperLeftCorner.X <= pos.X && | 107 | return (UpperLeftCorner.X <= pos.X && |
108 | UpperLeftCorner.Y <= pos.Y && | 108 | UpperLeftCorner.Y <= pos.Y && |
109 | LowerRightCorner.X >= pos.X && | 109 | LowerRightCorner.X >= pos.X && |
110 | LowerRightCorner.Y >= pos.Y); | 110 | LowerRightCorner.Y >= pos.Y); |
111 | } | 111 | } |
112 | 112 | ||
113 | //! Check if the rectangle collides with another rectangle. | 113 | //! Check if the rectangle collides with another rectangle. |
114 | /** \param other Rectangle to test collision with | 114 | /** \param other Rectangle to test collision with |
115 | \return True if the rectangles collide. */ | 115 | \return True if the rectangles collide. */ |
116 | bool isRectCollided(const rect<T>& other) const | 116 | bool isRectCollided(const rect<T>& other) const |
117 | { | 117 | { |
118 | return (LowerRightCorner.Y > other.UpperLeftCorner.Y && | 118 | return (LowerRightCorner.Y > other.UpperLeftCorner.Y && |
119 | UpperLeftCorner.Y < other.LowerRightCorner.Y && | 119 | UpperLeftCorner.Y < other.LowerRightCorner.Y && |
120 | LowerRightCorner.X > other.UpperLeftCorner.X && | 120 | LowerRightCorner.X > other.UpperLeftCorner.X && |
121 | UpperLeftCorner.X < other.LowerRightCorner.X); | 121 | UpperLeftCorner.X < other.LowerRightCorner.X); |
122 | } | 122 | } |
123 | 123 | ||
124 | //! Clips this rectangle with another one. | 124 | //! Clips this rectangle with another one. |
125 | /** \param other Rectangle to clip with */ | 125 | /** \param other Rectangle to clip with */ |
126 | void clipAgainst(const rect<T>& other) | 126 | void clipAgainst(const rect<T>& other) |
127 | { | 127 | { |
128 | if (other.LowerRightCorner.X < LowerRightCorner.X) | 128 | if (other.LowerRightCorner.X < LowerRightCorner.X) |
129 | LowerRightCorner.X = other.LowerRightCorner.X; | 129 | LowerRightCorner.X = other.LowerRightCorner.X; |
130 | if (other.LowerRightCorner.Y < LowerRightCorner.Y) | 130 | if (other.LowerRightCorner.Y < LowerRightCorner.Y) |
131 | LowerRightCorner.Y = other.LowerRightCorner.Y; | 131 | LowerRightCorner.Y = other.LowerRightCorner.Y; |
132 | 132 | ||
133 | if (other.UpperLeftCorner.X > UpperLeftCorner.X) | 133 | if (other.UpperLeftCorner.X > UpperLeftCorner.X) |
134 | UpperLeftCorner.X = other.UpperLeftCorner.X; | 134 | UpperLeftCorner.X = other.UpperLeftCorner.X; |
135 | if (other.UpperLeftCorner.Y > UpperLeftCorner.Y) | 135 | if (other.UpperLeftCorner.Y > UpperLeftCorner.Y) |
136 | UpperLeftCorner.Y = other.UpperLeftCorner.Y; | 136 | UpperLeftCorner.Y = other.UpperLeftCorner.Y; |
137 | 137 | ||
138 | // correct possible invalid rect resulting from clipping | 138 | // correct possible invalid rect resulting from clipping |
139 | if (UpperLeftCorner.Y > LowerRightCorner.Y) | 139 | if (UpperLeftCorner.Y > LowerRightCorner.Y) |
140 | UpperLeftCorner.Y = LowerRightCorner.Y; | 140 | UpperLeftCorner.Y = LowerRightCorner.Y; |
141 | if (UpperLeftCorner.X > LowerRightCorner.X) | 141 | if (UpperLeftCorner.X > LowerRightCorner.X) |
142 | UpperLeftCorner.X = LowerRightCorner.X; | 142 | UpperLeftCorner.X = LowerRightCorner.X; |
143 | } | 143 | } |
144 | 144 | ||
145 | //! Moves this rectangle to fit inside another one. | 145 | //! Moves this rectangle to fit inside another one. |
146 | /** \return True on success, false if not possible */ | 146 | /** \return True on success, false if not possible */ |
147 | bool constrainTo(const rect<T>& other) | 147 | bool constrainTo(const rect<T>& other) |
148 | { | 148 | { |
149 | if (other.getWidth() < getWidth() || other.getHeight() < getHeight()) | 149 | if (other.getWidth() < getWidth() || other.getHeight() < getHeight()) |
150 | return false; | 150 | return false; |
151 | 151 | ||
152 | T diff = other.LowerRightCorner.X - LowerRightCorner.X; | 152 | T diff = other.LowerRightCorner.X - LowerRightCorner.X; |
153 | if (diff < 0) | 153 | if (diff < 0) |
154 | { | 154 | { |
155 | LowerRightCorner.X += diff; | 155 | LowerRightCorner.X += diff; |
156 | UpperLeftCorner.X += diff; | 156 | UpperLeftCorner.X += diff; |
157 | } | 157 | } |
158 | 158 | ||
159 | diff = other.LowerRightCorner.Y - LowerRightCorner.Y; | 159 | diff = other.LowerRightCorner.Y - LowerRightCorner.Y; |
160 | if (diff < 0) | 160 | if (diff < 0) |
161 | { | 161 | { |
162 | LowerRightCorner.Y += diff; | 162 | LowerRightCorner.Y += diff; |
163 | UpperLeftCorner.Y += diff; | 163 | UpperLeftCorner.Y += diff; |
164 | } | 164 | } |
165 | 165 | ||
166 | diff = UpperLeftCorner.X - other.UpperLeftCorner.X; | 166 | diff = UpperLeftCorner.X - other.UpperLeftCorner.X; |
167 | if (diff < 0) | 167 | if (diff < 0) |
168 | { | 168 | { |
169 | UpperLeftCorner.X -= diff; | 169 | UpperLeftCorner.X -= diff; |
170 | LowerRightCorner.X -= diff; | 170 | LowerRightCorner.X -= diff; |
171 | } | 171 | } |
172 | 172 | ||
173 | diff = UpperLeftCorner.Y - other.UpperLeftCorner.Y; | 173 | diff = UpperLeftCorner.Y - other.UpperLeftCorner.Y; |
174 | if (diff < 0) | 174 | if (diff < 0) |
175 | { | 175 | { |
176 | UpperLeftCorner.Y -= diff; | 176 | UpperLeftCorner.Y -= diff; |
177 | LowerRightCorner.Y -= diff; | 177 | LowerRightCorner.Y -= diff; |
178 | } | 178 | } |
179 | 179 | ||
180 | return true; | 180 | return true; |
181 | } | 181 | } |
182 | 182 | ||
183 | //! Get width of rectangle. | 183 | //! Get width of rectangle. |
184 | T getWidth() const | 184 | T getWidth() const |
185 | { | 185 | { |
186 | return LowerRightCorner.X - UpperLeftCorner.X; | 186 | return LowerRightCorner.X - UpperLeftCorner.X; |
187 | } | 187 | } |
188 | 188 | ||
189 | //! Get height of rectangle. | 189 | //! Get height of rectangle. |
190 | T getHeight() const | 190 | T getHeight() const |
191 | { | 191 | { |
192 | return LowerRightCorner.Y - UpperLeftCorner.Y; | 192 | return LowerRightCorner.Y - UpperLeftCorner.Y; |
193 | } | 193 | } |
194 | 194 | ||
195 | //! If the lower right corner of the rect is smaller then the upper left, the points are swapped. | 195 | //! If the lower right corner of the rect is smaller then the upper left, the points are swapped. |
196 | void repair() | 196 | void repair() |
197 | { | 197 | { |
198 | if (LowerRightCorner.X < UpperLeftCorner.X) | 198 | if (LowerRightCorner.X < UpperLeftCorner.X) |
199 | { | 199 | { |
200 | T t = LowerRightCorner.X; | 200 | T t = LowerRightCorner.X; |
201 | LowerRightCorner.X = UpperLeftCorner.X; | 201 | LowerRightCorner.X = UpperLeftCorner.X; |
202 | UpperLeftCorner.X = t; | 202 | UpperLeftCorner.X = t; |
203 | } | 203 | } |
204 | 204 | ||
205 | if (LowerRightCorner.Y < UpperLeftCorner.Y) | 205 | if (LowerRightCorner.Y < UpperLeftCorner.Y) |
206 | { | 206 | { |
207 | T t = LowerRightCorner.Y; | 207 | T t = LowerRightCorner.Y; |
208 | LowerRightCorner.Y = UpperLeftCorner.Y; | 208 | LowerRightCorner.Y = UpperLeftCorner.Y; |
209 | UpperLeftCorner.Y = t; | 209 | UpperLeftCorner.Y = t; |
210 | } | 210 | } |
211 | } | 211 | } |
212 | 212 | ||
213 | //! Returns if the rect is valid to draw. | 213 | //! Returns if the rect is valid to draw. |
214 | /** It would be invalid if the UpperLeftCorner is lower or more | 214 | /** It would be invalid if the UpperLeftCorner is lower or more |
215 | right than the LowerRightCorner. */ | 215 | right than the LowerRightCorner. */ |
216 | bool isValid() const | 216 | bool isValid() const |
217 | { | 217 | { |
218 | return ((LowerRightCorner.X >= UpperLeftCorner.X) && | 218 | return ((LowerRightCorner.X >= UpperLeftCorner.X) && |
219 | (LowerRightCorner.Y >= UpperLeftCorner.Y)); | 219 | (LowerRightCorner.Y >= UpperLeftCorner.Y)); |
220 | } | 220 | } |
221 | 221 | ||
222 | //! Get the center of the rectangle | 222 | //! Get the center of the rectangle |
223 | position2d<T> getCenter() const | 223 | position2d<T> getCenter() const |
224 | { | 224 | { |
225 | return position2d<T>( | 225 | return position2d<T>( |
226 | (UpperLeftCorner.X + LowerRightCorner.X) / 2, | 226 | (UpperLeftCorner.X + LowerRightCorner.X) / 2, |
227 | (UpperLeftCorner.Y + LowerRightCorner.Y) / 2); | 227 | (UpperLeftCorner.Y + LowerRightCorner.Y) / 2); |
228 | } | 228 | } |
229 | 229 | ||
230 | //! Get the dimensions of the rectangle | 230 | //! Get the dimensions of the rectangle |
231 | dimension2d<T> getSize() const | 231 | dimension2d<T> getSize() const |
232 | { | 232 | { |
233 | return dimension2d<T>(getWidth(), getHeight()); | 233 | return dimension2d<T>(getWidth(), getHeight()); |
234 | } | 234 | } |
235 | 235 | ||
236 | 236 | ||
237 | //! Adds a point to the rectangle | 237 | //! Adds a point to the rectangle |
238 | /** Causes the rectangle to grow bigger if point is outside of | 238 | /** Causes the rectangle to grow bigger if point is outside of |
239 | the box | 239 | the box |
240 | \param p Point to add to the box. */ | 240 | \param p Point to add to the box. */ |
241 | void addInternalPoint(const position2d<T>& p) | 241 | void addInternalPoint(const position2d<T>& p) |
242 | { | 242 | { |
243 | addInternalPoint(p.X, p.Y); | 243 | addInternalPoint(p.X, p.Y); |
244 | } | 244 | } |
245 | 245 | ||
246 | //! Adds a point to the bounding rectangle | 246 | //! Adds a point to the bounding rectangle |
247 | /** Causes the rectangle to grow bigger if point is outside of | 247 | /** Causes the rectangle to grow bigger if point is outside of |
248 | the box | 248 | the box |
249 | \param x X-Coordinate of the point to add to this box. | 249 | \param x X-Coordinate of the point to add to this box. |
250 | \param y Y-Coordinate of the point to add to this box. */ | 250 | \param y Y-Coordinate of the point to add to this box. */ |
251 | void addInternalPoint(T x, T y) | 251 | void addInternalPoint(T x, T y) |
252 | { | 252 | { |
253 | if (x>LowerRightCorner.X) | 253 | if (x>LowerRightCorner.X) |
254 | LowerRightCorner.X = x; | 254 | LowerRightCorner.X = x; |
255 | if (y>LowerRightCorner.Y) | 255 | if (y>LowerRightCorner.Y) |
256 | LowerRightCorner.Y = y; | 256 | LowerRightCorner.Y = y; |
257 | 257 | ||
258 | if (x<UpperLeftCorner.X) | 258 | if (x<UpperLeftCorner.X) |
259 | UpperLeftCorner.X = x; | 259 | UpperLeftCorner.X = x; |
260 | if (y<UpperLeftCorner.Y) | 260 | if (y<UpperLeftCorner.Y) |
261 | UpperLeftCorner.Y = y; | 261 | UpperLeftCorner.Y = y; |
262 | } | 262 | } |
263 | 263 | ||
264 | //! Upper left corner | 264 | //! Upper left corner |
265 | position2d<T> UpperLeftCorner; | 265 | position2d<T> UpperLeftCorner; |
266 | //! Lower right corner | 266 | //! Lower right corner |
267 | position2d<T> LowerRightCorner; | 267 | position2d<T> LowerRightCorner; |
268 | }; | 268 | }; |
269 | 269 | ||
270 | //! Rectangle with float values | 270 | //! Rectangle with float values |
271 | typedef rect<f32> rectf; | 271 | typedef rect<f32> rectf; |
272 | //! Rectangle with int values | 272 | //! Rectangle with int values |
273 | typedef rect<s32> recti; | 273 | typedef rect<s32> recti; |
274 | 274 | ||
275 | } // end namespace core | 275 | } // end namespace core |
276 | } // end namespace irr | 276 | } // end namespace irr |
277 | 277 | ||
278 | #endif | 278 | #endif |
279 | 279 | ||