2
0

Vector2.cpp 3.2 KB

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