Skip to content

Instantly share code, notes, and snippets.

@Shchvova

Shchvova/vec2.h Secret

Last active August 29, 2015 14:15
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Shchvova/3dfadf89cef0a1a537eb to your computer and use it in GitHub Desktop.
omg :(
#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