You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

vector3d.cpp 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include "vector3d.h"
  2. #include <math.h>
  3. Vector3D::Vector3D(double x, double y, double z)
  4. : _x(x)
  5. , _y(y)
  6. , _z(z)
  7. {
  8. }
  9. Vector3D::Vector3D(const Vector3D &other)
  10. : _x(other._x)
  11. , _y(other._y)
  12. , _z(other._z)
  13. {
  14. }
  15. double Vector3D::getX() const
  16. {
  17. return _x;
  18. }
  19. Vector3D& Vector3D::setX(double x)
  20. {
  21. _x = x;
  22. return *this;
  23. }
  24. double Vector3D::getY() const
  25. {
  26. return _y;
  27. }
  28. Vector3D& Vector3D::setY(double y)
  29. {
  30. _y = y;
  31. return *this;
  32. }
  33. double Vector3D::getZ() const
  34. {
  35. return _z;
  36. }
  37. Vector3D& Vector3D::setZ(double z)
  38. {
  39. _z = z;
  40. return *this;
  41. }
  42. bool Vector3D::isNull() const
  43. {
  44. return _x == 0 && _y == 0 && _z == 0;
  45. }
  46. Vector3D &Vector3D::add(double x, double y, double z)
  47. {
  48. return add(Vector3D(x, y, z));
  49. }
  50. Vector3D& Vector3D::add(const Vector3D &other)
  51. {
  52. _x += other._x;
  53. _y += other._y;
  54. _z += other._z;
  55. return *this;
  56. }
  57. Vector3D &Vector3D::sub(double x, double y, double z)
  58. {
  59. return sub(Vector3D(x, y, z));
  60. }
  61. Vector3D& Vector3D::sub(const Vector3D &other)
  62. {
  63. _x -= other._x;
  64. _y -= other._y;
  65. _z -= other._z;
  66. return *this;
  67. }
  68. Vector3D &Vector3D::mult(double k)
  69. {
  70. _x *= k;
  71. _y *= k;
  72. _z *= k;
  73. return *this;
  74. }
  75. Vector3D &Vector3D::div(double k)
  76. {
  77. _x /= k;
  78. _y /= k;
  79. _z /= k;
  80. return *this;
  81. }
  82. double Vector3D::dotProduct(double x, double y, double z) const
  83. {
  84. return dotProduct(Vector3D(x, y, z));
  85. }
  86. double Vector3D::dotProduct(const Vector3D &other) const
  87. {
  88. return (_x * other._x) + (_y * other._y) + (_z * other._z);
  89. }
  90. double Vector3D::norm() const
  91. {
  92. return sqrt((_x * _x) + (_y * _y) + (_z * _z));
  93. }
  94. Vector3D &operator+(const Vector3D &v1, const Vector3D &v2)
  95. {
  96. return Vector3D(v1).add(v2);
  97. }