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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. Vector3D::~Vector3D()
  16. {
  17. }
  18. double Vector3D::getX() const
  19. {
  20. return _x;
  21. }
  22. Vector3D& Vector3D::setX(double x)
  23. {
  24. _x = x;
  25. return *this;
  26. }
  27. double Vector3D::getY() const
  28. {
  29. return _y;
  30. }
  31. Vector3D& Vector3D::setY(double y)
  32. {
  33. _y = y;
  34. return *this;
  35. }
  36. double Vector3D::getZ() const
  37. {
  38. return _z;
  39. }
  40. Vector3D& Vector3D::setZ(double z)
  41. {
  42. _z = z;
  43. return *this;
  44. }
  45. bool Vector3D::isNull() const
  46. {
  47. return _x == 0 && _y == 0 && _z == 0;
  48. }
  49. bool Vector3D::equal(const Vector3D &other) const
  50. {
  51. return _x == other._x && _y == other._y && _z == other._z;
  52. }
  53. Vector3D &Vector3D::add(double k)
  54. {
  55. return add(Vector3D(k, k, k));
  56. }
  57. Vector3D &Vector3D::add(double x, double y, double z)
  58. {
  59. return add(Vector3D(x, y, z));
  60. }
  61. Vector3D& Vector3D::add(const Vector3D &other)
  62. {
  63. _x += other._x;
  64. _y += other._y;
  65. _z += other._z;
  66. return *this;
  67. }
  68. Vector3D &Vector3D::sub(double k)
  69. {
  70. return sub(Vector3D(k, k, k));
  71. }
  72. Vector3D &Vector3D::sub(double x, double y, double z)
  73. {
  74. return sub(Vector3D(x, y, z));
  75. }
  76. Vector3D& Vector3D::sub(const Vector3D &other)
  77. {
  78. _x -= other._x;
  79. _y -= other._y;
  80. _z -= other._z;
  81. return *this;
  82. }
  83. Vector3D &Vector3D::mult(double k)
  84. {
  85. _x *= k;
  86. _y *= k;
  87. _z *= k;
  88. return *this;
  89. }
  90. Vector3D &Vector3D::div(double k)
  91. {
  92. _x /= k;
  93. _y /= k;
  94. _z /= k;
  95. return *this;
  96. }
  97. double Vector3D::dotProduct(double x, double y, double z) const
  98. {
  99. return dotProduct(Vector3D(x, y, z));
  100. }
  101. double Vector3D::dotProduct(const Vector3D &other) const
  102. {
  103. return (_x * other._x) + (_y * other._y) + (_z * other._z);
  104. }
  105. double Vector3D::norm() const
  106. {
  107. return sqrt((_x * _x) + (_y * _y) + (_z * _z));
  108. }
  109. Vector3D Vector3D::operator+()
  110. {
  111. return *this;
  112. }
  113. Vector3D Vector3D::operator+(const double &k)
  114. {
  115. return Vector3D(*this).add(k);
  116. }
  117. Vector3D &Vector3D::operator+=(const double &k)
  118. {
  119. return add(k);
  120. }
  121. Vector3D Vector3D::operator+(const Vector3D &other)
  122. {
  123. return Vector3D(*this).add(other);
  124. }
  125. Vector3D &Vector3D::operator+=(const Vector3D &other)
  126. {
  127. return add(other);
  128. }
  129. Vector3D Vector3D::operator-()
  130. {
  131. return Vector3D(-_x, -_y, -_z);
  132. }
  133. Vector3D Vector3D::operator-(const double &k)
  134. {
  135. return Vector3D(*this).sub(k);
  136. }
  137. Vector3D &Vector3D::operator-=(const double &k)
  138. {
  139. return sub(k);
  140. }
  141. Vector3D Vector3D::operator-(const Vector3D &other)
  142. {
  143. return Vector3D(*this).sub(other);
  144. }
  145. Vector3D &Vector3D::operator-=(const Vector3D &other)
  146. {
  147. return sub(other);
  148. }
  149. Vector3D Vector3D::operator*(const double &k)
  150. {
  151. return Vector3D(*this).mult(k);
  152. }
  153. Vector3D &Vector3D::operator*=(const double &k)
  154. {
  155. return mult(k);
  156. }
  157. double Vector3D::operator*(const Vector3D &other)
  158. {
  159. return Vector3D(*this).dotProduct(other);
  160. }
  161. Vector3D &Vector3D::operator*=(const Vector3D &other)
  162. {
  163. dotProduct(other);
  164. return *this;
  165. }
  166. Vector3D Vector3D::operator/(const double &k)
  167. {
  168. return Vector3D(*this).div(k);
  169. }
  170. Vector3D &Vector3D::operator/=(const double &k)
  171. {
  172. return div(k);
  173. }
  174. bool Vector3D::operator==(const Vector3D &other)
  175. {
  176. return equal(other);
  177. }
  178. bool Vector3D::operator!=(const Vector3D &other)
  179. {
  180. return !equal(other);
  181. }
  182. bool Vector3D::operator!()
  183. {
  184. return isNull();
  185. }
  186. Vector3D::operator bool()
  187. {
  188. return !isNull();
  189. }