Last active
June 4, 2016 22:51
-
-
Save dvtate/0a93e2f803ede6b32ee01552393b3b6f to your computer and use it in GitHub Desktop.
pod vector2 class template
This file contains hidden or 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
| #ifndef VECTOR2_H | |
| #define VECTOR2_H | |
| template <class TYPE> | |
| class Vector2 { | |
| public: | |
| TYPE x, y; | |
| Vector2(TYPE xVal = 0, TYPE yVal = 0) : x(xVal), y(yVal) {} | |
| Vector2& operator+=(const Vector2& vector){ | |
| x += val; | |
| y += val; | |
| return *this; | |
| } | |
| Vector2& operator-=(const Vector2& vector){ | |
| x -= val; | |
| y -= val; | |
| return *this; | |
| } | |
| template<class T> | |
| Vector2& operator*=(const T& val){ | |
| x *= val; | |
| y *= val; | |
| return *this; | |
| } | |
| template<class T> | |
| Vector2& operator/=(const T& val){ | |
| x /= val; | |
| y /= val; | |
| return *this; | |
| } | |
| Vector2 operator+(const Vector2& vector){ | |
| Vector2<TYPE> ret; | |
| ret.x = x + vector.x; | |
| ret.y = y + vector.y; | |
| return ret; | |
| } | |
| Vector2 operator-(const Vector2& vector){ | |
| Vector2<TYPE> ret; | |
| ret.x = x - vector.x; | |
| ret.y = y - vector.y; | |
| return ret; | |
| } | |
| template<class T> | |
| Vector2 operator*(const T& val){ | |
| Vector2<TYPE> ret; | |
| ret.x = x * val; | |
| ret.y = y * val; | |
| return ret; | |
| } | |
| template<class T> | |
| Vector2 operator/(const T& val){ | |
| Vector2<TYPE> ret; | |
| ret.x = x / val; | |
| ret.y = y / val; | |
| return ret; | |
| } | |
| // prefix -- operator ( --(*this) ) | |
| Vector2& operator--(){ | |
| x--; | |
| y--; | |
| return *this; | |
| } | |
| // prefix ++ operator ( ++(*this) ) | |
| Vector2& operator++(){ | |
| x++; | |
| y++; | |
| return *this; | |
| } | |
| // postfix -- operator ( (*this)-- ) | |
| Vector2 operator--( int ){ | |
| Vector2<TYPE> ret(x, y); | |
| x--; | |
| y--; | |
| return ret; | |
| } | |
| // postfix ++ operator ( (*this)++ ) | |
| Vector2 operator++( int ){ | |
| Vector2<TYPE> ret(x, y); | |
| x++; | |
| y++; | |
| return ret; | |
| } | |
| }; | |
| #endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
yes, I know operator-overloading is bad...