Created
September 17, 2019 21:09
-
-
Save izanbf1803/53f0931755e1407d2604c39d8d08a0a9 to your computer and use it in GitHub Desktop.
Simple math vector class in C++
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
#ifndef __VEC_HPP__ | |
#define __VEC_HPP__ | |
#include <iostream> | |
#include <cmath> | |
namespace vec | |
{ | |
typedef long double ld; | |
class Vec // 2d | |
{ | |
public: | |
ld x, y; | |
inline ~Vec() { x = y = 0; }; | |
Vec(const ld& x_, const ld& y_) | |
{ | |
x = x_; | |
y = y_; | |
} | |
inline ld mod() const { return sqrt(x*x + y*y); }; | |
inline bool operator==(const Vec& v) const { return x == v.x and y == v.y; } | |
inline bool operator!=(const Vec& v) const { return x != v.x or y != v.y; } | |
inline bool operator>=(const Vec& v) const { return mod() >= v.mod(); } | |
inline bool operator<=(const Vec& v) const { return mod() <= v.mod(); } | |
inline bool operator>(const Vec& v) const { return mod() > v.mod(); } | |
inline bool operator<(const Vec& v) const { return mod() < v.mod(); } | |
inline Vec operator+(const Vec& v) const { return Vec(x + v.x, y + v.y); } | |
inline Vec operator-(const Vec& v) const { return Vec(x - v.x, y - v.y); } | |
inline Vec operator*(const ld& k) const { return Vec(k*x, k*y); } | |
inline Vec operator/(const ld& k) const { return Vec(x/k, y/k); } | |
inline ld operator*(const Vec& v) const { return x*v.x + y*v.y; } | |
inline Vec operator+=(const Vec& v) { *this = *this + v; return *this; } | |
inline Vec operator-=(const Vec& v) { *this = *this - v; return *this; } | |
inline Vec operator*=(const ld& k) { *this = *this * k; return *this; } | |
friend std::ostream& operator<<(std::ostream& out, const Vec& v) | |
{ | |
out << '(' << v.x << ',' << ' ' << v.y << ')'; | |
return out; | |
} | |
}; | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment