Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/water/src/lib/math/rect2D.cc @ 8314

Last change on this file since 8314 was 8145, checked in by bensch, 19 years ago

trunk: merged the gui back
merged with command:
svn merge -r8114:HEAD https://svn.orxonox.net/orxonox/branches/gui .
→ no conflicts

File size: 9.9 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   ### File Specific:
12   main-programmer: Benjamin Grauer
13   co-programmer: Patrick Boenzli : Vector2D::scale()
14                                    Vector2D::abs()
15
16   Benjamin Grauer: port to Vector2D
17*/
18
19#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_MATH
20
21#include "rect2D.h"
22#ifdef DEBUG
23  #include "debug.h"
24#else
25  #include <stdio.h>
26  #define PRINT(x) printf
27#endif
28
29/**
30 * @brief creates a new Rectangle with
31 * @param x the left border
32 * @param y the top border
33 * @param width: the width
34 * @param height the height
35 */
36Rect2D::Rect2D(float x, float y, float width, float height)
37{
38  this->setLeft(x),
39  this->setTop(y);
40  this->setSize(width, height);
41}
42
43/**
44 * @brief creates a new Rectangle with
45 * @param topLeft the top-left corner
46 * @param bottomRight the lower-right corner
47 */
48Rect2D::Rect2D(const Vector2D& topLeft, const Vector2D& bottomRight )
49{
50  this->setTopLeft(topLeft);
51  this->setBottomRight(bottomRight);
52}
53
54/**
55 * @brief creates a new Rectangle with
56 * @param rect the rectangle to copy.
57 */
58Rect2D::Rect2D(const Rect2D& rect)
59{
60  *this = rect;
61}
62
63/**
64 * @brief copies the Values of rect to this rect
65 * @param rect copy the values from.
66 * @returns this reference.
67 */
68Rect2D& Rect2D::operator=(const Rect2D& rect)
69{
70  this->_topLeft = rect._topLeft;
71  this->_bottomRight = rect._bottomRight;
72  return *this;
73}
74
75/**
76 * @brief compares two rectanles.
77 * @param rect the rectangle to compare
78 * @returns true if the two rectangles are the same
79 */
80bool Rect2D::operator==(const Rect2D& rect) const
81{
82  return (this->_topLeft == rect._topLeft &&
83          this->_bottomRight == rect._bottomRight);
84}
85
86/**
87 * @brief negative compares two rectanles.
88 * @param rect the rectangle to compare
89 * @returns true if the two rectangles are different
90 */
91bool Rect2D::operator!=(const Rect2D& rect) const
92{
93  return (this->_topLeft != rect._topLeft ||
94          this->_bottomRight != rect._bottomRight);
95}
96
97/**
98 * @brief adds two rectangles together
99 * @param rect the rectagle to be added
100 * @returns a Rectangle, where both are added together.
101 *
102 * @note since adding rectangles does not make sense generally, here a description:
103 * adds toplefts and botomrights
104 */
105Rect2D Rect2D::operator+(const Rect2D& rect) const
106{
107  return Rect2D(this->_topLeft + rect._topLeft,
108                this->_bottomRight + rect._bottomRight);
109}
110
111
112/**
113 * @brief adds two rectangles together
114 * @param rect the rectagle to be added to this one
115 * @returns a Rectangle, where both are added together.
116 */
117Rect2D& Rect2D::operator+=(const Rect2D& rect)
118{
119  this->_topLeft += rect._topLeft;
120  this->_bottomRight += rect._bottomRight;
121  return *this;
122}
123
124
125/**
126 * @brief subracts two rectangles from each other
127 * @param rect the rectagle to be substracted from this one
128 * @returns a Rectangle, where both are substracted from one another.
129 */
130Rect2D Rect2D::operator-(const Rect2D& rect) const
131{
132  return Rect2D(this->_topLeft - rect._topLeft,
133                this->_bottomRight - rect._bottomRight);
134}
135
136/**
137 * @brief subracts two rectangles from each other
138 * @param rect the rectagle to be substracted from this one
139 * @returns a Rectangle, where both are substracted from one another.
140 */
141Rect2D& Rect2D::operator-=(const Rect2D& rect)
142{
143  this->_topLeft -= rect._topLeft;
144  this->_bottomRight -= rect._bottomRight;
145  return *this;
146}
147
148/**
149 * @brief intersects two Rectangles.
150 * @see intersection .
151 * @param rect the Rectangle to intersect with this one.
152 * @returns the intersection Region.
153 */
154Rect2D Rect2D::operator&(const Rect2D& rect) const
155{
156  return this->intersection(rect);
157}
158
159/**
160 * @brief intersects two Rectangles and assigns result to this one.
161 * @param rect the Rectangle to intersect with this one.
162 * @returns the intersection Region.
163 */
164Rect2D& Rect2D::operator&=(const Rect2D& rect)
165{
166  *this = this->intersection(rect);
167}
168
169/**
170 * TODO coment/implement
171 */
172Rect2D Rect2D::operator*(const Vector2D& scaling) const
173{
174#warning implements this...
175  //this->scale(scaling);
176}
177
178/**
179 * @brief sets the width of the Rectangle
180 * @param width the new width of the Rectangle
181 *
182 * the rectangle will be resized from the top left corner out.
183 */
184void Rect2D::setWidth(float width)
185{
186  this->_bottomRight.x = this->_topLeft.x + width;
187}
188
189
190/**
191 * @brief sets the height of the Rectangle
192 * @param height the new height of the Rectangle
193 *
194 * the rectangle will be resized from the top left corner out.
195 */
196void Rect2D::setHeight(float height)
197{
198  this->_bottomRight.y = this->_topLeft.y + height;
199}
200
201/**
202 * @brief sets the size of the Rectangle
203 * @param width the new width of the Rectangle.
204 * @param height the new height of the Rectangle.
205 * the rectangle will be resized from the top left corner out.
206 */
207void Rect2D::setSize(float width, float height)
208{
209  this->_bottomRight = this->_topLeft + Vector2D(width, height);
210}
211
212
213/**
214 * @brief sets the size of the Rectangle
215 * @param size the new size of the Rectangle.
216 * the rectangle will be resized from the top left corner out.
217 */
218void Rect2D::setSize(const Vector2D& size)
219{
220  this->_bottomRight = this->_topLeft + size;
221}
222
223/**
224 * @brief sets the new Center
225 * @param center the center to move the Rectangle to.
226 * moves the Rectangle from its current center to the new Center
227 */
228void Rect2D::setCenter(const Vector2D& center)
229{
230  this->move(center - this->center());
231}
232
233/**
234 * @brief scales the Rectangle from topLeft out.
235 * @param x: the scale factor in x direction
236 */
237void Rect2D::scaleX(float x)
238{
239  this->_bottomRight.x = this->_topLeft.x + this->width() * x;
240}
241
242/**
243 * @brief scales the Rectangle from topLeft out.
244 * @param y: the scale factor in y direction
245 */
246void Rect2D::scaleY(float y)
247{
248  this->_bottomRight.y = this->_topLeft.y + this->height() * y;
249}
250
251/**
252 * @brief scales the Rectangle from topLeft out.
253 * @param x: the scale factor in x direction
254 * @param y: the scale factor in y direction
255 */
256void Rect2D::scale(float x, float y)
257{
258  this->scale(Vector2D(x,y));
259}
260
261/**
262 * @brief scales the Rectangle from topLeft out.
263 * @param v: the scale factors.
264 */
265void Rect2D::scale(const Vector2D& v)
266{
267  this->_bottomRight = this->_topLeft + this->size().internalMultipy(v);
268
269}
270
271/**
272 * @brief scales the Rectangle from the Center out.
273 * @param x: the scale factor in x-direction.
274 * @param y: the scale factor in y-direction.
275 */
276void Rect2D::scaleCentered(float x, float y)
277{
278  this->scaleCentered(Vector2D(x, y));
279}
280
281
282/**
283 * @brief scales the Rectangle from the Center out.
284 * @param v: the scale factors.
285 */
286void Rect2D::scaleCentered(const Vector2D& v)
287{
288#warning implement this
289}
290
291/**
292 * @brief moves the Rectangle
293 * @param x the amount to move in x.
294 */
295void Rect2D::moveX(float x)
296{
297  this->_topLeft.x += x;
298  this->_bottomRight.x += x;
299}
300
301/**
302 * @brief moves the Rectangle
303 * @param y the amount to move in y.
304 */
305void Rect2D::moveY(float y)
306{
307  this->_topLeft.y += y;
308  this->_bottomRight.y += y;
309}
310
311/**
312 * @brief moves the Rectangle
313 * @param x the amount to move in x.
314 * @param y the amount to move in y.
315 */
316void Rect2D::move(float x, float y)
317{
318  this->move(Vector2D(x, y));
319}
320
321/**
322 * @brief moves the Rectangle
323 * @param v the amount to move.
324 */
325void Rect2D::move(const Vector2D& v)
326{
327  this->_topLeft += v;
328  this->_bottomRight += v;
329}
330
331/**
332 * @brief swaps top and bottom.
333 */
334void Rect2D::swapTopBottom()
335{
336  float tmp = this->top();
337  this->setTop(this->bottom());
338  this->setBottom(tmp);
339}
340
341/**
342 * @brief swaps left and right.
343 */
344void Rect2D::swapLeftRight()
345{
346  float tmp = this->left();
347  this->setLeft(this->right());
348  this->setRight(tmp);
349}
350
351/**
352 * @brief normalizes the Rectangle.
353 *
354 * Normalizing a Rectangle means, that the top is above bottom, and left is left of right afterwards :)
355 */
356void Rect2D::normalize()
357{
358  if (this->top() > this->bottom())
359    this->swapTopBottom();
360  if (this->left() > this->right())
361    this->swapLeftRight();
362}
363
364/**
365 * @brief normalizes the Rectangle.
366 * @returns the normalized version of this rectangle.
367 *
368 * Normalizing a Rectangle means, that the top is above bottom, and left is left of right afterwards :)
369 */
370Rect2D Rect2D::normalized() const
371{
372  Rect2D norm(*this);
373  norm.normalize();
374  return norm;
375}
376
377/**
378 * @brief The intersection of two Rectangles is calculated and returned.
379 * @param intersector the Rectangle to intersect with this one.
380 * @return the intersection region.
381 */
382Rect2D Rect2D::intersection(const Rect2D& intersector) const
383{
384#warning implement
385}
386
387/**
388 * @brief checks if the intersection of two Rectangles is not empty.
389 */
390bool Rect2D::intersects(const Rect2D& intersector) const
391{
392#warning implement
393
394}
395
396/**
397 * @brief make an Extensive join of two rectangles.
398 * @param rect the rectangle to unite with this one.
399 * @returns the united rectangle
400 */
401Rect2D Rect2D::unite(const Rect2D& rect)
402{
403#warning implement
404
405}
406
407/**
408 * @brief checks if rect is inside of this Rectangle.
409 * @param rect The Rectangle that should be inside of this one.
410 * @returns tru if it is.
411 */
412bool Rect2D::contains(const Rect2D& rect) const
413{
414  return (this->top() <= rect.top() &&
415          this->bottom() >= rect.bottom() &&
416          this->left() <= rect.left() &&
417          this->right() >= rect.right());
418}
419
420/**
421 * @param point the point to check if it is inside.
422 * @returns true if the point is inside of this Rectangle.
423 */
424bool Rect2D::contains(const Vector2D& point) const
425{
426  return (this->top() <= point.y &&
427          this->bottom() >= point.y &&
428          this->left() <= point.x &&
429          this->right() >= point.x);
430}
431
432/**
433 * @brief slerps this Rectangle to rect.
434 * @param rect the rectangle to slerp to.
435 * @param value how much to slerp to [0:stay, 1:go there]
436 * @returns reference to this.
437 */
438const Rect2D& Rect2D::slerp(const Rect2D& rect, float value)
439{
440  this->_topLeft.slerp(rect._topLeft, value);
441  this->_bottomRight.slerp(rect._bottomRight, value);
442
443  return *this;
444}
445
Note: See TracBrowser for help on using the repository browser.