| 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 | */ |
|---|
| 36 | Rect2D::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 | */ |
|---|
| 48 | Rect2D::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 | */ |
|---|
| 58 | Rect2D::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 | */ |
|---|
| 68 | Rect2D& 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 | */ |
|---|
| 80 | bool 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 | */ |
|---|
| 91 | bool 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 | */ |
|---|
| 105 | Rect2D 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 | */ |
|---|
| 117 | Rect2D& 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 | */ |
|---|
| 130 | Rect2D 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 | */ |
|---|
| 141 | Rect2D& 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 | */ |
|---|
| 154 | Rect2D 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 | */ |
|---|
| 164 | Rect2D& Rect2D::operator&=(const Rect2D& rect) |
|---|
| 165 | { |
|---|
| 166 | *this = this->intersection(rect); |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | /** |
|---|
| 170 | * TODO coment/implement |
|---|
| 171 | */ |
|---|
| 172 | Rect2D 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 | */ |
|---|
| 184 | void 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 | */ |
|---|
| 196 | void 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 | */ |
|---|
| 207 | void 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 | */ |
|---|
| 218 | void 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 | */ |
|---|
| 228 | void Rect2D::setCenter(const Vector2D& center) |
|---|
| 229 | { |
|---|
| 230 | this->move(center - this->center()); |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | /** |
|---|
| 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 | */ |
|---|
| 239 | void 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 | */ |
|---|
| 249 | void 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 | */ |
|---|
| 259 | void Rect2D::setCenterY(float y) |
|---|
| 260 | { |
|---|
| 261 | #warning implement this |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | /** |
|---|
| 265 | * @brief scales the Rectangle from topLeft out. |
|---|
| 266 | * @param x: the scale factor in x direction |
|---|
| 267 | */ |
|---|
| 268 | void Rect2D::scaleX(float x) |
|---|
| 269 | { |
|---|
| 270 | this->_bottomRight.x = this->_topLeft.x + this->width() * x; |
|---|
| 271 | } |
|---|
| 272 | |
|---|
| 273 | /** |
|---|
| 274 | * @brief scales the Rectangle from topLeft out. |
|---|
| 275 | * @param y: the scale factor in y direction |
|---|
| 276 | */ |
|---|
| 277 | void Rect2D::scaleY(float y) |
|---|
| 278 | { |
|---|
| 279 | this->_bottomRight.y = this->_topLeft.y + this->height() * y; |
|---|
| 280 | } |
|---|
| 281 | |
|---|
| 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 | */ |
|---|
| 287 | void Rect2D::scale(float x, float y) |
|---|
| 288 | { |
|---|
| 289 | this->scale(Vector2D(x,y)); |
|---|
| 290 | } |
|---|
| 291 | |
|---|
| 292 | /** |
|---|
| 293 | * @brief scales the Rectangle from topLeft out. |
|---|
| 294 | * @param v: the scale factors. |
|---|
| 295 | */ |
|---|
| 296 | void Rect2D::scale(const Vector2D& v) |
|---|
| 297 | { |
|---|
| 298 | this->_bottomRight = this->_topLeft + this->size().internalMultipy(v); |
|---|
| 299 | |
|---|
| 300 | } |
|---|
| 301 | |
|---|
| 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 | */ |
|---|
| 307 | void Rect2D::scaleCentered(float x, float y) |
|---|
| 308 | { |
|---|
| 309 | this->scaleCentered(Vector2D(x, y)); |
|---|
| 310 | } |
|---|
| 311 | |
|---|
| 312 | |
|---|
| 313 | /** |
|---|
| 314 | * @brief scales the Rectangle from the Center out. |
|---|
| 315 | * @param v: the scale factors. |
|---|
| 316 | */ |
|---|
| 317 | void Rect2D::scaleCentered(const Vector2D& v) |
|---|
| 318 | { |
|---|
| 319 | #warning implement this |
|---|
| 320 | } |
|---|
| 321 | |
|---|
| 322 | /** |
|---|
| 323 | * @brief moves the Rectangle |
|---|
| 324 | * @param x the amount to move in x. |
|---|
| 325 | */ |
|---|
| 326 | void Rect2D::moveX(float x) |
|---|
| 327 | { |
|---|
| 328 | this->_topLeft.x += x; |
|---|
| 329 | this->_bottomRight.x += x; |
|---|
| 330 | } |
|---|
| 331 | |
|---|
| 332 | /** |
|---|
| 333 | * @brief moves the Rectangle |
|---|
| 334 | * @param y the amount to move in y. |
|---|
| 335 | */ |
|---|
| 336 | void Rect2D::moveY(float y) |
|---|
| 337 | { |
|---|
| 338 | this->_topLeft.y += y; |
|---|
| 339 | this->_bottomRight.y += y; |
|---|
| 340 | } |
|---|
| 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 | */ |
|---|
| 347 | void 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 | */ |
|---|
| 356 | void Rect2D::move(const Vector2D& v) |
|---|
| 357 | { |
|---|
| 358 | this->_topLeft += v; |
|---|
| 359 | this->_bottomRight += v; |
|---|
| 360 | } |
|---|
| 361 | |
|---|
| 362 | /** |
|---|
| 363 | * @brief swaps top and bottom. |
|---|
| 364 | */ |
|---|
| 365 | void Rect2D::swapTopBottom() |
|---|
| 366 | { |
|---|
| 367 | float tmp = this->top(); |
|---|
| 368 | this->setTop(this->bottom()); |
|---|
| 369 | this->setBottom(tmp); |
|---|
| 370 | } |
|---|
| 371 | |
|---|
| 372 | /** |
|---|
| 373 | * @brief swaps left and right. |
|---|
| 374 | */ |
|---|
| 375 | void Rect2D::swapLeftRight() |
|---|
| 376 | { |
|---|
| 377 | float tmp = this->left(); |
|---|
| 378 | this->setLeft(this->right()); |
|---|
| 379 | this->setRight(tmp); |
|---|
| 380 | } |
|---|
| 381 | |
|---|
| 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 | */ |
|---|
| 387 | void Rect2D::normalize() |
|---|
| 388 | { |
|---|
| 389 | if (this->top() > this->bottom()) |
|---|
| 390 | this->swapTopBottom(); |
|---|
| 391 | if (this->left() > this->right()) |
|---|
| 392 | this->swapLeftRight(); |
|---|
| 393 | } |
|---|
| 394 | |
|---|
| 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 | */ |
|---|
| 401 | Rect2D Rect2D::normalized() const |
|---|
| 402 | { |
|---|
| 403 | Rect2D norm(*this); |
|---|
| 404 | norm.normalize(); |
|---|
| 405 | return norm; |
|---|
| 406 | } |
|---|
| 407 | |
|---|
| 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 | */ |
|---|
| 413 | Rect2D Rect2D::intersection(const Rect2D& intersector) const |
|---|
| 414 | { |
|---|
| 415 | #warning implement |
|---|
| 416 | } |
|---|
| 417 | |
|---|
| 418 | /** |
|---|
| 419 | * @brief checks if the intersection of two Rectangles is not empty. |
|---|
| 420 | */ |
|---|
| 421 | bool Rect2D::intersects(const Rect2D& intersector) const |
|---|
| 422 | { |
|---|
| 423 | #warning implement |
|---|
| 424 | |
|---|
| 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 | */ |
|---|
| 432 | Rect2D Rect2D::unite(const Rect2D& rect) |
|---|
| 433 | { |
|---|
| 434 | #warning implement |
|---|
| 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 | */ |
|---|
| 443 | bool Rect2D::contains(const Rect2D& rect) const |
|---|
| 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 | */ |
|---|
| 455 | bool Rect2D::contains(const Vector2D& point) const |
|---|
| 456 | { |
|---|
| 457 | return (this->top() <= point.y && |
|---|
| 458 | this->bottom() >= point.y && |
|---|
| 459 | this->left() <= point.x && |
|---|
| 460 | this->right() >= point.x); |
|---|
| 461 | } |
|---|
| 462 | |
|---|
| 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 | */ |
|---|
| 469 | const 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 | |
|---|