Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/math/rect2D.cc @ 9940

Last change on this file since 9940 was 8619, checked in by bensch, 18 years ago

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

File size: 10.9 KB
RevLine 
[4578]1/*
[2043]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:
[6615]12   main-programmer: Benjamin Grauer
13   co-programmer: Patrick Boenzli : Vector2D::scale()
14                                    Vector2D::abs()
[4578]15
[6615]16   Benjamin Grauer: port to Vector2D
[2043]17*/
18
[3590]19#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_MATH
[2043]20
[7914]21#include "rect2D.h"
[5662]22#ifdef DEBUG
[5672]23  #include "debug.h"
[5662]24#else
[5672]25  #include <stdio.h>
26  #define PRINT(x) printf
[5662]27#endif
[2043]28
[7914]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{
[8145]38  this->setLeft(x),
39  this->setTop(y);
[7914]40  this->setSize(width, height);
41}
[2043]42
43/**
[7914]44 * @brief creates a new Rectangle with
45 * @param topLeft the top-left corner
46 * @param bottomRight the lower-right corner
[5420]47 */
[7914]48Rect2D::Rect2D(const Vector2D& topLeft, const Vector2D& bottomRight )
[2551]49{
[7914]50  this->setTopLeft(topLeft);
51  this->setBottomRight(bottomRight);
[2551]52}
53
[3449]54/**
[7914]55 * @brief creates a new Rectangle with
56 * @param rect the rectangle to copy.
57 */
58Rect2D::Rect2D(const Rect2D& rect)
[4477]59{
[7914]60  *this = rect;
[4477]61}
62
63/**
[7914]64 * @brief copies the Values of rect to this rect
65 * @param rect copy the values from.
66 * @returns this reference.
[5420]67 */
[7914]68Rect2D& Rect2D::operator=(const Rect2D& rect)
[3541]69{
[7914]70  this->_topLeft = rect._topLeft;
71  this->_bottomRight = rect._bottomRight;
72  return *this;
[3541]73}
[7914]74
[7915]75/**
76 * @brief compares two rectanles.
77 * @param rect the rectangle to compare
78 * @returns true if the two rectangles are the same
79 */
[7914]80bool Rect2D::operator==(const Rect2D& rect) const
81{
82  return (this->_topLeft == rect._topLeft &&
[7915]83          this->_bottomRight == rect._bottomRight);
[7914]84}
85
[7915]86/**
87 * @brief negative compares two rectanles.
88 * @param rect the rectangle to compare
89 * @returns true if the two rectangles are different
90 */
[7914]91bool Rect2D::operator!=(const Rect2D& rect) const
92{
93  return (this->_topLeft != rect._topLeft ||
[7915]94          this->_bottomRight != rect._bottomRight);
[7914]95}
96
[7915]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 */
[7914]105Rect2D Rect2D::operator+(const Rect2D& rect) const
[7915]106{
107  return Rect2D(this->_topLeft + rect._topLeft,
108                this->_bottomRight + rect._bottomRight);
109}
[7914]110
111
[7915]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 */
[7914]117Rect2D& Rect2D::operator+=(const Rect2D& rect)
118{
119  this->_topLeft += rect._topLeft;
120  this->_bottomRight += rect._bottomRight;
121  return *this;
122}
123
124
[7915]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 */
[7914]130Rect2D Rect2D::operator-(const Rect2D& rect) const
131{
132  return Rect2D(this->_topLeft - rect._topLeft,
133                this->_bottomRight - rect._bottomRight);
134}
135
[7915]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 */
[7914]141Rect2D& Rect2D::operator-=(const Rect2D& rect)
142{
143  this->_topLeft -= rect._topLeft;
144  this->_bottomRight -= rect._bottomRight;
145  return *this;
146}
147
[7915]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 */
[7914]154Rect2D Rect2D::operator&(const Rect2D& rect) const
[7915]155{
156  return this->intersection(rect);
157}
[7914]158
[7915]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 */
[7914]164Rect2D& Rect2D::operator&=(const Rect2D& rect)
165{
166  *this = this->intersection(rect);
167}
168
[7915]169/**
170 * TODO coment/implement
171 */
[7914]172Rect2D Rect2D::operator*(const Vector2D& scaling) const
173{
174#warning implements this...
175  //this->scale(scaling);
176}
177
[7915]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 */
[7914]184void Rect2D::setWidth(float width)
185{
186  this->_bottomRight.x = this->_topLeft.x + width;
187}
188
189
[7915]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 */
[7914]196void Rect2D::setHeight(float height)
197{
198  this->_bottomRight.y = this->_topLeft.y + height;
199}
200
[7915]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 */
[7914]207void Rect2D::setSize(float width, float height)
208{
209  this->_bottomRight = this->_topLeft + Vector2D(width, height);
210}
211
[7915]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 */
[7914]218void Rect2D::setSize(const Vector2D& size)
219{
220  this->_bottomRight = this->_topLeft + size;
221}
222
[7915]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 */
[7914]228void Rect2D::setCenter(const Vector2D& center)
229{
230  this->move(center - this->center());
231}
232
[7915]233/**
[8448]234 * @brief sets the new Center
235 * @param x the center's X to move the Rectangle to.
236 * @param y the center's Y to move the Rectangle to.
237 * moves the Rectangle from its current center to the new Center
238 */
239void Rect2D::setCenter(float x, float y)
240{
241  this->setCenter(Vector2D(x, y));
242}
243
244/**
245 * @brief sets the new Center
246 * @param center the center to move the Rectangle to.
247 * moves the Rectangle from its current center to the new Center
248 */
249void Rect2D::setCenterX(float x)
250{
251#warning implement this
252}
253
254/**
255 * @brief sets the new Center
256 * @param center the center to move the Rectangle to.
257 * moves the Rectangle from its current center to the new Center
258 */
259void Rect2D::setCenterY(float y)
260{
261#warning implement this
262}
263
264/**
[7915]265 * @brief scales the Rectangle from topLeft out.
266 * @param x: the scale factor in x direction
267 */
[7914]268void Rect2D::scaleX(float x)
269{
270  this->_bottomRight.x = this->_topLeft.x + this->width() * x;
271}
272
[7915]273/**
274 * @brief scales the Rectangle from topLeft out.
275 * @param y: the scale factor in y direction
276 */
[7914]277void Rect2D::scaleY(float y)
278{
279  this->_bottomRight.y = this->_topLeft.y + this->height() * y;
280}
281
[7915]282/**
283 * @brief scales the Rectangle from topLeft out.
284 * @param x: the scale factor in x direction
285 * @param y: the scale factor in y direction
286 */
[7914]287void Rect2D::scale(float x, float y)
288{
289  this->scale(Vector2D(x,y));
290}
291
[7915]292/**
293 * @brief scales the Rectangle from topLeft out.
294 * @param v: the scale factors.
295 */
[7914]296void Rect2D::scale(const Vector2D& v)
297{
298  this->_bottomRight = this->_topLeft + this->size().internalMultipy(v);
299
300}
301
[7915]302/**
303 * @brief scales the Rectangle from the Center out.
304 * @param x: the scale factor in x-direction.
305 * @param y: the scale factor in y-direction.
306 */
[7914]307void Rect2D::scaleCentered(float x, float y)
308{
309  this->scaleCentered(Vector2D(x, y));
310}
311
[7915]312
313/**
314 * @brief scales the Rectangle from the Center out.
315 * @param v: the scale factors.
316 */
[7914]317void Rect2D::scaleCentered(const Vector2D& v)
318{
319#warning implement this
320}
321
[7915]322/**
323 * @brief moves the Rectangle
324 * @param x the amount to move in x.
325 */
[7914]326void Rect2D::moveX(float x)
327{
328  this->_topLeft.x += x;
329  this->_bottomRight.x += x;
330}
331
[7915]332/**
333 * @brief moves the Rectangle
334 * @param y the amount to move in y.
335 */
[7914]336void Rect2D::moveY(float y)
337{
338  this->_topLeft.y += y;
339  this->_bottomRight.y += y;
340}
[7915]341
342/**
343 * @brief moves the Rectangle
344 * @param x the amount to move in x.
345 * @param y the amount to move in y.
346 */
347void Rect2D::move(float x, float y)
348{
349  this->move(Vector2D(x, y));
350}
351
352/**
353 * @brief moves the Rectangle
354 * @param v the amount to move.
355 */
[7914]356void Rect2D::move(const Vector2D& v)
357{
358  this->_topLeft += v;
359  this->_bottomRight += v;
360}
361
[7915]362/**
363 * @brief swaps top and bottom.
364 */
[7914]365void Rect2D::swapTopBottom()
366{
367  float tmp = this->top();
368  this->setTop(this->bottom());
369  this->setBottom(tmp);
370}
371
[7915]372/**
373 * @brief swaps left and right.
374 */
[7914]375void Rect2D::swapLeftRight()
376{
377  float tmp = this->left();
378  this->setLeft(this->right());
379  this->setRight(tmp);
380}
381
[7915]382/**
383 * @brief normalizes the Rectangle.
384 *
385 * Normalizing a Rectangle means, that the top is above bottom, and left is left of right afterwards :)
386 */
[7914]387void Rect2D::normalize()
388{
389  if (this->top() > this->bottom())
390    this->swapTopBottom();
391  if (this->left() > this->right())
392    this->swapLeftRight();
393}
394
[7915]395/**
396 * @brief normalizes the Rectangle.
397 * @returns the normalized version of this rectangle.
398 *
399 * Normalizing a Rectangle means, that the top is above bottom, and left is left of right afterwards :)
400 */
[7914]401Rect2D Rect2D::normalized() const
402{
403  Rect2D norm(*this);
404  norm.normalize();
405  return norm;
406}
407
[7915]408/**
409 * @brief The intersection of two Rectangles is calculated and returned.
410 * @param intersector the Rectangle to intersect with this one.
411 * @return the intersection region.
412 */
[7914]413Rect2D Rect2D::intersection(const Rect2D& intersector) const
414{
415#warning implement
416}
417
[7915]418/**
419 * @brief checks if the intersection of two Rectangles is not empty.
420 */
[7914]421bool Rect2D::intersects(const Rect2D& intersector) const
[7915]422{
[7914]423#warning implement
424
[7915]425}
426
427/**
428 * @brief make an Extensive join of two rectangles.
429 * @param rect the rectangle to unite with this one.
430 * @returns the united rectangle
431 */
[7914]432Rect2D Rect2D::unite(const Rect2D& rect)
[7915]433{
[7914]434#warning implement
[7915]435
436}
437
438/**
439 * @brief checks if rect is inside of this Rectangle.
440 * @param rect The Rectangle that should be inside of this one.
441 * @returns tru if it is.
442 */
[7914]443bool Rect2D::contains(const Rect2D& rect) const
[7915]444{
445  return (this->top() <= rect.top() &&
446          this->bottom() >= rect.bottom() &&
447          this->left() <= rect.left() &&
448          this->right() >= rect.right());
449}
450
451/**
452 * @param point the point to check if it is inside.
453 * @returns true if the point is inside of this Rectangle.
454 */
[7914]455bool Rect2D::contains(const Vector2D& point) const
[7915]456{
457  return (this->top() <= point.y &&
458          this->bottom() >= point.y &&
459          this->left() <= point.x &&
460          this->right() >= point.x);
461}
[7914]462
[7915]463/**
464 * @brief slerps this Rectangle to rect.
465 * @param rect the rectangle to slerp to.
466 * @param value how much to slerp to [0:stay, 1:go there]
467 * @returns reference to this.
468 */
[7914]469const Rect2D& Rect2D::slerp(const Rect2D& rect, float value)
470{
471  this->_topLeft.slerp(rect._topLeft, value);
472  this->_bottomRight.slerp(rect._bottomRight, value);
473
474  return *this;
475}
476
[8619]477/**
478 * @brief print debug output for the Rect
479 */
480void Rect2D::debug() const
481{
482  PRINT(0)("Top: %0.2f Left: %0.2f; Bottom %0.2f Right:%0.2f; Size: %0.2fx%0.2f\n", top(), left(), bottom(), right(), size().x, size().y);
483}
Note: See TracBrowser for help on using the repository browser.