| 1 | /* |
|---|
| 2 | ----------------------------------------------------------------------------- |
|---|
| 3 | This source file is part of LEXIExporter |
|---|
| 4 | |
|---|
| 5 | Copyright 2006 NDS Limited |
|---|
| 6 | |
|---|
| 7 | Author(s): |
|---|
| 8 | Bo Krohn |
|---|
| 9 | |
|---|
| 10 | This program is free software; you can redistribute it and/or modify it under |
|---|
| 11 | the terms of the GNU Lesser General Public License as published by the Free Software |
|---|
| 12 | Foundation; either version 2 of the License, or (at your option) any later |
|---|
| 13 | version. |
|---|
| 14 | |
|---|
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT |
|---|
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|---|
| 17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
|---|
| 18 | |
|---|
| 19 | You should have received a copy of the GNU Lesser General Public License along with |
|---|
| 20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
|---|
| 21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to |
|---|
| 22 | http://www.gnu.org/copyleft/lesser.txt. |
|---|
| 23 | ----------------------------------------------------------------------------- |
|---|
| 24 | */ |
|---|
| 25 | |
|---|
| 26 | //@£$sdk "Vector2 Class" |
|---|
| 27 | |
|---|
| 28 | #ifndef __Accuro_Vector2__ |
|---|
| 29 | #define __Accuro_Vector2__ |
|---|
| 30 | |
|---|
| 31 | #pragma once |
|---|
| 32 | |
|---|
| 33 | // |
|---|
| 34 | |
|---|
| 35 | class CVec2 { |
|---|
| 36 | |
|---|
| 37 | friend class CMatrix; |
|---|
| 38 | |
|---|
| 39 | public: |
|---|
| 40 | |
|---|
| 41 | static const CVec2 _zero; |
|---|
| 42 | static const CVec2 _x; |
|---|
| 43 | static const CVec2 _y; |
|---|
| 44 | |
|---|
| 45 | // Constructors/Destructor |
|---|
| 46 | inline CVec2() |
|---|
| 47 | { |
|---|
| 48 | } |
|---|
| 49 | inline CVec2(const CVec2& v) |
|---|
| 50 | { |
|---|
| 51 | x=v.x; |
|---|
| 52 | y=v.y; |
|---|
| 53 | } |
|---|
| 54 | inline CVec2(float fX, float fY) |
|---|
| 55 | { |
|---|
| 56 | x=fX; |
|---|
| 57 | y=fY; |
|---|
| 58 | } |
|---|
| 59 | inline CVec2(const float* v) |
|---|
| 60 | { |
|---|
| 61 | x=v[0]; |
|---|
| 62 | y=v[1]; |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | // Direct access to vector |
|---|
| 66 | float x, y; |
|---|
| 67 | |
|---|
| 68 | // Assignment |
|---|
| 69 | inline CVec2& operator = (const CVec2& v) |
|---|
| 70 | { |
|---|
| 71 | x=v.x; |
|---|
| 72 | y=v.y; |
|---|
| 73 | |
|---|
| 74 | return *this; |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | // Comparing |
|---|
| 78 | inline bool operator == (const CVec2& v) const |
|---|
| 79 | { |
|---|
| 80 | return (x==v.x && y==v.y); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | inline bool operator != (const CVec2& v) const |
|---|
| 84 | { |
|---|
| 85 | return (x!=v.x || y!=v.y); |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | // Operators |
|---|
| 89 | inline CVec2 CVec2::operator - () const |
|---|
| 90 | { |
|---|
| 91 | return CVec2(-x, -y); |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | inline CVec2 CVec2::operator + (const CVec2& v) const |
|---|
| 95 | { |
|---|
| 96 | return CVec2(x+v.x, y+v.y); |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | inline CVec2 CVec2::operator - (const CVec2& v) const |
|---|
| 100 | { |
|---|
| 101 | return CVec2(x-v.x, y-v.y); |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | inline CVec2 CVec2::operator * (const CVec2& v) const |
|---|
| 105 | { |
|---|
| 106 | return CVec2(x*v.x, y*v.y); |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | inline CVec2 CVec2::operator / (const CVec2& v) const |
|---|
| 110 | { |
|---|
| 111 | return CVec2(x/v.x, y/v.y); |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | inline void add(const CVec2& v) |
|---|
| 115 | { |
|---|
| 116 | x += v.x; |
|---|
| 117 | y += v.y; |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | inline void add(const CVec2& v1, const CVec2& v2) |
|---|
| 121 | { |
|---|
| 122 | x = v1.x + v2.x; |
|---|
| 123 | y = v1.y + v2.y; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | inline CVec2& CVec2::operator += (const CVec2& v) |
|---|
| 127 | { |
|---|
| 128 | x += v.x; |
|---|
| 129 | y += v.y; |
|---|
| 130 | return *this; |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | inline void subtract(const CVec2& v) |
|---|
| 134 | { |
|---|
| 135 | x += v.x; |
|---|
| 136 | y += v.y; |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | inline void subtract(const CVec2& v1, const CVec2& v2) |
|---|
| 140 | { |
|---|
| 141 | x = v1.x - v2.x; |
|---|
| 142 | y = v1.y - v2.y; |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | inline CVec2& CVec2::operator -= (const CVec2& v) |
|---|
| 146 | { |
|---|
| 147 | x -= v.x; |
|---|
| 148 | y -= v.y; |
|---|
| 149 | return *this; |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | inline CVec2& CVec2::operator *= (const CVec2& v) |
|---|
| 153 | { |
|---|
| 154 | x *= v.x; |
|---|
| 155 | y *= v.y; |
|---|
| 156 | return *this; |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | inline CVec2& CVec2::operator /= (const CVec2& v) |
|---|
| 160 | { |
|---|
| 161 | x /= v.x; |
|---|
| 162 | y /= v.y; |
|---|
| 163 | return *this; |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | inline CVec2 CVec2::operator * (float r) const |
|---|
| 167 | { |
|---|
| 168 | return CVec2(r*x, r*y); |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | inline void multiply(const CVec2& v1, const CVec2& v2) |
|---|
| 172 | { |
|---|
| 173 | x = v1.x * v2.x; |
|---|
| 174 | y = v1.y * v2.y; |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | inline void multiply(const CVec2& v) |
|---|
| 178 | { |
|---|
| 179 | x *= v.x; |
|---|
| 180 | y *= v.y; |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | inline void negate(const CVec2& v) |
|---|
| 184 | { |
|---|
| 185 | x = -v.x; |
|---|
| 186 | y = -v.y; |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | inline void negate() |
|---|
| 190 | { |
|---|
| 191 | x = -x; |
|---|
| 192 | y = -y; |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | inline float dot(const CVec2& v) const |
|---|
| 196 | { |
|---|
| 197 | return x * v.x + y * v.y; |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | inline void scale(float scalar, const CVec2& v) |
|---|
| 201 | { |
|---|
| 202 | x = scalar * v.x; |
|---|
| 203 | y = scalar * v.y; |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | inline void scale(float scalar) |
|---|
| 207 | { |
|---|
| 208 | x *= scalar; |
|---|
| 209 | y *= scalar; |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | inline void addScaled(const CVec2& v1, float scalar, const CVec2& v2) |
|---|
| 213 | { |
|---|
| 214 | x = v1.x + scalar * v2.x; |
|---|
| 215 | y = v1.y + scalar * v2.y; |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | inline void addScaled(float scalar, const CVec2& v) |
|---|
| 219 | { |
|---|
| 220 | x += scalar * v.x; |
|---|
| 221 | y += scalar * v.y; |
|---|
| 222 | } |
|---|
| 223 | |
|---|
| 224 | inline void combine(float scalar1, const CVec2& v1, float scalar2, const CVec2& v2) |
|---|
| 225 | { |
|---|
| 226 | x = scalar1 * v1.x + scalar2 * v2.x; |
|---|
| 227 | y = scalar1 * v1.y + scalar2 * v2.y; |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | inline void normalize() |
|---|
| 231 | { |
|---|
| 232 | float t = length(); |
|---|
| 233 | scale(1.0f / t); |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | inline void perpendicular(const CVec2& v) |
|---|
| 237 | { |
|---|
| 238 | float t = v.x; |
|---|
| 239 | x = -v.y; |
|---|
| 240 | y = t; |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | inline void perpendicular() |
|---|
| 244 | { |
|---|
| 245 | float t = x; |
|---|
| 246 | x = -y; |
|---|
| 247 | y = t; |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | inline float length2() const |
|---|
| 251 | { |
|---|
| 252 | return F_Square(x) + F_Square(y); |
|---|
| 253 | } |
|---|
| 254 | |
|---|
| 255 | inline float length() const |
|---|
| 256 | { |
|---|
| 257 | return sqrtf(length2()); |
|---|
| 258 | } |
|---|
| 259 | |
|---|
| 260 | inline float distance2(const CVec2& v) const |
|---|
| 261 | { |
|---|
| 262 | return (F_Square(x - v.x) + F_Square(y - v.y)); |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | inline float distance(const CVec2& v) const |
|---|
| 266 | { |
|---|
| 267 | return sqrtf(distance2(v)); |
|---|
| 268 | } |
|---|
| 269 | |
|---|
| 270 | inline void lerp(const float& scalar, const CVec2& v1, const CVec2& v2) |
|---|
| 271 | { |
|---|
| 272 | x = v1.x + scalar * (v2.x - v1.x); |
|---|
| 273 | y = v1.y + scalar * (v2.y - v1.y); |
|---|
| 274 | } |
|---|
| 275 | |
|---|
| 276 | inline float angle(const CVec2& v) const |
|---|
| 277 | { |
|---|
| 278 | return acosf(dot(v) / (length() * v.length())); |
|---|
| 279 | } |
|---|
| 280 | |
|---|
| 281 | inline CVec2 CVec2::operator / (float r) const |
|---|
| 282 | { |
|---|
| 283 | float ri = 1.0f/r; |
|---|
| 284 | return CVec2(ri*x, ri*y); |
|---|
| 285 | } |
|---|
| 286 | |
|---|
| 287 | inline CVec2& CVec2::operator *= (float r) |
|---|
| 288 | { |
|---|
| 289 | x *= r; |
|---|
| 290 | y *= r; |
|---|
| 291 | return *this; |
|---|
| 292 | } |
|---|
| 293 | |
|---|
| 294 | inline CVec2& CVec2::operator /= (float r) |
|---|
| 295 | { |
|---|
| 296 | float ri = 1.0f/r; |
|---|
| 297 | x *= ri; |
|---|
| 298 | y *= ri; |
|---|
| 299 | return *this; |
|---|
| 300 | } |
|---|
| 301 | |
|---|
| 302 | inline void clamp() |
|---|
| 303 | { |
|---|
| 304 | if(x < 0.0f) x = 0.0f; |
|---|
| 305 | else if(x > 1.0f) x = 1.0f; |
|---|
| 306 | if(y < 0.0f) y = 0.0f; |
|---|
| 307 | else if(y > 1.0f) y = 1.0f; |
|---|
| 308 | } |
|---|
| 309 | |
|---|
| 310 | inline void clamp(float min, float max) |
|---|
| 311 | { |
|---|
| 312 | if(x < min) x = min; |
|---|
| 313 | else if(x > max) x = max; |
|---|
| 314 | if(y < min) y = min; |
|---|
| 315 | else if(y > max) y = max; |
|---|
| 316 | } |
|---|
| 317 | |
|---|
| 318 | inline void zero() |
|---|
| 319 | { |
|---|
| 320 | x = y = 0.0f; |
|---|
| 321 | } |
|---|
| 322 | |
|---|
| 323 | }; |
|---|
| 324 | |
|---|
| 325 | // |
|---|
| 326 | |
|---|
| 327 | #endif // __Accuro_Vector2__ |
|---|