2D Vector Class


/ Published in: C++
Save to your folder(s)

Test harness included.


Copy this code and paste it in your HTML
  1. #include <iostream>
  2. #include <string>
  3. #include <math.h>
  4.  
  5. using namespace std;
  6.  
  7. class Vector2D
  8. {
  9.  
  10. public:
  11. Vector2D(float X = 0, float Y = 0)
  12. {
  13. x = X;
  14. y = Y;
  15. };
  16. ~Vector2D() {} ;
  17.  
  18. float x, y;
  19.  
  20. Vector2D operator*(float scalar) const
  21. {
  22. return Vector2D(x * scalar, y * scalar);
  23. }
  24.  
  25.  
  26. Vector2D operator+(const Vector2D &vect) const
  27. {
  28. return Vector2D(x + vect.x, y + vect.y);
  29. }
  30.  
  31. Vector2D operator-(const Vector2D &vect) const
  32. {
  33. return Vector2D(x - vect.x, y - vect.y);
  34. }
  35.  
  36. void rotate(float angle)
  37. {
  38. float xt = (x * cosf(angle)) - (y * sinf(angle));
  39. float yt = (y * cosf(angle)) + (x * sinf(angle));
  40. x = xt;
  41. y = yt;
  42. }
  43.  
  44. float crossproduct(const Vector2D &vect2) const
  45. {
  46. return (this->x * vect2.y) - (this->y * vect2.x);
  47. }
  48.  
  49. float magnitude()
  50. {
  51. return sqrtf(x * x +y * y);
  52. }
  53.  
  54. void normalise()
  55. {
  56. float mag = sqrtf(x* x + y * y);
  57. this->x = x / mag;
  58. this->y = y / mag;
  59. }
  60.  
  61. // return dot product
  62. float dotproduct(const Vector2D &vect) const
  63. {
  64. return (x * vect.x) + (y * vect.y);
  65. }
  66. };
  67.  
  68. int main()
  69. {
  70. cout << "### VECTOR INITIALISE ###" << endl;
  71. cout << "We are now initilising a vector [3, 3]. Results: " << endl;
  72. Vector2D vec(3, 3);
  73. cout << "new x: " << vec.x << endl;
  74. cout << "new y: " << vec.y << endl;
  75.  
  76. cout << endl << "### VECTOR NORMALISE ###" << endl;
  77. vec.normalise();
  78. cout << "We are now normalising a vector [3, 3]. Results: " << endl;
  79. cout << "new x: " << vec.x << endl;
  80. cout << "new y: " << vec.y << endl;
  81. cout << "new mag: " << vec.magnitude() << endl;
  82.  
  83. cout << endl << "### VECTOR MULTIPLICATION BY SCALAR ###" << endl;
  84. cout << "We are now multiplying a vector [10,10] by the scalar 5. Results: " << endl;
  85. Vector2D vec2(10, 10);
  86. Vector2D vec3 = vec2 * 5;
  87. cout << "new x: " << vec3.x << endl;
  88. cout << "new y: " << vec3.y << endl;
  89.  
  90. cout << endl << "### VECTOR CROSS PRODUCT ###" << endl;
  91. cout << "Cross product between vectors [1,2] and [2,4] Results: " << endl;
  92. Vector2D vec4(1, 2);
  93. Vector2D vec5(2, 4);
  94. double cp = vec4.crossproduct(vec5);
  95. cout << "Cross product: " << cp << endl;
  96.  
  97. cout << endl << "### VECTOR ROTATION ###" << endl;
  98. cout << "Rotating a vector (10, 0) by 90 degrees CCW. Results: " << endl;
  99. Vector2D vec6(10, 0);
  100. vec6.rotate( 90 * (3.14159265/180) ); // remember to convert deg to rad
  101. cout << "new x: " << vec6.x << endl;
  102. cout << "new y: " << vec6.y << endl;
  103.  
  104. cout << endl << "### VECTOR DOT PRODUCT ###" << endl;
  105. cout << "Dot product between vectors [5,5] and [10,10] Results: " << endl;
  106. Vector2D vec7(5, 5);
  107. Vector2D vec8(10, 10);
  108. double dp = vec7.dotproduct(vec8);
  109. cout << "Dot product: " << dp << endl;
  110.  
  111. system("PAUSE");
  112. return 0;
  113. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.