Vector2.h 2.5 KB

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