Advertisement
Qanar

Vector2.cpp

Mar 27th, 2022
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.34 KB | None | 0 0
  1. #include "Vector2.h"
  2.  
  3. // Default Constructor
  4. Vector2::Vector2()
  5. {
  6.     this->x = 0;
  7.     this->y = 0;
  8.     this->mag = 0;
  9.     this->dir = 0;
  10. }
  11.  
  12. // Constructor to 2D Vector
  13. Vector2::Vector2(double x, double y)
  14. {
  15.     this->x = x;
  16.     this->y = y;
  17.  
  18.     if (x != 0 || y != 0)
  19.     {
  20.         this->mag = sqrt(pow(x, 2) + pow(y, 2));
  21.  
  22.         if (y >= 0)
  23.             this->dir = acos(x / mag);
  24.         else
  25.             this->dir = 2 * PI - acos(x / mag);
  26.     }
  27.     else
  28.     {
  29.         this->mag = 0;
  30.         this->dir = 0;
  31.     }
  32. }
  33.  
  34. // Getter and Setter Functions
  35. double Vector2::getX()
  36. {
  37.     return this->x;
  38. }
  39.  
  40. double Vector2::getY()
  41. {
  42.     return this->y;
  43. }
  44.  
  45. double Vector2::getMag()
  46. {
  47.     return this->mag;
  48. }
  49.  
  50. double Vector2::getDir()
  51. {
  52.     return this->dir;
  53. }
  54.  
  55. void Vector2::setX(double x)
  56. {
  57.     this->x = x;
  58.  
  59.     this->mag = sqrt(pow(this->x, 2) + pow(this->y, 2));
  60.  
  61.     if (mag == 0)
  62.         this->dir = 0;
  63.     else
  64.     {
  65.         if (this->y > 0)
  66.             this->dir = acos(this->x / this->mag);
  67.         else
  68.             this->dir = 2 * PI - acos(this->x / this->mag);
  69.     }
  70. }
  71.  
  72. void Vector2::setY(double y)
  73. {
  74.     this->y = y;
  75.  
  76.     this->mag = sqrt(pow(this->x, 2) + pow(this->y, 2));
  77.  
  78.     if (mag == 0)
  79.         this->dir = 0;
  80.     else
  81.     {
  82.         if (this->y > 0)
  83.             this->dir = acos(this->x / this->mag);
  84.         else
  85.             this->dir = 2 * PI - acos(this->x / this->mag);
  86.     }
  87.  
  88. }
  89.  
  90. void Vector2::setMag(double mag)
  91. {
  92.     this->mag = mag;
  93.  
  94.     if (this->mag == 0)
  95.     {
  96.         this->x = 0;
  97.         this->y = 0;
  98.         this->dir = 0;
  99.     }
  100.     else
  101.     {
  102.         this->x = this->mag * cos(this->dir);
  103.         this->y = this->mag * sin(this->dir);
  104.     }
  105. }
  106.  
  107. void Vector2::setDir(double dir)
  108. {
  109.     this->dir = dir;
  110.    
  111.     this->x = this->mag * cos(dir);
  112.     this->y = this->mag * sin(dir);
  113.  
  114. }
  115.  
  116. // Returns 2-Dimensional Zero Vector
  117. Vector2 Vector2::zeroVector()
  118. {
  119.     Vector2 zero{ 0,0 };
  120.     return zero;
  121. }
  122.  
  123. // Returns 2-Dimensional Identity Matrix in the form of a vector of Vector2 Objects
  124. vector<Vector2> Vector2::identityMatrix()
  125. {
  126.     Vector2 idX{ 1,0 };
  127.     Vector2 idY{ 0,1 };
  128.  
  129.     vector <Vector2> identity{ idX, idY };
  130.  
  131.     return identity;
  132. }
  133.  
  134. // Prints 2 Vector in Magnitude-Direction format
  135. void Vector2::printVector2MagnitudeDirection()
  136. {
  137.     cout << "(" << this->mag << ", " << this->dir << "rad)" << endl;
  138. //  cout << "Vector:\nMagnitude: " << this->mag << "\nDirection(rad): " << this->dir << endl;
  139. }
  140.  
  141. // Prints Vector in (x,y) format
  142. void Vector2::printVector2()
  143. {
  144.     cout << "(x " << this->x << "| y " <<  this->y << ")" << endl;
  145. }
  146.  
  147. // Prints Matrix of 2D Vectors. For future project, make matrix class to work with
  148. // Vector2 objects
  149. void Vector2::printMatrix(vector <Vector2> matrix)
  150. {
  151.  
  152.     cout << "(";
  153.     for (size_t i{ 0 }; i < matrix.size(); i++)
  154.     {
  155.         if (i != matrix.size() - 1)
  156.             cout << matrix.at(i).x << " ";
  157.         else
  158.             cout << matrix.at(i).x;
  159.     }
  160.     cout << ")\n(";
  161.     for (size_t i{ 0 }; i < matrix.size(); i++)
  162.     {
  163.         if (i != matrix.size() - 1)
  164.             cout << matrix.at(i).y << " ";
  165.         else
  166.             cout << matrix.at(i).y;
  167.     }
  168.     cout << ")" << endl;
  169.  
  170. }
  171.  
  172. // Normalizes vector
  173. void Vector2::normalizeVector()
  174. {
  175.     this->x = x / mag;
  176.     this->y = y / mag;
  177.  
  178.     this->mag = 1;
  179. }
  180.  
  181. // Calculates perpendicular unit vector
  182. Vector2 Vector2::calculateNormal()
  183. {
  184.     Vector2 u{};
  185.     double direction{};
  186.     direction = this->dir + PI / 2;
  187.  
  188.     if (direction > 2 * PI)
  189.         direction = direction - 2 * PI;
  190.     u.dir = direction;
  191.     u.mag = 1;
  192.     u.x = cos(u.dir);
  193.     u.y = sin(u.dir);
  194.  
  195.     return u;
  196.  
  197. }
  198.  
  199. // Calculates scalar multiplication of vector
  200. Vector2 Vector2::scalarMultiplication(double s, Vector2 v)
  201. {
  202.     v.x = s * v.x;
  203.     v.y = s * v.y;
  204.  
  205.     v.mag = sqrt(pow(v.x, 2) + pow(v.y, 2));
  206.  
  207.     return v;
  208. }
  209.  
  210. // Overloading "+" operator
  211. Vector2 Vector2::operator+ (Vector2 const& obj)
  212. {
  213.     Vector2 vec;
  214.     vec.x = x + obj.x;
  215.     vec.y = y + obj.y;
  216.  
  217.     vec.mag = sqrt(pow(vec.x, 2) + pow(vec.y, 2));
  218.  
  219.     if (mag == 0)
  220.     {
  221.         vec.dir = 0;
  222.     }
  223.     else {
  224.         if (y > 0)
  225.             vec.dir = acos(vec.x / vec.mag);
  226.         else
  227.             vec.dir = 2 * PI - acos(vec.x / vec.mag);
  228.     }
  229.  
  230.     return vec;
  231. }
  232.  
  233. // Overloading "-" operator
  234. Vector2 Vector2::operator- (Vector2 const& obj)
  235. {
  236.     Vector2 vec;
  237.     vec.x = x - obj.x;
  238.     vec.y = y - obj.y;
  239.  
  240.     if (mag == 0)
  241.     {
  242.         vec.dir = 0;
  243.     }
  244.     else {
  245.  
  246.         if (y > 0)
  247.             vec.dir = acos(vec.x / vec.mag);
  248.         else
  249.             vec.dir = 2 * PI - acos(vec.x / vec.mag);
  250.  
  251.     }
  252.  
  253.     return vec;
  254. }
  255.  
  256. // Overloading "*" operator for dot product.
  257. double Vector2::operator* (Vector2 const& obj)
  258. {
  259.     double dotP;
  260.     dotP = x * obj.x + y * obj.y;
  261.     return dotP;
  262. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement