Vector.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include <cmath>
  2. #include "client/math/Vector.h"
  3. Vector::Vector() : x(0), y(0), z(0)
  4. {
  5. }
  6. Vector::Vector(float ix, float iy, float iz) : x(ix), y(iy), z(iz)
  7. {
  8. }
  9. float Vector::getX() const
  10. {
  11. return x;
  12. }
  13. float Vector::getY() const
  14. {
  15. return y;
  16. }
  17. float Vector::getZ() const
  18. {
  19. return z;
  20. }
  21. void Vector::setX(float ix)
  22. {
  23. x = ix;
  24. }
  25. void Vector::setY(float iy)
  26. {
  27. y = iy;
  28. }
  29. void Vector::setZ(float iz)
  30. {
  31. z = iz;
  32. }
  33. void Vector::set(float ix, float iy, float iz)
  34. {
  35. x = ix;
  36. y = iy;
  37. z = iz;
  38. }
  39. void Vector::setInverse(const Vector& v)
  40. {
  41. x = -v.x;
  42. y = -v.y;
  43. z = -v.z;
  44. }
  45. void Vector::setMul(const Vector& v, float f)
  46. {
  47. x = v.x * f;
  48. y = v.y * f;
  49. z = v.z * f;
  50. }
  51. void Vector::setAngles(float lengthAngle, float widthAngle)
  52. {
  53. lengthAngle = lengthAngle * M_PI / 180.0f;
  54. widthAngle = widthAngle * M_PI / 180.0f;
  55. x = cosf(widthAngle) * sinf(lengthAngle);
  56. y = sinf(widthAngle);
  57. z = cosf(widthAngle) * cosf(lengthAngle);
  58. }
  59. void Vector::add(const Vector& v)
  60. {
  61. x += v.x;
  62. y += v.y;
  63. z += v.z;
  64. }
  65. void Vector::sub(const Vector& v)
  66. {
  67. x -= v.x;
  68. y -= v.y;
  69. z -= v.z;
  70. }
  71. void Vector::mul(float f)
  72. {
  73. x *= f;
  74. y *= f;
  75. z *= f;
  76. }
  77. void Vector::addMul(const Vector& v, float f)
  78. {
  79. x += v.x * f;
  80. y += v.y * f;
  81. z += v.z * f;
  82. }
  83. void Vector::cross(float ix, float iy, float iz)
  84. {
  85. set(y * iz - z * iy, z * ix - x * iz, x * iy - y * ix);
  86. }
  87. void Vector::cross(const Vector& v)
  88. {
  89. set(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x);
  90. }
  91. void Vector::normalize()
  92. {
  93. float f = 1.0f / sqrtf(squareLength());
  94. x *= f;
  95. y *= f;
  96. z *= f;
  97. }
  98. float Vector::squareLength() const
  99. {
  100. return x * x + y * y + z * z;
  101. }
  102. float Vector::dot(const Vector& v) const
  103. {
  104. return x * v.x + y * v.y + z * v.z;
  105. }
  106. float Vector::dotInverse(const Vector& v) const
  107. {
  108. return x * (-v.x) + y * (-v.y) + z * (-v.z);
  109. }
  110. std::ostream& operator<<(std::ostream& os, const Vector& v)
  111. {
  112. return os << "Vector(x = " << v.getX() << ", y = " << v.getY() << ", z = " << v.getZ() << ")";
  113. }