summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJon Santmyer <jon@jonsantmyer.com>2022-11-20 23:35:33 -0500
committerJon Santmyer <jon@jonsantmyer.com>2022-11-20 23:35:33 -0500
commit7b1f1372e3993860bd0909e6851952fffc5d097f (patch)
treeb4fcd8f5ae683b471818e0573b4f5b96fea8caee
downloadmathvector-7b1f1372e3993860bd0909e6851952fffc5d097f.tar.gz
mathvector-7b1f1372e3993860bd0909e6851952fffc5d097f.tar.bz2
mathvector-7b1f1372e3993860bd0909e6851952fffc5d097f.zip
initial commit. math using arbitrary length vectorsHEADmaster
-rw-r--r--README.MD17
-rw-r--r--mvector.hpp164
2 files changed, 181 insertions, 0 deletions
diff --git a/README.MD b/README.MD
new file mode 100644
index 0000000..b07b77d
--- /dev/null
+++ b/README.MD
@@ -0,0 +1,17 @@
+# MATHVECTOR
+A simple C++ library for arbitrary length mathematical vectors. All functions
+and member classes lie in the mvec namespace
+
+## Use
+Add mvector.hpp to your project, make a new vector using
+```
+mvec::vec<double, 2>
+```
+or
+```
+mvec;:vec2<double>
+```
+mvec comes with multiple pre-built vector types. for vectors of length 2-4.
+
+Each pre-made vectors include x,y,z,w members that can access their respective
+indices in the vector
diff --git a/mvector.hpp b/mvector.hpp
new file mode 100644
index 0000000..b7347d1
--- /dev/null
+++ b/mvector.hpp
@@ -0,0 +1,164 @@
+#ifndef MATH_VECTOR_HPP
+#define MATH_VECTOR_HPP 1
+
+#include <concepts>
+#include <array>
+
+namespace mvec
+{
+
+template<typename T, std::size_t S>
+ requires(std::is_arithmetic_v<T>)
+class vec
+{
+protected:
+ std::array<T, S> v;
+public:
+ constexpr vec(const std::array<T, S> &vs)
+ : v(vs)
+ {}
+ constexpr vec(T vs)
+ : v(vs)
+ {}
+
+ template<std::size_t S2>
+ constexpr vec &operator+=(const vec<T, S2> &rhs) {
+ for(std::size_t i = 0; i < S2 && i < S; i++) v[i] += rhs[i];
+ return *this;
+ }
+
+ template<std::size_t S2>
+ constexpr vec &operator-=(const vec<T, S2> &rhs) {
+ for(std::size_t i = 0; i < S2 && i < S; i++) v[i] -= rhs[i];
+ return *this;
+ }
+ template<std::size_t S2>
+ constexpr vec &operator*=(const vec<T, S2> &rhs) {
+ for(std::size_t i = 0; i < S2 && i < S; i++) v[i] *= rhs[i];
+ return *this;
+ }
+ template<std::size_t S2>
+ constexpr vec &operator/=(const vec<T, S2> &rhs) {
+ for(std::size_t i = 0; i < S2 && i < S; i++) v[i] /= rhs[i];
+ return *this;
+ }
+
+ template<std::size_t S2>
+ friend constexpr vec operator+(vec lhs, const vec<T, S2> &rhs) {
+ lhs += rhs;
+ return lhs;
+ }
+ template<std::size_t S2>
+ friend constexpr vec operator-(vec lhs, const vec<T, S2> &rhs) {
+ lhs -= rhs;
+ return lhs;
+ }
+ template<std::size_t S2>
+ friend constexpr vec operator*(vec lhs, const vec<T, S2> &rhs) {
+ lhs *= rhs;
+ return lhs;
+ }
+ template<std::size_t S2>
+ friend constexpr vec operator/(vec lhs, const vec<T, S2> &rhs) {
+ lhs /= rhs;
+ return lhs;
+ }
+
+ constexpr T &operator[](std::size_t i) {
+ return v[i];
+ }
+ constexpr T operator[](std::size_t i) const {
+ return v[i];
+ }
+};
+
+template<typename T>
+ requires(std::is_arithmetic_v<T>)
+class vec2 final : public vec<T, 2>
+{
+public:
+ T &x;
+ T &y;
+
+ constexpr vec2(T X, T Y) :
+ vec<T, 2>(std::array<T, 2>{X, Y}),
+ x(this->v[0]),
+ y(this->v[1])
+ {}
+ constexpr vec2(std::array<T, 2> vs) :
+ vec<T, 2>(vs),
+ x(this->v[0]),
+ y(this->v[1])
+ {}
+ constexpr vec2(T vs) :
+ vec<T, 2>(vs),
+ x(this->v[0]),
+ y(this->v[1])
+ {}
+};
+
+template<typename T>
+ requires(std::is_arithmetic_v<T>)
+class vec3 final : public vec<T, 3>
+{
+public:
+ T &x;
+ T &y;
+ T &z;
+
+ constexpr vec3(T X, T Y, T Z) :
+ vec<T, 3>(std::array<T, 3>{X, Y, Z}),
+ x(this->v[0]),
+ y(this->v[1]),
+ z(this->v[2])
+ {}
+ constexpr vec3(std::array<T, 3> vs) :
+ vec<T, 3>(vs),
+ x(this->v[0]),
+ y(this->v[1]),
+ z(this->v[2])
+ {}
+ constexpr vec3(T vs) :
+ vec<T, 3>(vs),
+ x(this->v[0]),
+ y(this->v[1]),
+ z(this->v[2])
+ {}
+};
+
+template<typename T>
+ requires(std::is_arithmetic_v<T>)
+class vec4 final : public vec<T, 4>
+{
+public:
+ T &x;
+ T &y;
+ T &z;
+ T &w;
+
+ constexpr vec4(T X, T Y, T Z, T W) :
+ vec<T, 4>(std::array<T, 4>{X, Y, Z, W}),
+ x(this->v[0]),
+ y(this->v[1]),
+ z(this->v[2]),
+ w(this->v[3])
+ {}
+ constexpr vec4(std::array<T, 4> vs) :
+ vec<T, 4>(vs),
+ x(this->v[0]),
+ y(this->v[1]),
+ z(this->v[2]),
+ w(this->v[3])
+ {}
+ constexpr vec4(T vs) :
+ vec<T, 4>(vs),
+ x(this->v[0]),
+ y(this->v[1]),
+ z(this->v[2]),
+ w(this->v[3])
+ {}
+};
+
+}
+
+#endif