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 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. bool Vector3D::equal(const Vector3D &other) const
  47. {
  48. return _x == other._x && _y == other._y && _z == other._z;
  49. }
  50. Vector3D &Vector3D::add(double k)
  51. {
  52. return add(Vector3D(k, k, k));
  53. }
  54. Vector3D &Vector3D::add(double x, double y, double z)
  55. {
  56. return add(Vector3D(x, y, z));
  57. }
  58. Vector3D& Vector3D::add(const Vector3D &other)
  59. {
  60. _x += other._x;
  61. _y += other._y;
  62. _z += other._z;
  63. return *this;
  64. }
  65. Vector3D &Vector3D::sub(double k)
  66. {
  67. return sub(Vector3D(k, k, k));
  68. }
  69. Vector3D &Vector3D::sub(double x, double y, double z)
  70. {
  71. return sub(Vector3D(x, y, z));
  72. }
  73. Vector3D& Vector3D::sub(const Vector3D &other)
  74. {
  75. _x -= other._x;
  76. _y -= other._y;
  77. _z -= other._z;
  78. return *this;
  79. }
  80. Vector3D &Vector3D::mult(double k)
  81. {
  82. _x *= k;
  83. _y *= k;
  84. _z *= k;
  85. return *this;
  86. }
  87. Vector3D &Vector3D::div(double k)
  88. {
  89. _x /= k;
  90. _y /= k;
  91. _z /= k;
  92. return *this;
  93. }
  94. double Vector3D::dotProduct(double x, double y, double z) const
  95. {
  96. return dotProduct(Vector3D(x, y, z));
  97. }
  98. double Vector3D::dotProduct(const Vector3D &other) const
  99. {
  100. return (_x * other._x) + (_y * other._y) + (_z * other._z);
  101. }
  102. double Vector3D::norm() const
  103. {
  104. return sqrt((_x * _x) + (_y * _y) + (_z * _z));
  105. }
  106. Vector3D Vector3D::operator+()
  107. {
  108. return *this;
  109. }
  110. Vector3D Vector3D::operator+(const double &k)
  111. {
  112. return Vector3D(*this).add(k);
  113. }
  114. Vector3D Vector3D::operator+(const Vector3D &v2)
  115. {
  116. return Vector3D(*this).add(v2);
  117. }
  118. Vector3D Vector3D::operator-()
  119. {
  120. return Vector3D(-_x, -_y, -_z);
  121. }
  122. Vector3D Vector3D::operator-(const double &k)
  123. {
  124. return Vector3D(*this).sub(k);
  125. }
  126. Vector3D Vector3D::operator-(const Vector3D &v2)
  127. {
  128. return Vector3D(*this).sub(v2);
  129. }
  130. Vector3D Vector3D::operator*(const double &k)
  131. {
  132. return Vector3D(*this).mult(k);
  133. }
  134. double Vector3D::operator*(const Vector3D &v2)
  135. {
  136. return Vector3D(*this).dotProduct(v2);
  137. }
  138. Vector3D Vector3D::operator/(const double &k)
  139. {
  140. return Vector3D(*this).div(k);
  141. }
  142. bool Vector3D::operator==(const Vector3D &other)
  143. {
  144. return equal(other);
  145. }
  146. bool Vector3D::operator!=(const Vector3D &other)
  147. {
  148. return !equal(other);
  149. }
  150. bool Vector3D::operator!()
  151. {
  152. return isNull();
  153. }
  154. Vector3D::operator bool()
  155. {
  156. return !isNull();
  157. }