[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 | */ |
---|
| 36 | Rect2D::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] | 47 | Rect2D::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 | */ |
---|
| 57 | Rect2D::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] | 67 | Rect2D& 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 | |
---|
| 74 | bool Rect2D::operator==(const Rect2D& rect) const |
---|
| 75 | { |
---|
| 76 | return (this->_topLeft == rect._topLeft && |
---|
| 77 | this->_bottomRight == rect._bottomRight); |
---|
| 78 | } |
---|
| 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 | Rect2D Rect2D::operator+(const Rect2D& rect) const |
---|
| 88 | { |
---|
| 89 | return Rect2D(this->_topLeft + rect._topLeft, |
---|
| 90 | this->_bottomRight + rect._bottomRight); |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | |
---|
| 94 | Rect2D& Rect2D::operator+=(const Rect2D& rect) |
---|
| 95 | { |
---|
| 96 | this->_topLeft += rect._topLeft; |
---|
| 97 | this->_bottomRight += rect._bottomRight; |
---|
| 98 | return *this; |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | |
---|
| 102 | Rect2D Rect2D::operator-(const Rect2D& rect) const |
---|
| 103 | { |
---|
| 104 | return Rect2D(this->_topLeft - rect._topLeft, |
---|
| 105 | this->_bottomRight - rect._bottomRight); |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | Rect2D& Rect2D::operator-=(const Rect2D& rect) |
---|
| 109 | { |
---|
| 110 | this->_topLeft -= rect._topLeft; |
---|
| 111 | this->_bottomRight -= rect._bottomRight; |
---|
| 112 | return *this; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | Rect2D Rect2D::operator&(const Rect2D& rect) const |
---|
| 116 | { |
---|
| 117 | return this->intersection(rect); |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | Rect2D& Rect2D::operator&=(const Rect2D& rect) |
---|
| 121 | { |
---|
| 122 | *this = this->intersection(rect); |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | Rect2D Rect2D::operator*(const Vector2D& scaling) const |
---|
| 126 | { |
---|
| 127 | #warning implements this... |
---|
| 128 | //this->scale(scaling); |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | |
---|
| 132 | void Rect2D::setWidth(float width) |
---|
| 133 | { |
---|
| 134 | this->_bottomRight.x = this->_topLeft.x + width; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | |
---|
| 138 | void Rect2D::setHeight(float height) |
---|
| 139 | { |
---|
| 140 | this->_bottomRight.y = this->_topLeft.y + height; |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | void Rect2D::setSize(float width, float height) |
---|
| 144 | { |
---|
| 145 | this->_bottomRight = this->_topLeft + Vector2D(width, height); |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | void Rect2D::setSize(const Vector2D& size) |
---|
| 149 | { |
---|
| 150 | this->_bottomRight = this->_topLeft + size; |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | void Rect2D::setCenter(const Vector2D& center) |
---|
| 154 | { |
---|
| 155 | this->move(center - this->center()); |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | void Rect2D::scaleX(float x) |
---|
| 159 | { |
---|
| 160 | this->_bottomRight.x = this->_topLeft.x + this->width() * x; |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | void Rect2D::scaleY(float y) |
---|
| 164 | { |
---|
| 165 | this->_bottomRight.y = this->_topLeft.y + this->height() * y; |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | void Rect2D::scale(float x, float y) |
---|
| 169 | { |
---|
| 170 | this->scale(Vector2D(x,y)); |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | void Rect2D::scale(const Vector2D& v) |
---|
| 174 | { |
---|
| 175 | this->_bottomRight = this->_topLeft + this->size().internalMultipy(v); |
---|
| 176 | |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | void Rect2D::scaleCentered(float x, float y) |
---|
| 180 | { |
---|
| 181 | this->scaleCentered(Vector2D(x, y)); |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | void Rect2D::scaleCentered(const Vector2D& v) |
---|
| 185 | { |
---|
| 186 | #warning implement this |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | void Rect2D::moveX(float x) |
---|
| 190 | { |
---|
| 191 | this->_topLeft.x += x; |
---|
| 192 | this->_bottomRight.x += x; |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | void Rect2D::moveY(float y) |
---|
| 196 | { |
---|
| 197 | this->_topLeft.y += y; |
---|
| 198 | this->_bottomRight.y += y; |
---|
| 199 | } |
---|
| 200 | void Rect2D::move(const Vector2D& v) |
---|
| 201 | { |
---|
| 202 | this->_topLeft += v; |
---|
| 203 | this->_bottomRight += v; |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | void Rect2D::swapTopBottom() |
---|
| 207 | { |
---|
| 208 | float tmp = this->top(); |
---|
| 209 | this->setTop(this->bottom()); |
---|
| 210 | this->setBottom(tmp); |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | void Rect2D::swapLeftRight() |
---|
| 214 | { |
---|
| 215 | float tmp = this->left(); |
---|
| 216 | this->setLeft(this->right()); |
---|
| 217 | this->setRight(tmp); |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | |
---|
| 221 | void Rect2D::normalize() |
---|
| 222 | { |
---|
| 223 | if (this->top() > this->bottom()) |
---|
| 224 | this->swapTopBottom(); |
---|
| 225 | if (this->left() > this->right()) |
---|
| 226 | this->swapLeftRight(); |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | Rect2D Rect2D::normalized() const |
---|
| 230 | { |
---|
| 231 | Rect2D norm(*this); |
---|
| 232 | norm.normalize(); |
---|
| 233 | return norm; |
---|
| 234 | } |
---|
| 235 | |
---|
| 236 | |
---|
| 237 | Rect2D Rect2D::intersection(const Rect2D& intersector) const |
---|
| 238 | { |
---|
| 239 | #warning implement |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | bool Rect2D::intersects(const Rect2D& intersector) const |
---|
| 243 | { |
---|
| 244 | #warning implement |
---|
| 245 | |
---|
| 246 | } |
---|
| 247 | Rect2D Rect2D::unite(const Rect2D& rect) |
---|
| 248 | { |
---|
| 249 | #warning implement |
---|
| 250 | } |
---|
| 251 | bool Rect2D::contains(const Rect2D& rect) const |
---|
| 252 | { |
---|
| 253 | return (this->top() <= rect.top() && |
---|
| 254 | this->bottom() >= rect.bottom() && |
---|
| 255 | this->left() <= rect.left() && |
---|
| 256 | this->right() >= rect.right()); |
---|
| 257 | } |
---|
| 258 | bool Rect2D::contains(const Vector2D& point) const |
---|
| 259 | { |
---|
| 260 | return (this->top() <= point.y && |
---|
| 261 | this->bottom() >= point.y && |
---|
| 262 | this->left() <= point.x && |
---|
| 263 | this->right() >= point.x); |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | const Rect2D& Rect2D::slerp(const Rect2D& rect, float value) |
---|
| 267 | { |
---|
| 268 | this->_topLeft.slerp(rect._topLeft, value); |
---|
| 269 | this->_bottomRight.slerp(rect._bottomRight, value); |
---|
| 270 | |
---|
| 271 | return *this; |
---|
| 272 | } |
---|
| 273 | |
---|