Implement a bunch of the useful Vector operations

This commit is contained in:
Eryn Wells 2014-08-08 23:05:38 -07:00
parent 9b076f1533
commit fa3708edfb
2 changed files with 65 additions and 0 deletions

View file

@ -3,6 +3,8 @@
* Eryn Wells <eryn@erynwells.me>
*/
#include <cmath>
#include "basics/vector.hh"
@ -60,6 +62,50 @@ Vector4::Z()
return mData[2];
}
/*
* charles::basics::Vector4::length2 --
*/
Double
Vector4::Length2()
const
{
return mData[0] * mData[0] + mData[1] * mData[1] + mData[2] * mData[2];
}
Double
Vector4::Length()
const
{
return std::sqrt(Length2());
}
Double
Vector4::Dot(const Vector4& rhs)
const
{
return mData[0] * rhs.mData[0] + mData[1] * rhs.mData[1] + mData[2] + rhs.mData[2];
}
Vector4
Vector4::Cross(const Vector4& rhs)
const
{
return Vector4(mData[1]*rhs.mData[2] - mData[2]*rhs.mData[1],
mData[2]*rhs.mData[0] - mData[0]*rhs.mData[2],
mData[0]*rhs.mData[1] - mData[1]*rhs.mData[0]);
}
Vector4&
Vector4::Normalize()
{
return *this /= Length();
}
} /* namespace basics */
} /* namespace charles */

View file

@ -28,8 +28,27 @@ struct Vector4
Double& X();
Double& Y();
Double& Z();
/** Get the length-squared of this vector. */
Double Length2() const;
/** Get the length of this vector. */
Double Length() const;
/** Get the dot product of `this` and `rhs`. */
Double Dot(const Vector4& rhs) const;
/** Get the cross product of `this` and `rhs`. */
Vector4 Cross(const Vector4& rhs) const;
/** Normalize this vector. */
Vector4& Normalize();
};
/** Normalize the given vector and return a copy of it. */
Vector4& Normalized(const Vector4& v);
} /* namespace basics */
} /* namespace charles */