Vector2.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #include "Vector2.h"
  2. #include <cstddef>
  3. #include <cmath>
  4. namespace msdfgen {
  5. Vector2::Vector2(double val) : x(val), y(val) { }
  6. Vector2::Vector2(double x, double y) : x(x), y(y) { }
  7. void Vector2::reset() {
  8. x = 0, y = 0;
  9. }
  10. void Vector2::set(double x, double y) {
  11. Vector2::x = x, Vector2::y = y;
  12. }
  13. double Vector2::length() const {
  14. return sqrt(x*x+y*y);
  15. }
  16. double Vector2::direction() const {
  17. return atan2(y, x);
  18. }
  19. Vector2 Vector2::normalize(bool allowZero) const {
  20. double len = length();
  21. if (len == 0)
  22. return Vector2(0, !allowZero);
  23. return Vector2(x/len, y/len);
  24. }
  25. Vector2 Vector2::getOrthogonal(bool polarity) const {
  26. return polarity ? Vector2(-y, x) : Vector2(y, -x);
  27. }
  28. Vector2 Vector2::getOrthonormal(bool polarity, bool allowZero) const {
  29. double len = length();
  30. if (len == 0)
  31. return polarity ? Vector2(0, !allowZero) : Vector2(0, -!allowZero);
  32. return polarity ? Vector2(-y/len, x/len) : Vector2(y/len, -x/len);
  33. }
  34. Vector2 Vector2::project(const Vector2 &vector, bool positive) const {
  35. Vector2 n = normalize(true);
  36. double t = dotProduct(vector, n);
  37. if (positive && t <= 0)
  38. return Vector2();
  39. return t*n;
  40. }
  41. Vector2::operator const void*() const {
  42. return x || y ? this : NULL;
  43. }
  44. bool Vector2::operator!() const {
  45. return !x && !y;
  46. }
  47. bool Vector2::operator==(const Vector2 &other) const {
  48. return x == other.x && y == other.y;
  49. }
  50. bool Vector2::operator!=(const Vector2 &other) const {
  51. return x != other.x || y != other.y;
  52. }
  53. Vector2 Vector2::operator+() const {
  54. return *this;
  55. }
  56. Vector2 Vector2::operator-() const {
  57. return Vector2(-x, -y);
  58. }
  59. Vector2 Vector2::operator+(const Vector2 &other) const {
  60. return Vector2(x+other.x, y+other.y);
  61. }
  62. Vector2 Vector2::operator-(const Vector2 &other) const {
  63. return Vector2(x-other.x, y-other.y);
  64. }
  65. Vector2 Vector2::operator*(const Vector2 &other) const {
  66. return Vector2(x*other.x, y*other.y);
  67. }
  68. Vector2 Vector2::operator/(const Vector2 &other) const {
  69. return Vector2(x/other.x, y/other.y);
  70. }
  71. Vector2 Vector2::operator*(double value) const {
  72. return Vector2(x*value, y*value);
  73. }
  74. Vector2 Vector2::operator/(double value) const {
  75. return Vector2(x/value, y/value);
  76. }
  77. Vector2 & Vector2::operator+=(const Vector2 &other) {
  78. x += other.x, y += other.y;
  79. return *this;
  80. }
  81. Vector2 & Vector2::operator-=(const Vector2 &other) {
  82. x -= other.x, y -= other.y;
  83. return *this;
  84. }
  85. Vector2 & Vector2::operator*=(const Vector2 &other) {
  86. x *= other.x, y *= other.y;
  87. return *this;
  88. }
  89. Vector2 & Vector2::operator/=(const Vector2 &other) {
  90. x /= other.x, y /= other.y;
  91. return *this;
  92. }
  93. Vector2 & Vector2::operator*=(double value) {
  94. x *= value, y *= value;
  95. return *this;
  96. }
  97. Vector2 & Vector2::operator/=(double value) {
  98. x /= value, y /= value;
  99. return *this;
  100. }
  101. double dotProduct(const Vector2 &a, const Vector2 &b) {
  102. return a.x*b.x+a.y*b.y;
  103. }
  104. double crossProduct(const Vector2 &a, const Vector2 &b) {
  105. return a.x*b.y-a.y*b.x;
  106. }
  107. Vector2 operator*(double value, const Vector2 &vector) {
  108. return Vector2(value*vector.x, value*vector.y);
  109. }
  110. Vector2 operator/(double value, const Vector2 &vector) {
  111. return Vector2(value/vector.x, value/vector.y);
  112. }
  113. }