From 7b1f1372e3993860bd0909e6851952fffc5d097f Mon Sep 17 00:00:00 2001 From: Jon Santmyer Date: Sun, 20 Nov 2022 23:35:33 -0500 Subject: initial commit. math using arbitrary length vectors --- README.MD | 17 +++++++ mvector.hpp | 164 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 181 insertions(+) create mode 100644 README.MD create mode 100644 mvector.hpp 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 +``` +or +``` +mvec;:vec2 +``` +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 +#include + +namespace mvec +{ + +template + requires(std::is_arithmetic_v) +class vec +{ +protected: + std::array v; +public: + constexpr vec(const std::array &vs) + : v(vs) + {} + constexpr vec(T vs) + : v(vs) + {} + + template + constexpr vec &operator+=(const vec &rhs) { + for(std::size_t i = 0; i < S2 && i < S; i++) v[i] += rhs[i]; + return *this; + } + + template + constexpr vec &operator-=(const vec &rhs) { + for(std::size_t i = 0; i < S2 && i < S; i++) v[i] -= rhs[i]; + return *this; + } + template + constexpr vec &operator*=(const vec &rhs) { + for(std::size_t i = 0; i < S2 && i < S; i++) v[i] *= rhs[i]; + return *this; + } + template + constexpr vec &operator/=(const vec &rhs) { + for(std::size_t i = 0; i < S2 && i < S; i++) v[i] /= rhs[i]; + return *this; + } + + template + friend constexpr vec operator+(vec lhs, const vec &rhs) { + lhs += rhs; + return lhs; + } + template + friend constexpr vec operator-(vec lhs, const vec &rhs) { + lhs -= rhs; + return lhs; + } + template + friend constexpr vec operator*(vec lhs, const vec &rhs) { + lhs *= rhs; + return lhs; + } + template + friend constexpr vec operator/(vec lhs, const vec &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 + requires(std::is_arithmetic_v) +class vec2 final : public vec +{ +public: + T &x; + T &y; + + constexpr vec2(T X, T Y) : + vec(std::array{X, Y}), + x(this->v[0]), + y(this->v[1]) + {} + constexpr vec2(std::array vs) : + vec(vs), + x(this->v[0]), + y(this->v[1]) + {} + constexpr vec2(T vs) : + vec(vs), + x(this->v[0]), + y(this->v[1]) + {} +}; + +template + requires(std::is_arithmetic_v) +class vec3 final : public vec +{ +public: + T &x; + T &y; + T &z; + + constexpr vec3(T X, T Y, T Z) : + vec(std::array{X, Y, Z}), + x(this->v[0]), + y(this->v[1]), + z(this->v[2]) + {} + constexpr vec3(std::array vs) : + vec(vs), + x(this->v[0]), + y(this->v[1]), + z(this->v[2]) + {} + constexpr vec3(T vs) : + vec(vs), + x(this->v[0]), + y(this->v[1]), + z(this->v[2]) + {} +}; + +template + requires(std::is_arithmetic_v) +class vec4 final : public vec +{ +public: + T &x; + T &y; + T &z; + T &w; + + constexpr vec4(T X, T Y, T Z, T W) : + vec(std::array{X, Y, Z, W}), + x(this->v[0]), + y(this->v[1]), + z(this->v[2]), + w(this->v[3]) + {} + constexpr vec4(std::array vs) : + vec(vs), + x(this->v[0]), + y(this->v[1]), + z(this->v[2]), + w(this->v[3]) + {} + constexpr vec4(T vs) : + vec(vs), + x(this->v[0]), + y(this->v[1]), + z(this->v[2]), + w(this->v[3]) + {} +}; + +} + +#endif -- cgit v1.2.1