summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--vex.hpp55
1 files changed, 30 insertions, 25 deletions
diff --git a/vex.hpp b/vex.hpp
index e75ea7e..a611562 100644
--- a/vex.hpp
+++ b/vex.hpp
@@ -19,33 +19,33 @@ struct vec_dimd
{
std::array<T, D> v;
- explicit constexpr vec_dimd() = default;
+ explicit vec_dimd() = default;
template<typename ...Args>
- explicit constexpr vec_dimd(Args&&... args) : v{args...} {}
- explicit constexpr vec_dimd(T args[D]) : v(args) {}
- explicit constexpr vec_dimd(T fill) { v.fill(fill); }
+ explicit vec_dimd(Args&&... args) : v{args...} {}
+ explicit vec_dimd(T args[D]) : v(args) {}
+ explicit vec_dimd(T fill) { v.fill(fill); }
- constexpr T operator[](std::size_t i) { return v[i]; }
- constexpr T operator[](std::size_t i) const { return v[i]; }
+ T operator[](std::size_t i) { return v[i]; }
+ T operator[](std::size_t i) const { return v[i]; }
- constexpr vec_dimd<T, D> operator+=(const vec_dimd<T, D> &rhs) { for(size_t i = 0; i < D; i++) v[i] += rhs.v[i]; return *this; }
- constexpr vec_dimd<T, D> operator-=(const vec_dimd<T, D> &rhs) { for(size_t i = 0; i < D; i++) v[i] -= rhs.v[i]; return *this; }
- constexpr vec_dimd<T, D> operator*=(const vec_dimd<T, D> &rhs) { for(size_t i = 0; i < D; i++) v[i] *= rhs.v[i]; return *this; }
- constexpr vec_dimd<T, D> operator/=(const vec_dimd<T, D> &rhs) { for(size_t i = 0; i < D; i++) v[i] /= rhs.v[i]; return *this; }
+ vec_dimd<T, D> operator+=(const vec_dimd<T, D> &rhs) { for(size_t i = 0; i < D; i++) v[i] += rhs.v[i]; return *this; }
+ vec_dimd<T, D> operator-=(const vec_dimd<T, D> &rhs) { for(size_t i = 0; i < D; i++) v[i] -= rhs.v[i]; return *this; }
+ vec_dimd<T, D> operator*=(const vec_dimd<T, D> &rhs) { for(size_t i = 0; i < D; i++) v[i] *= rhs.v[i]; return *this; }
+ vec_dimd<T, D> operator/=(const vec_dimd<T, D> &rhs) { for(size_t i = 0; i < D; i++) v[i] /= rhs.v[i]; return *this; }
- constexpr vec_dimd<T, D> operator*=(const T &rhs) { for(size_t i = 0; i < D; i++) v[i] *= rhs; return *this; }
- constexpr vec_dimd<T, D> operator/=(const T &rhs) { for(size_t i = 0; i < D; i++) v[i] /= rhs; return *this; }
+ vec_dimd<T, D> operator*=(const T &rhs) { for(size_t i = 0; i < D; i++) v[i] *= rhs; return *this; }
+ vec_dimd<T, D> operator/=(const T &rhs) { for(size_t i = 0; i < D; i++) v[i] /= rhs; return *this; }
- constexpr auto operator<=>(const vec_dimd<T,D> &rhs) const { return magnitude() <=> rhs.magnitude(); }
- constexpr bool operator==(const vec_dimd<T,D> &rhs) const { for(unsigned i = 0; i < D; i++) if(v[i] != rhs.v[i]) return false; return true; }
+ auto operator<=>(const vec_dimd<T,D> &rhs) const { return magnitude() <=> rhs.magnitude(); }
+ bool operator==(const vec_dimd<T,D> &rhs) const { for(unsigned i = 0; i < D; i++) if(v[i] != rhs.v[i]) return false; return true; }
- constexpr friend vec_dimd<T, D> operator+(vec_dimd<T, D> lhs, const vec_dimd<T, D> &rhs) { lhs += rhs; return lhs; }
- constexpr friend vec_dimd<T, D> operator-(vec_dimd<T, D> lhs, const vec_dimd<T, D> &rhs) { lhs -= rhs; return lhs; }
- constexpr friend vec_dimd<T, D> operator*(vec_dimd<T, D> lhs, const vec_dimd<T, D> &rhs) { lhs *= rhs; return lhs; }
- constexpr friend vec_dimd<T, D> operator/(vec_dimd<T, D> lhs, const vec_dimd<T, D> &rhs) { lhs /= rhs; return lhs; }
+ friend vec_dimd<T, D> operator+(vec_dimd<T, D> lhs, const vec_dimd<T, D> &rhs) { lhs += rhs; return lhs; }
+ friend vec_dimd<T, D> operator-(vec_dimd<T, D> lhs, const vec_dimd<T, D> &rhs) { lhs -= rhs; return lhs; }
+ friend vec_dimd<T, D> operator*(vec_dimd<T, D> lhs, const vec_dimd<T, D> &rhs) { lhs *= rhs; return lhs; }
+ friend vec_dimd<T, D> operator/(vec_dimd<T, D> lhs, const vec_dimd<T, D> &rhs) { lhs /= rhs; return lhs; }
- constexpr friend vec_dimd<T, D> operator*(vec_dimd<T, D> lhs, const T &rhs) { lhs *= rhs; return lhs; }
- constexpr friend vec_dimd<T, D> operator/(vec_dimd<T, D> lhs, const T &rhs) { lhs /= rhs; return lhs; }
+ friend vec_dimd<T, D> operator*(vec_dimd<T, D> lhs, const T &rhs) { lhs *= rhs; return lhs; }
+ friend vec_dimd<T, D> operator/(vec_dimd<T, D> lhs, const T &rhs) { lhs /= rhs; return lhs; }
friend std::ostream &operator<<(std::ostream &os, const vec_dimd<T,D> obj) {
os << '{';
@@ -54,11 +54,16 @@ struct vec_dimd
}
/*Finds the distance from the origin to the ray cast in D dimension space using components of vec*/
- constexpr T magnitude() const { T t{}; for(size_t i = 0; i < D; i++) t += v[i] * v[i]; return std::sqrt(t); }
+ T magnitude() const { T t{}; for(size_t i = 0; i < D; i++) t += v[i] * v[i]; return std::sqrt(t); }
/*Finds the dot product of itself and another vec of same T and D*/
- constexpr T dot(const vec_dimd<T, D> &b) const { T t; for(size_t i = 0; i < D; i++) t += (v[i] * b.v[i]); return t; }
- constexpr friend T dot(const vec_dimd<T,D> &lhs, const vec_dimd<T,D> &rhs) { return lhs.dot(rhs); }
+ T dot(const vec_dimd<T, D> &b) const { T t; for(size_t i = 0; i < D; i++) t += (v[i] * b.v[i]); return t; }
+ friend T dot(const vec_dimd<T,D> &lhs, const vec_dimd<T,D> &rhs) { return lhs.dot(rhs); }
+
+ auto cross(const vec_dimd<T,D> &o) const {
+ if constexpr(D == 2) return v[0]*o[0] - v[1]*o[1];
+ if constexpr(D == 3) return vec_dimd<T, D>{v[1]*o[2]-v[2]*o[1], v[2]*o[0]-v[0]*o[2],v[0]*o[1]-v[1]*o[0]};
+ }
};
template<vectype T>
@@ -69,7 +74,7 @@ using vec3 = vec_dimd<T, 3>;
/*(x, y) -> (r, theta)*/
template<vectype R, vectype P>
-static constexpr vec2<R>
+static vec2<R>
polar(const vec2<P> &in)
{
return vec2<R>(
@@ -79,7 +84,7 @@ polar(const vec2<P> &in)
}
/*(r, theta) -> (x, y)*/
template<vectype R, vectype P>
-static constexpr vec2<R>
+static vec2<R>
cartesian(const vec2<P> &in)
{
return vec2<R>(