omg :(
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 __Renbro__linal__ | |
#define __Renbro__linal__ | |
#include <type_traits> | |
#include <ostream> | |
#include <math.h> | |
template <class T, class = typename std::enable_if<std::is_arithmetic<T>::value>::type > | |
struct Vec2{ | |
T x; | |
T y; | |
constexpr T sqrLen() const { | |
return x*x+y*y; | |
} | |
constexpr auto len() -> decltype(sqrt(sqrLen())) const { | |
return sqrt(sqrLen()); | |
} | |
constexpr auto norm() -> Vec2<decltype(x/len())> const { | |
constexpr auto l = len(); | |
return Vec2<decltype(x/len())>{x/l,y/l}; | |
} | |
template<class A> | |
constexpr auto operator+(const Vec2<A>& r) const -> Vec2<decltype(r.x+x)> { | |
return Vec2<decltype(r.x+x)>{x+r.x, y+r.y}; | |
} | |
template<class A> | |
constexpr auto operator-(const Vec2<A>& r) const -> Vec2<decltype(r.x+x)> { | |
return Vec2<decltype(r.x+x)>{x-r.x, y-r.y}; | |
} | |
template<class A> | |
constexpr auto dot(const Vec2<A>& r) const -> decltype(r.x+x) { | |
return x*r.x + y+r.y; | |
} | |
constexpr auto operator-() const -> Vec2<decltype(-x)> { | |
return Vec2<decltype(-x)>{-x, -y}; | |
} | |
template<class A> | |
constexpr auto operator*(const A& n) const -> Vec2<decltype(n*x)> { | |
return Vec2<decltype(n*x)>{x*n, y*n}; | |
} | |
template<class A, class = typename std::enable_if<std::is_arithmetic<A>::value>::type> | |
friend constexpr auto operator*(const A n, const Vec2<T>& v) -> Vec2<decltype(n*x)> { | |
return Vec2<decltype(n*x)>{v.x*n, v.y*n}; | |
} | |
template<class O> | |
constexpr auto operator/(const O& n) const -> Vec2<decltype(n*x)> { | |
return Vec2<decltype(n*x)>{x/n, y/n}; | |
} | |
template<class A, class = typename std::enable_if<std::is_arithmetic<A>::value>::type> | |
friend constexpr auto operator/(const A n, const Vec2<T>& v) -> Vec2<decltype(n*x)> { | |
return Vec2<decltype(n*x)>{v.x/n, v.y/n}; | |
} | |
friend std::ostream& operator<<(std::ostream& os, Vec2<T> v) | |
{ | |
os<<"{x:"<<v.x<<"; y:"<<v.y<<"}"; | |
return os; | |
} | |
}; | |
#endif /* defined(__Renbro__linal__) */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment