Vector2.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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::squaredLength() const {
  15. return x*x+y*y;
  16. }
  17. double Vector2::direction() const {
  18. return atan2(y, x);
  19. }
  20. Vector2 Vector2::normalize(bool allowZero) const {
  21. double len = length();
  22. if (len == 0)
  23. return Vector2(0, !allowZero);
  24. return Vector2(x/len, y/len);
  25. }
  26. Vector2 Vector2::getOrthogonal(bool polarity) const {
  27. return polarity ? Vector2(-y, x) : Vector2(y, -x);
  28. }
  29. Vector2 Vector2::getOrthonormal(bool polarity, bool allowZero) const {
  30. double len = length();
  31. if (len == 0)
  32. return polarity ? Vector2(0, !allowZero) : Vector2(0, -!allowZero);
  33. return polarity ? Vector2(-y/len, x/len) : Vector2(y/len, -x/len);
  34. }
  35. Vector2 Vector2::project(const Vector2 &vector, bool positive) const {
  36. Vector2 n = normalize(true);
  37. double t = dotProduct(vector, n);
  38. if (positive && t <= 0)
  39. return Vector2();
  40. return t*n;
  41. }
  42. Vector2::operator const void*() const {
  43. return x || y ? this : NULL;
  44. }
  45. bool Vector2::operator!() const {
  46. return !x && !y;
  47. }
  48. bool Vector2::operator==(const Vector2 &other) const {
  49. return x == other.x && y == other.y;
  50. }
  51. bool Vector2::operator!=(const Vector2 &other) const {
  52. return x != other.x || y != other.y;
  53. }
  54. Vector2 Vector2::operator+() const {
  55. return *this;
  56. }
  57. Vector2 Vector2::operator-() const {
  58. return Vector2(-x, -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/(const Vector2 &other) const {
  70. return Vector2(x/other.x, y/other.y);
  71. }
  72. Vector2 Vector2::operator*(double value) const {
  73. return Vector2(x*value, y*value);
  74. }
  75. Vector2 Vector2::operator/(double value) const {
  76. return Vector2(x/value, y/value);
  77. }
  78. Vector2 & Vector2::operator+=(const Vector2 &other) {
  79. x += other.x, y += other.y;
  80. return *this;
  81. }
  82. Vector2 & Vector2::operator-=(const Vector2 &other) {
  83. x -= other.x, y -= other.y;
  84. return *this;
  85. }
  86. Vector2 & Vector2::operator*=(const Vector2 &other) {
  87. x *= other.x, y *= other.y;
  88. return *this;
  89. }
  90. Vector2 & Vector2::operator/=(const Vector2 &other) {
  91. x /= other.x, y /= other.y;
  92. return *this;
  93. }
  94. Vector2 & Vector2::operator*=(double value) {
  95. x *= value, y *= value;
  96. return *this;
  97. }
  98. Vector2 & Vector2::operator/=(double value) {
  99. x /= value, y /= value;
  100. return *this;
  101. }
  102. double dotProduct(const Vector2 &a, const Vector2 &b) {
  103. return a.x*b.x+a.y*b.y;
  104. }
  105. double crossProduct(const Vector2 &a, const Vector2 &b) {
  106. return a.x*b.y-a.y*b.x;
  107. }
  108. Vector2 operator*(double value, const Vector2 &vector) {
  109. return Vector2(value*vector.x, value*vector.y);
  110. }
  111. Vector2 operator/(double value, const Vector2 &vector) {
  112. return Vector2(value/vector.x, value/vector.y);
  113. }
  114. }