Advertisement
Guest User

Untitled

a guest
Feb 21st, 2015
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.51 KB | None | 0 0
  1. // Made by MLM
  2. // Thanks to Amble Lighthertz(http://codereview.stackexchange.com/questions/11934/c-vector2-class-review) for the operator overloading snippets
  3.  
  4. #if defined(CORE_TEENSY)
  5. #include "Arduino.h"
  6. #else
  7. // pow and sqrt
  8. #include <math.h>
  9. #endif
  10.  
  11. #include "Vector2.h"
  12.  
  13.  
  14. const Vector2 Vector2::zero(0, 0);
  15. const Vector2 Vector2::one(1, 1);
  16. const Vector2 Vector2::invalid(false);
  17.  
  18. float x;
  19. float y;
  20. bool isValid = true;
  21.  
  22. Vector2::Vector2()
  23. {
  24.     this->init(0, 0);
  25. }
  26. Vector2::Vector2(float x, float y)
  27. {
  28.     this->init(x, y);
  29. }
  30. Vector2::Vector2(int x, int y)
  31. {
  32.     this->init((float)x, (float)y);
  33. }
  34. Vector2::Vector2(unsigned int x, unsigned int y)
  35. {
  36.     this->init((float)x, (float)y);
  37. }
  38. Vector2::Vector2(bool isValid)
  39. {
  40.     if(isValid) {
  41.         this->init(0, 0);
  42.     }
  43.  
  44.     this->isValid = isValid;
  45.    
  46. }
  47.  
  48. Vector2::Vector2(const Vector2& vec)
  49. {
  50.     this->copy(vec);
  51. }
  52.  
  53. void Vector2::init(float x, float y)
  54. {
  55.     this->x = x;
  56.     this->y = y;
  57. }
  58. void Vector2::init(float x, float y, bool isValid)
  59. {
  60.     this->init(x, y);
  61.     this->isValid = isValid;
  62. }
  63.  
  64. void Vector2::copy(const Vector2& vec)
  65. {
  66.     this->init(vec.x, vec.y, vec.isValid);
  67. }
  68.  
  69.  
  70. float Vector2::magnitude()
  71. {
  72.     return sqrt(pow(this->x, 2) + pow(this->y, 2));
  73. }
  74.  
  75. Vector2 Vector2::normalized()
  76. {
  77.     float length = this->magnitude();
  78.     Vector2 unitVector(Vector2(this->x/length, this->y/length));
  79.     return unitVector;
  80. }
  81.  
  82.  
  83.  
  84.  
  85. /* */
  86. // assignment operator
  87. Vector2& Vector2::operator= (const Vector2& v) {
  88.     // Check for self-assignment!
  89.     if (this == &v) {
  90.         return *this;
  91.     }
  92.  
  93.     this->copy(v);
  94.     return *this;
  95. }
  96. /* */
  97.  
  98. // For making something negative
  99. Vector2& Vector2::operator- (void) {
  100.     x = -x;
  101.     y = -y;
  102.     return *this;
  103. }
  104.  
  105. // equality operators
  106. bool Vector2::operator == (const Vector2 & v) { return (x == v.x) && (y == v.y); }
  107. bool Vector2::operator != (const Vector2 & v) { return !(*this == v); }
  108.  
  109. // scaler to this operators
  110. Vector2& Vector2::operator+= (float s) { x += s; y += s; return *this; }
  111. Vector2& Vector2::operator-= (float s) { x -= s; y -= s; return *this; }
  112. Vector2& Vector2::operator*= (float s) { x *= s; y *= s; return *this; }
  113. Vector2& Vector2::operator/= (float s) { x /= s; y /= s; return *this; }
  114.  
  115. // scaler to vector2 operators
  116. Vector2 Vector2::operator+ (float s) { Vector2 r(*this); return r += s; }
  117. Vector2 Vector2::operator- (float s) { Vector2 r(*this); return r -= s; }
  118. Vector2 Vector2::operator* (float s) { Vector2 r(*this); return r *= s; }
  119. Vector2 Vector2::operator/ (float s) { Vector2 r(*this); return r /= s; }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement