Vector2.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 angle of the vector in radians (atan2).
  21. double direction() const;
  22. /// Returns the normalized vector - one that has the same direction but unit length.
  23. Vector2 normalize(bool allowZero = false) const;
  24. /// Returns a vector with the same length that is orthogonal to this one.
  25. Vector2 getOrthogonal(bool polarity = true) const;
  26. /// Returns a vector with unit length that is orthogonal to this one.
  27. Vector2 getOrthonormal(bool polarity = true, bool allowZero = false) const;
  28. /// Returns a vector projected along this one.
  29. Vector2 project(const Vector2 &vector, bool positive = false) const;
  30. operator const void *() const;
  31. bool operator!() const;
  32. bool operator==(const Vector2 &other) const;
  33. bool operator!=(const Vector2 &other) const;
  34. Vector2 operator+() const;
  35. Vector2 operator-() const;
  36. Vector2 operator+(const Vector2 &other) const;
  37. Vector2 operator-(const Vector2 &other) const;
  38. Vector2 operator*(const Vector2 &other) const;
  39. Vector2 operator/(const Vector2 &other) const;
  40. Vector2 operator*(double value) const;
  41. Vector2 operator/(double value) const;
  42. Vector2 & operator+=(const Vector2 &other);
  43. Vector2 & operator-=(const Vector2 &other);
  44. Vector2 & operator*=(const Vector2 &other);
  45. Vector2 & operator/=(const Vector2 &other);
  46. Vector2 & operator*=(double value);
  47. Vector2 & operator/=(double value);
  48. /// Dot product of two vectors.
  49. friend double dotProduct(const Vector2 &a, const Vector2 &b);
  50. /// A special version of the cross product for 2D vectors (returns scalar value).
  51. friend double crossProduct(const Vector2 &a, const Vector2 &b);
  52. friend Vector2 operator*(double value, const Vector2 &vector);
  53. friend Vector2 operator/(double value, const Vector2 &vector);
  54. };
  55. /// A vector may also represent a point, which shall be differentiated semantically using the alias Point2.
  56. typedef Vector2 Point2;
  57. }