Vector4 +, -, negation, and *

+, -, and negation were easy. Multiplication is wacky because the Matrix class does it. So, to do binary * (not *=) I needed to dynamic_cast the resulting Matrix<4,1> to a Vector4&&.
This commit is contained in:
Eryn Wells 2014-08-09 09:32:48 -07:00
parent a3d51f7cf3
commit b41cdb7186
2 changed files with 94 additions and 0 deletions

View file

@ -78,6 +78,9 @@ Vector4::Z()
}
/*
* charles::basics::Vector4::Z --
*/
const Double&
Vector4::Z()
const
@ -86,6 +89,73 @@ Vector4::Z()
}
/*
* charles::basics::Vector4::operator* --
*/
Vector4
Vector4::operator*(const Double& rhs)
const
{
return dynamic_cast<Vector4&&>(*this * rhs);
}
/*
* charles::basics::Vector4::operator+ --
*/
Vector4
Vector4::operator+(const Vector4& rhs)
const
{
return Vector4(*this) += rhs;
}
/*
* charles::basics::Vector4::operator+= --
*/
Vector4&
Vector4::operator+=(const Vector4& rhs)
{
mData[0] += rhs.mData[0];
mData[1] += rhs.mData[1];
mData[2] += rhs.mData[2];
return *this;
}
/*
* charles::basics::Vector4::operator- --
*/
Vector4
Vector4::operator-(const Vector4& rhs)
const
{
return Vector4(*this) -= rhs;
}
/*
* charles::basics::Vector4::operator-= --
*/
Vector4&
Vector4::operator-=(const Vector4& rhs)
{
return *this += -rhs;
}
/*
* charles::basics::Vector4::operator- --
*/
Vector4
Vector4::operator-()
const
{
return Vector4(-X(), -Y(), -Z());
}
/*
* charles::basics::Vector4::Length2 --
*/
@ -146,6 +216,17 @@ Vector4::Normalize()
return *this;
}
/*
* charles::basics::operator* --
*/
Vector4
operator*(const Double& lhs,
const Vector4& rhs)
{
return rhs * lhs;
}
} /* namespace basics */
} /* namespace charles */

View file

@ -32,6 +32,15 @@ struct Vector4
Double& Z();
const Double& Z() const;
Vector4 operator*(const Double& rhs) const;
Vector4 operator+(const Vector4& rhs) const;
Vector4& operator+=(const Vector4& rhs);
Vector4 operator-(const Vector4& rhs) const;
Vector4& operator-=(const Vector4& rhs);
Vector4 operator-() const;
/** Get the length-squared of this vector. */
Double Length2() const;
@ -49,6 +58,10 @@ struct Vector4
};
/** Scalar multiplication of vectors, with the scalar factor on the left. */
Vector4 operator*(const Double& lhs, const Vector4& rhs);
/** Normalize the given vector and return a copy of it. */
Vector4& Normalized(const Vector4& v);