Vector3D.cpp 2.3 KB

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