Vector2.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #pragma once
  2. #include <cmath>
  3. #include "base.h"
  4. namespace msdfgen {
  5. /**
  6. * A 2-dimensional euclidean floating-point vector.
  7. * @author Viktor Chlumsky
  8. */
  9. struct Vector2 {
  10. double x, y;
  11. inline Vector2(double val = 0) : x(val), y(val) { }
  12. inline Vector2(double x, double y) : x(x), y(y) { }
  13. /// Sets the vector to zero.
  14. inline void reset() {
  15. x = 0, y = 0;
  16. }
  17. /// Sets individual elements of the vector.
  18. inline void set(double newX, double newY) {
  19. x = newX, y = newY;
  20. }
  21. /// Returns the vector's squared length.
  22. inline double squaredLength() const {
  23. return x*x+y*y;
  24. }
  25. /// Returns the vector's length.
  26. inline double length() const {
  27. return sqrt(x*x+y*y);
  28. }
  29. /// Returns the normalized vector - one that has the same direction but unit length.
  30. inline Vector2 normalize(bool allowZero = false) const {
  31. if (double len = length())
  32. return Vector2(x/len, y/len);
  33. return Vector2(0, !allowZero);
  34. }
  35. /// Returns a vector with the same length that is orthogonal to this one.
  36. inline Vector2 getOrthogonal(bool polarity = true) const {
  37. return polarity ? Vector2(-y, x) : Vector2(y, -x);
  38. }
  39. /// Returns a vector with unit length that is orthogonal to this one.
  40. inline Vector2 getOrthonormal(bool polarity = true, bool allowZero = false) const {
  41. if (double len = length())
  42. return polarity ? Vector2(-y/len, x/len) : Vector2(y/len, -x/len);
  43. return polarity ? Vector2(0, !allowZero) : Vector2(0, -!allowZero);
  44. }
  45. #ifdef MSDFGEN_USE_CPP11
  46. inline explicit operator bool() const {
  47. return x || y;
  48. }
  49. #else
  50. inline operator const void *() const {
  51. return x || y ? this : NULL;
  52. }
  53. #endif
  54. inline Vector2 &operator+=(const Vector2 other) {
  55. x += other.x, y += other.y;
  56. return *this;
  57. }
  58. inline Vector2 &operator-=(const Vector2 other) {
  59. x -= other.x, y -= other.y;
  60. return *this;
  61. }
  62. inline Vector2 &operator*=(const Vector2 other) {
  63. x *= other.x, y *= other.y;
  64. return *this;
  65. }
  66. inline Vector2 &operator/=(const Vector2 other) {
  67. x /= other.x, y /= other.y;
  68. return *this;
  69. }
  70. inline Vector2 &operator*=(double value) {
  71. x *= value, y *= value;
  72. return *this;
  73. }
  74. inline Vector2 &operator/=(double value) {
  75. x /= value, y /= value;
  76. return *this;
  77. }
  78. };
  79. /// A vector may also represent a point, which shall be differentiated semantically using the alias Point2.
  80. typedef Vector2 Point2;
  81. /// Dot product of two vectors.
  82. inline double dotProduct(const Vector2 a, const Vector2 b) {
  83. return a.x*b.x+a.y*b.y;
  84. }
  85. /// A special version of the cross product for 2D vectors (returns scalar value).
  86. inline double crossProduct(const Vector2 a, const Vector2 b) {
  87. return a.x*b.y-a.y*b.x;
  88. }
  89. inline bool operator==(const Vector2 a, const Vector2 b) {
  90. return a.x == b.x && a.y == b.y;
  91. }
  92. inline bool operator!=(const Vector2 a, const Vector2 b) {
  93. return a.x != b.x || a.y != b.y;
  94. }
  95. inline Vector2 operator+(const Vector2 v) {
  96. return v;
  97. }
  98. inline Vector2 operator-(const Vector2 v) {
  99. return Vector2(-v.x, -v.y);
  100. }
  101. inline bool operator!(const Vector2 v) {
  102. return !v.x && !v.y;
  103. }
  104. inline Vector2 operator+(const Vector2 a, const Vector2 b) {
  105. return Vector2(a.x+b.x, a.y+b.y);
  106. }
  107. inline Vector2 operator-(const Vector2 a, const Vector2 b) {
  108. return Vector2(a.x-b.x, a.y-b.y);
  109. }
  110. inline Vector2 operator*(const Vector2 a, const Vector2 b) {
  111. return Vector2(a.x*b.x, a.y*b.y);
  112. }
  113. inline Vector2 operator/(const Vector2 a, const Vector2 b) {
  114. return Vector2(a.x/b.x, a.y/b.y);
  115. }
  116. inline Vector2 operator*(double a, const Vector2 b) {
  117. return Vector2(a*b.x, a*b.y);
  118. }
  119. inline Vector2 operator/(double a, const Vector2 b) {
  120. return Vector2(a/b.x, a/b.y);
  121. }
  122. inline Vector2 operator*(const Vector2 a, double b) {
  123. return Vector2(a.x*b, a.y*b);
  124. }
  125. inline Vector2 operator/(const Vector2 a, double b) {
  126. return Vector2(a.x/b, a.y/b);
  127. }
  128. }