Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7993 was 7919, checked in by bensch, 18 years ago

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