| 1 | |
| 2 | #pragma once |
| 3 | |
| 4 | #include <cstdlib> |
| 5 | #include <cmath> |
| 6 | |
| 7 | namespace msdfgen { |
| 8 | |
| 9 | /** |
| 10 | * A 2-dimensional euclidean vector with double precision. |
| 11 | * Implementation based on the Vector2 template from Artery Engine. |
| 12 | * @author Viktor Chlumsky |
| 13 | */ |
| 14 | struct Vector2 { |
| 15 | |
| 16 | double x, y; |
| 17 | |
| 18 | Vector2(double val = 0); |
| 19 | Vector2(double x, double y); |
| 20 | /// Sets the vector to zero. |
| 21 | void reset(); |
| 22 | /// Sets individual elements of the vector. |
| 23 | void set(double x, double y); |
| 24 | /// Returns the vector's length. |
| 25 | double length() const; |
| 26 | /// Returns the angle of the vector in radians (atan2). |
| 27 | double direction() const; |
| 28 | /// Returns the normalized vector - one that has the same direction but unit length. |
| 29 | Vector2 normalize(bool allowZero = false) const; |
| 30 | /// Returns a vector with the same length that is orthogonal to this one. |
| 31 | Vector2 getOrthogonal(bool polarity = true) const; |
| 32 | /// Returns a vector with unit length that is orthogonal to this one. |
| 33 | Vector2 getOrthonormal(bool polarity = true, bool allowZero = false) const; |
| 34 | /// Returns a vector projected along this one. |
| 35 | Vector2 project(const Vector2 &vector, bool positive = false) const; |
| 36 | operator const void *() const; |
| 37 | bool operator!() const; |
| 38 | bool operator==(const Vector2 &other) const; |
| 39 | bool operator!=(const Vector2 &other) const; |
| 40 | Vector2 operator+() const; |
| 41 | Vector2 operator-() const; |
| 42 | Vector2 operator+(const Vector2 &other) const; |
| 43 | Vector2 operator-(const Vector2 &other) const; |
| 44 | Vector2 operator*(const Vector2 &other) const; |
| 45 | Vector2 operator/(const Vector2 &other) const; |
| 46 | Vector2 operator*(double value) const; |
| 47 | Vector2 operator/(double value) const; |
| 48 | Vector2 & operator+=(const Vector2 &other); |
| 49 | Vector2 & operator-=(const Vector2 &other); |
| 50 | Vector2 & operator*=(const Vector2 &other); |
| 51 | Vector2 & operator/=(const Vector2 &other); |
| 52 | Vector2 & operator*=(double value); |
| 53 | Vector2 & operator/=(double value); |
| 54 | /// Dot product of two vectors. |
| 55 | friend double dotProduct(const Vector2 &a, const Vector2 &b); |
| 56 | /// A special version of the cross product for 2D vectors (returns scalar value). |
| 57 | friend double crossProduct(const Vector2 &a, const Vector2 &b); |
| 58 | friend Vector2 operator*(double value, const Vector2 &vector); |
| 59 | friend Vector2 operator/(double value, const Vector2 &vector); |
| 60 | |
| 61 | }; |
| 62 | |
| 63 | /// A vector may also represent a point, which shall be differentiated semantically using the alias Point2. |
| 64 | typedef Vector2 Point2; |
| 65 | |
| 66 | } |
| 67 | |