Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7952 was 7919, checked in by bensch, 19 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
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), this->setTop(y);
39  this->setSize(width, height);
40}
41
42/**
43 * @brief creates a new Rectangle with
44 * @param topLeft the top-left corner
45 * @param bottomRight the lower-right corner
46 */
47Rect2D::Rect2D(const Vector2D& topLeft, const Vector2D& bottomRight )
48{
49  this->setTopLeft(topLeft);
50  this->setBottomRight(bottomRight);
51}
52
53/**
54 * @brief creates a new Rectangle with
55 * @param rect the rectangle to copy.
56 */
57Rect2D::Rect2D(const Rect2D& rect)
58{
59  *this = rect;
60}
61
62/**
63 * @brief copies the Values of rect to this rect
64 * @param rect copy the values from.
65 * @returns this reference.
66 */
67Rect2D& Rect2D::operator=(const Rect2D& rect)
68{
69  this->_topLeft = rect._topLeft;
70  this->_bottomRight = rect._bottomRight;
71  return *this;
72}
73
74/**
75 * @brief compares two rectanles.
76 * @param rect the rectangle to compare
77 * @returns true if the two rectangles are the same
78 */
79bool Rect2D::operator==(const Rect2D& rect) const
80{
81  return (this->_topLeft == rect._topLeft &&
82          this->_bottomRight == rect._bottomRight);
83}
84
85/**
86 * @brief negative compares two rectanles.
87 * @param rect the rectangle to compare
88 * @returns true if the two rectangles are different
89 */
90bool Rect2D::operator!=(const Rect2D& rect) const
91{
92  return (this->_topLeft != rect._topLeft ||
93          this->_bottomRight != rect._bottomRight);
94}
95
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 */
104Rect2D Rect2D::operator+(const Rect2D& rect) const
105{
106  return Rect2D(this->_topLeft + rect._topLeft,
107                this->_bottomRight + rect._bottomRight);
108}
109
110
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 */
116Rect2D& Rect2D::operator+=(const Rect2D& rect)
117{
118  this->_topLeft += rect._topLeft;
119  this->_bottomRight += rect._bottomRight;
120  return *this;
121}
122
123
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 */
129Rect2D Rect2D::operator-(const Rect2D& rect) const
130{
131  return Rect2D(this->_topLeft - rect._topLeft,
132                this->_bottomRight - rect._bottomRight);
133}
134
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 */
140Rect2D& Rect2D::operator-=(const Rect2D& rect)
141{
142  this->_topLeft -= rect._topLeft;
143  this->_bottomRight -= rect._bottomRight;
144  return *this;
145}
146
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 */
153Rect2D Rect2D::operator&(const Rect2D& rect) const
154{
155  return this->intersection(rect);
156}
157
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 */
163Rect2D& Rect2D::operator&=(const Rect2D& rect)
164{
165  *this = this->intersection(rect);
166}
167
168/**
169 * TODO coment/implement
170 */
171Rect2D Rect2D::operator*(const Vector2D& scaling) const
172{
173#warning implements this...
174  //this->scale(scaling);
175}
176
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 */
183void Rect2D::setWidth(float width)
184{
185  this->_bottomRight.x = this->_topLeft.x + width;
186}
187
188
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 */
195void Rect2D::setHeight(float height)
196{
197  this->_bottomRight.y = this->_topLeft.y + height;
198}
199
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 */
206void Rect2D::setSize(float width, float height)
207{
208  this->_bottomRight = this->_topLeft + Vector2D(width, height);
209}
210
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 */
217void Rect2D::setSize(const Vector2D& size)
218{
219  this->_bottomRight = this->_topLeft + size;
220}
221
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 */
227void Rect2D::setCenter(const Vector2D& center)
228{
229  this->move(center - this->center());
230}
231
232/**
233 * @brief scales the Rectangle from topLeft out.
234 * @param x: the scale factor in x direction
235 */
236void Rect2D::scaleX(float x)
237{
238  this->_bottomRight.x = this->_topLeft.x + this->width() * x;
239}
240
241/**
242 * @brief scales the Rectangle from topLeft out.
243 * @param y: the scale factor in y direction
244 */
245void Rect2D::scaleY(float y)
246{
247  this->_bottomRight.y = this->_topLeft.y + this->height() * y;
248}
249
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 */
255void Rect2D::scale(float x, float y)
256{
257  this->scale(Vector2D(x,y));
258}
259
260/**
261 * @brief scales the Rectangle from topLeft out.
262 * @param v: the scale factors.
263 */
264void Rect2D::scale(const Vector2D& v)
265{
266  this->_bottomRight = this->_topLeft + this->size().internalMultipy(v);
267
268}
269
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 */
275void Rect2D::scaleCentered(float x, float y)
276{
277  this->scaleCentered(Vector2D(x, y));
278}
279
280
281/**
282 * @brief scales the Rectangle from the Center out.
283 * @param v: the scale factors.
284 */
285void Rect2D::scaleCentered(const Vector2D& v)
286{
287#warning implement this
288}
289
290/**
291 * @brief moves the Rectangle
292 * @param x the amount to move in x.
293 */
294void Rect2D::moveX(float x)
295{
296  this->_topLeft.x += x;
297  this->_bottomRight.x += x;
298}
299
300/**
301 * @brief moves the Rectangle
302 * @param y the amount to move in y.
303 */
304void Rect2D::moveY(float y)
305{
306  this->_topLeft.y += y;
307  this->_bottomRight.y += y;
308}
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 */
324void Rect2D::move(const Vector2D& v)
325{
326  this->_topLeft += v;
327  this->_bottomRight += v;
328}
329
330/**
331 * @brief swaps top and bottom.
332 */
333void Rect2D::swapTopBottom()
334{
335  float tmp = this->top();
336  this->setTop(this->bottom());
337  this->setBottom(tmp);
338}
339
340/**
341 * @brief swaps left and right.
342 */
343void Rect2D::swapLeftRight()
344{
345  float tmp = this->left();
346  this->setLeft(this->right());
347  this->setRight(tmp);
348}
349
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 */
355void Rect2D::normalize()
356{
357  if (this->top() > this->bottom())
358    this->swapTopBottom();
359  if (this->left() > this->right())
360    this->swapLeftRight();
361}
362
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 */
369Rect2D Rect2D::normalized() const
370{
371  Rect2D norm(*this);
372  norm.normalize();
373  return norm;
374}
375
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 */
381Rect2D Rect2D::intersection(const Rect2D& intersector) const
382{
383#warning implement
384}
385
386/**
387 * @brief checks if the intersection of two Rectangles is not empty.
388 */
389bool Rect2D::intersects(const Rect2D& intersector) const
390{
391#warning implement
392
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 */
400Rect2D Rect2D::unite(const Rect2D& rect)
401{
402#warning implement
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 */
411bool Rect2D::contains(const Rect2D& rect) const
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 */
423bool Rect2D::contains(const Vector2D& point) const
424{
425  return (this->top() <= point.y &&
426          this->bottom() >= point.y &&
427          this->left() <= point.x &&
428          this->right() >= point.x);
429}
430
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 */
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.