Last active
April 14, 2018 22:41
-
-
Save creikey/e9654f684370b520df05f9be8559c4ba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template <class T = float> class Vector { | |
public: | |
T x; | |
T y; | |
Vector() : x(T(0)), y(T(0)){}; | |
Vector(T inx, T iny) : x(inx), y(iny){}; | |
Vector<double> normalize() { | |
double len = this->length(); | |
return Vector<double>(this->x / len, this->y / len); | |
}; | |
double length() { return sqrt(this->x * this->x + this->y * this->y); }; | |
std::string toString() { | |
return "(" + std::to_string(this->x) + "," + std::to_string(this->y) + ")"; | |
}; | |
Vector &operator+(const Vector &vec) { | |
this->x += vec.x; | |
this->y += vec.y; | |
return *this; | |
}; // add | |
Vector &operator*(const Vector &vec) { | |
this->x *= vec.x; | |
this->y *= vec.y; | |
return *this; | |
}; | |
Vector &operator*(const T scalar) { | |
this->x *= scalar; | |
this->y *= scalar; | |
return *this; | |
}; | |
Vector &operator-(const Vector &vec) { | |
this->x -= vec.x; | |
this->y -= vec.y; | |
return *this; | |
}; | |
Vector &operator/(const Vector &vec) { | |
this->x /= vec.x; | |
this->y /= vec.y; | |
return *this; | |
}; | |
bool operator>(const Vector &vec) { | |
return (this->x > vec.x) && (this->y > vec.y); | |
} | |
bool operator<(const Vector &vec) { | |
return (this->x < vec.x) && (this->y < vec.y); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment