* A 2-dimensional euclidean floating-point vector. * @author Viktor Chlumsky */
| 11 | * @author Viktor Chlumsky |
| 12 | */ |
| 13 | struct Vector2 { |
| 14 | |
| 15 | double x, y; |
| 16 | |
| 17 | inline Vector2(double val = 0) : x(val), y(val) { } |
| 18 | |
| 19 | inline Vector2(double x, double y) : x(x), y(y) { } |
| 20 | |
| 21 | /// Sets the vector to zero. |
| 22 | inline void reset() { |
| 23 | x = 0, y = 0; |
| 24 | } |
| 25 | |
| 26 | /// Sets individual elements of the vector. |
| 27 | inline void set(double newX, double newY) { |
| 28 | x = newX, y = newY; |
| 29 | } |
| 30 | |
| 31 | /// Returns the vector's squared length. |
| 32 | inline double squaredLength() const { |
| 33 | return x*x+y*y; |
| 34 | } |
| 35 | |
| 36 | /// Returns the vector's length. |
| 37 | inline double length() const { |
| 38 | return sqrt(x*x+y*y); |
| 39 | } |
| 40 | |
| 41 | /// Returns the normalized vector - one that has the same direction but unit length. |
| 42 | inline Vector2 normalize(bool allowZero = false) const { |
| 43 | if (double len = length()) |
| 44 | return Vector2(x/len, y/len); |
| 45 | return Vector2(0, !allowZero); |
| 46 | } |
| 47 | |
| 48 | /// Returns a vector with the same length that is orthogonal to this one. |
| 49 | inline Vector2 getOrthogonal(bool polarity = true) const { |
| 50 | return polarity ? Vector2(-y, x) : Vector2(y, -x); |
| 51 | } |
| 52 | |
| 53 | /// Returns a vector with unit length that is orthogonal to this one. |
| 54 | inline Vector2 getOrthonormal(bool polarity = true, bool allowZero = false) const { |
| 55 | if (double len = length()) |
| 56 | return polarity ? Vector2(-y/len, x/len) : Vector2(y/len, -x/len); |
| 57 | return polarity ? Vector2(0, !allowZero) : Vector2(0, -!allowZero); |
| 58 | } |
| 59 | |
| 60 | #ifdef MSDFGEN_USE_CPP11 |
| 61 | inline explicit operator bool() const { |
| 62 | return x || y; |
| 63 | } |
| 64 | #else |
| 65 | inline operator const void *() const { |
| 66 | return x || y ? this : NULL; |
| 67 | } |
| 68 | #endif |
| 69 | |
| 70 | inline Vector2 &operator+=(const Vector2 other) { |
no outgoing calls
no test coverage detected