Skip to content

Instantly share code, notes, and snippets.

@creikey
Last active April 14, 2018 22:41
Show Gist options
  • Save creikey/e9654f684370b520df05f9be8559c4ba to your computer and use it in GitHub Desktop.
Save creikey/e9654f684370b520df05f9be8559c4ba to your computer and use it in GitHub Desktop.
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