Vector3D.cpp 2.3 KB

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