2
0

Vector2.hpp 4.0 KB

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