Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8349 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
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/**
234 * @brief scales the Rectangle from topLeft out.
235 * @param x: the scale factor in x direction
236 */
[7914]237void Rect2D::scaleX(float x)
238{
239  this->_bottomRight.x = this->_topLeft.x + this->width() * x;
240}
241
[7915]242/**
243 * @brief scales the Rectangle from topLeft out.
244 * @param y: the scale factor in y direction
245 */
[7914]246void Rect2D::scaleY(float y)
247{
248  this->_bottomRight.y = this->_topLeft.y + this->height() * y;
249}
250
[7915]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 */
[7914]256void Rect2D::scale(float x, float y)
257{
258  this->scale(Vector2D(x,y));
259}
260
[7915]261/**
262 * @brief scales the Rectangle from topLeft out.
263 * @param v: the scale factors.
264 */
[7914]265void Rect2D::scale(const Vector2D& v)
266{
267  this->_bottomRight = this->_topLeft + this->size().internalMultipy(v);
268
269}
270
[7915]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 */
[7914]276void Rect2D::scaleCentered(float x, float y)
277{
278  this->scaleCentered(Vector2D(x, y));
279}
280
[7915]281
282/**
283 * @brief scales the Rectangle from the Center out.
284 * @param v: the scale factors.
285 */
[7914]286void Rect2D::scaleCentered(const Vector2D& v)
287{
288#warning implement this
289}
290
[7915]291/**
292 * @brief moves the Rectangle
293 * @param x the amount to move in x.
294 */
[7914]295void Rect2D::moveX(float x)
296{
297  this->_topLeft.x += x;
298  this->_bottomRight.x += x;
299}
300
[7915]301/**
302 * @brief moves the Rectangle
303 * @param y the amount to move in y.
304 */
[7914]305void Rect2D::moveY(float y)
306{
307  this->_topLeft.y += y;
308  this->_bottomRight.y += y;
309}
[7915]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 */
[7914]325void Rect2D::move(const Vector2D& v)
326{
327  this->_topLeft += v;
328  this->_bottomRight += v;
329}
330
[7915]331/**
332 * @brief swaps top and bottom.
333 */
[7914]334void Rect2D::swapTopBottom()
335{
336  float tmp = this->top();
337  this->setTop(this->bottom());
338  this->setBottom(tmp);
339}
340
[7915]341/**
342 * @brief swaps left and right.
343 */
[7914]344void Rect2D::swapLeftRight()
345{
346  float tmp = this->left();
347  this->setLeft(this->right());
348  this->setRight(tmp);
349}
350
[7915]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 */
[7914]356void Rect2D::normalize()
357{
358  if (this->top() > this->bottom())
359    this->swapTopBottom();
360  if (this->left() > this->right())
361    this->swapLeftRight();
362}
363
[7915]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 */
[7914]370Rect2D Rect2D::normalized() const
371{
372  Rect2D norm(*this);
373  norm.normalize();
374  return norm;
375}
376
[7915]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 */
[7914]382Rect2D Rect2D::intersection(const Rect2D& intersector) const
383{
384#warning implement
385}
386
[7915]387/**
388 * @brief checks if the intersection of two Rectangles is not empty.
389 */
[7914]390bool Rect2D::intersects(const Rect2D& intersector) const
[7915]391{
[7914]392#warning implement
393
[7915]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 */
[7914]401Rect2D Rect2D::unite(const Rect2D& rect)
[7915]402{
[7914]403#warning implement
[7915]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 */
[7914]412bool Rect2D::contains(const Rect2D& rect) const
[7915]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 */
[7914]424bool Rect2D::contains(const Vector2D& point) const
[7915]425{
426  return (this->top() <= point.y &&
427          this->bottom() >= point.y &&
428          this->left() <= point.x &&
429          this->right() >= point.x);
430}
[7914]431
[7915]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 */
[7914]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.