Added scalar addition / subtraction

This commit is contained in:
2025-02-04 14:33:29 -05:00
parent 437d209200
commit ab2d9f002b
2 changed files with 34 additions and 0 deletions

View File

@@ -2,6 +2,7 @@
// will evaluate to true // will evaluate to true
#include <cmath> #include <cmath>
#include <type_traits> #include <type_traits>
#include <string>
template <typename Type> template <typename Type>
V3D<Type>::V3D(const Matrix<1, 3> &other) V3D<Type>::V3D(const Matrix<1, 3> &other)
@@ -60,12 +61,24 @@ void V3D<Type>::operator=(const V3D<Type> &other)
this->z = other.z; this->z = other.z;
} }
template <typename Type>
V3D<Type> V3D<Type>::operator+(Type other) const
{
return V3D<Type>{this->x + other, this->y + other, this->z + other};
}
template <typename Type> template <typename Type>
V3D<Type> V3D<Type>::operator+(const V3D<Type> &other) const V3D<Type> V3D<Type>::operator+(const V3D<Type> &other) const
{ {
return V3D<Type>{this->x + other.x, this->y + other.y, this->z + other.z}; return V3D<Type>{this->x + other.x, this->y + other.y, this->z + other.z};
} }
template <typename Type>
V3D<Type> V3D<Type>::operator-(Type other) const
{
return V3D<Type>{this->x - other, this->y - other, this->z - other};
}
template <typename Type> template <typename Type>
V3D<Type> V3D<Type>::operator-(const V3D<Type> &other) const V3D<Type> V3D<Type>::operator-(const V3D<Type> &other) const
{ {
@@ -84,6 +97,13 @@ V3D<Type> V3D<Type>::operator/(Type scalar) const
return V3D<Type>{this->x / scalar, this->y / scalar, this->z / scalar}; return V3D<Type>{this->x / scalar, this->y / scalar, this->z / scalar};
} }
template <typename Type>
V3D<Type> &V3D<Type>::operator+=(Type other)
{
*this = *this + other;
return *this;
}
template <typename Type> template <typename Type>
V3D<Type> &V3D<Type>::operator+=(const V3D<Type> &other) V3D<Type> &V3D<Type>::operator+=(const V3D<Type> &other)
{ {
@@ -91,6 +111,13 @@ V3D<Type> &V3D<Type>::operator+=(const V3D<Type> &other)
return *this; return *this;
} }
template <typename Type>
V3D<Type> &V3D<Type>::operator-=(Type other)
{
*this = *this - other;
return *this;
}
template <typename Type> template <typename Type>
V3D<Type> &V3D<Type>::operator-=(const V3D<Type> &other) V3D<Type> &V3D<Type>::operator-=(const V3D<Type> &other)
{ {

View File

@@ -18,10 +18,15 @@ public:
template <typename OtherType> template <typename OtherType>
V3D(const V3D<OtherType> &other); V3D(const V3D<OtherType> &other);
template <typename OtherType>
operator OtherType() const;
std::array<Type, 3> ToArray() const; std::array<Type, 3> ToArray() const;
V3D<Type> operator+(Type other) const;
V3D<Type> operator+(const V3D<Type> &other) const; V3D<Type> operator+(const V3D<Type> &other) const;
V3D<Type> operator-(Type other) const;
V3D<Type> operator-(const V3D<Type> &other) const; V3D<Type> operator-(const V3D<Type> &other) const;
V3D<Type> operator*(Type scalar) const; V3D<Type> operator*(Type scalar) const;
@@ -30,8 +35,10 @@ public:
void operator=(const V3D<Type> &other); void operator=(const V3D<Type> &other);
V3D<Type> &operator+=(Type other);
V3D<Type> &operator+=(const V3D<Type> &other); V3D<Type> &operator+=(const V3D<Type> &other);
V3D<Type> &operator-=(Type other);
V3D<Type> &operator-=(const V3D<Type> &other); V3D<Type> &operator-=(const V3D<Type> &other);
V3D<Type> &operator/=(Type scalar); V3D<Type> &operator/=(Type scalar);