GeeckoDev

Vector3

Nov 7th, 2011
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.56 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. class Vector3
  5. {
  6. private:
  7.   double _x, _y, _z;
  8.  
  9. public:
  10.   Vector3();
  11.   Vector3(double, double, double);
  12.   ~Vector3();
  13.  
  14.   void set(double, double, double);
  15.   void setX(double);
  16.   void setY(double);
  17.   void setZ(double);
  18.  
  19.   double getX();
  20.   double getY();
  21.   double getZ();
  22.  
  23.   void print();
  24.   double length();
  25.   Vector3 normalized();
  26.   Vector3 vproduct(Vector3 v);
  27. };
  28.  
  29. // Operators
  30.  
  31. Vector3 operator+(Vector3 a, Vector3 b)
  32. {
  33.   Vector3 t(a.getX() + b.getX(), a.getY() + b.getY(), a.getZ() + b.getZ());
  34.   return t;
  35. }
  36.  
  37. Vector3 operator-(Vector3 a)
  38. {
  39.   Vector3 t(-a.getX(), -a.getY(), -a.getZ());
  40.   return t;
  41. }
  42.  
  43. Vector3 operator-(Vector3 a, Vector3 b)
  44. {
  45.   return a + (-b);
  46. }
  47.  
  48. double operator*(Vector3 a, Vector3 b)
  49. {
  50.   double t(a.getX()*b.getX() + a.getY()*b.getY() + a.getZ()*b.getZ());
  51.   return t;
  52. }
  53.  
  54. Vector3 operator*(Vector3 a, double b)
  55. {
  56.   Vector3 t(a.getX()*b, a.getY()*b, a.getZ()*b);
  57.   return t;
  58. }
  59.  
  60. Vector3 operator*(double a, Vector3 b)
  61. {
  62.   return b*a;
  63. }
  64.  
  65. Vector3 operator/(Vector3 a, double b)
  66. {
  67.   return a*(1./b);
  68. }
  69.  
  70. bool operator==(Vector3 a, Vector3 b)
  71. {
  72.   return a.getX() == b.getX() && a.getY() == b.getY() && a.getZ() == b.getZ();
  73. }
  74.  
  75. bool operator!=(Vector3 a, Vector3 b)
  76. {
  77.   return !(a == b);
  78. }
  79.  
  80. // Constructor/Destructor
  81.  
  82. Vector3::Vector3()
  83. {
  84.   _x = 0.;
  85.   _y = 0.;
  86.   _z = 0.;
  87. }
  88.  
  89. Vector3::Vector3(double x, double y, double z)
  90. {
  91.   _x = x;
  92.   _y = y;
  93.   _z = z;
  94. }
  95.  
  96. Vector3::~Vector3()
  97. {
  98. }
  99.  
  100. // Modifiers
  101.  
  102. void Vector3::set(double x, double y, double z)
  103. {
  104.   _x = x;
  105.   _y = y;
  106.   _z = z;
  107. }
  108.  
  109. void Vector3::setX(double x)
  110. {
  111.   _x = x;
  112. }
  113.  
  114. void Vector3::setY(double y)
  115. {
  116.   _y = y;
  117. }
  118.  
  119. void Vector3::setZ(double z)
  120. {
  121.   _z = z;
  122. }
  123.  
  124. // Accessors
  125.  
  126. double Vector3::getX()
  127. {
  128.   return _x;
  129. }
  130.  
  131. double Vector3::getY()
  132. {
  133.   return _y;
  134. }
  135.  
  136. double Vector3::getZ()
  137. {
  138.   return _z;
  139. }
  140.  
  141. // Methods
  142.  
  143. void Vector3::print()
  144. {
  145.   std::cout << "Vector3 (" << _x << ", " << _y << ", " << _z << ")\n";
  146. }
  147.  
  148. double Vector3::length()
  149. {
  150.   return sqrt((*this) * (*this));
  151. }
  152.  
  153. Vector3 Vector3::normalized()
  154. {
  155.   return *this / this->length();
  156. }
  157.  
  158. Vector3 Vector3::vproduct(Vector3 v)
  159. {
  160.   Vector3 t(this->_y*v._z - this->_z*v._y,
  161.             this->_z*v._x - this->_x*v._z,
  162.             this->_x*v._y - this->_y*v._x);
  163.   return t;
  164. }
  165.  
  166. // Main
  167.  
  168. int main()
  169. {
  170.   Vector3 v(3, 5, 8.2455);
  171.   v.print();
  172.   v = v*6 ;
  173.   v.print();
  174.   v = v+v;
  175.   v.print();
  176.   v = v.normalized();
  177.   v.print();
  178.   v = v.vproduct(v);
  179.   v.print();
  180.   std::cout << "Done\n";
  181.   return 0;
  182. }
  183.  
Advertisement
Add Comment
Please, Sign In to add comment