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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 double &k)
  115. {
  116. return add(k);
  117. }
  118. Vector3D Vector3D::operator+(const Vector3D &other)
  119. {
  120. return Vector3D(*this).add(other);
  121. }
  122. Vector3D &Vector3D::operator+=(const Vector3D &other)
  123. {
  124. return add(other);
  125. }
  126. Vector3D Vector3D::operator-()
  127. {
  128. return Vector3D(-_x, -_y, -_z);
  129. }
  130. Vector3D Vector3D::operator-(const double &k)
  131. {
  132. return Vector3D(*this).sub(k);
  133. }
  134. Vector3D &Vector3D::operator-=(const double &k)
  135. {
  136. return sub(k);
  137. }
  138. Vector3D Vector3D::operator-(const Vector3D &other)
  139. {
  140. return Vector3D(*this).sub(other);
  141. }
  142. Vector3D &Vector3D::operator-=(const Vector3D &other)
  143. {
  144. return sub(other);
  145. }
  146. Vector3D Vector3D::operator*(const double &k)
  147. {
  148. return Vector3D(*this).mult(k);
  149. }
  150. Vector3D &Vector3D::operator*=(const double &k)
  151. {
  152. return mult(k);
  153. }
  154. double Vector3D::operator*(const Vector3D &other)
  155. {
  156. return Vector3D(*this).dotProduct(other);
  157. }
  158. Vector3D &Vector3D::operator*=(const Vector3D &other)
  159. {
  160. dotProduct(other);
  161. return *this;
  162. }
  163. Vector3D Vector3D::operator/(const double &k)
  164. {
  165. return Vector3D(*this).div(k);
  166. }
  167. Vector3D &Vector3D::operator/=(const double &k)
  168. {
  169. return div(k);
  170. }
  171. bool Vector3D::operator==(const Vector3D &other)
  172. {
  173. return equal(other);
  174. }
  175. bool Vector3D::operator!=(const Vector3D &other)
  176. {
  177. return !equal(other);
  178. }
  179. bool Vector3D::operator!()
  180. {
  181. return isNull();
  182. }
  183. Vector3D::operator bool()
  184. {
  185. return !isNull();
  186. }