From f5ca6832bc838844f4468738213696af6370ffb2 Mon Sep 17 00:00:00 2001 From: Quinn Date: Wed, 4 Dec 2024 17:52:32 -0500 Subject: [PATCH] Initial commit --- Vector3D.h | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 Vector3D.h diff --git a/Vector3D.h b/Vector3D.h new file mode 100644 index 0000000..cb60300 --- /dev/null +++ b/Vector3D.h @@ -0,0 +1,80 @@ +#pragma once + +#include +#include + +template +class V3D{ + public: + constexpr V3D(const V3D& other): + x(other.x), + y(other.y), + z(other.z){ + static_assert(std::is_arithmetic::value, "Type must be a number"); + } + + constexpr V3D(Type x=0, Type y=0, Type z=0): + x(x), + y(y), + z(z){ + static_assert(std::is_arithmetic::value, "Type must be a number"); + } + + template + constexpr V3D(const V3D other): + x(static_cast(other.x)), + y(static_cast(other.y)), + z(static_cast(other.z)){ + static_assert(std::is_arithmetic::value, "Type must be a number"); + static_assert(std::is_arithmetic::value, "OtherType must be a number"); + } + + V3D& operator=(const V3D &other){ + this->x = other.x; + this->y = other.y; + this->z = other.z; + return *this; + } + + V3D& operator+=(const V3D &other){ + this->x += other.x; + this->y += other.y; + this->z += other.z; + return *this; + } + + V3D& operator-=(const V3D &other){ + this->x -= other.x; + this->y -= other.y; + this->z -= other.z; + return *this; + } + + V3D& operator/=(const Type scalar){ + if(scalar == 0){ + return *this; + } + this->x /= scalar; + this->y /= scalar; + this->z /= scalar; + return *this; + } + + V3D& operator*=(const Type scalar){ + this->x *= scalar; + this->y *= scalar; + this->z *= scalar; + return *this; + } + + bool operator==(const V3D &other){ + return this->x == other.x && this->y == other.y && this->z == other.z; + } + + float magnitude(){ + return std::sqrt(static_cast(this->x * this->x + this->y * this->y + this->z * this->z)); + } + Type x; + Type y; + Type z; +}; \ No newline at end of file