Return *this after normalizing

This commit is contained in:
Eryn Wells 2013-09-11 22:07:34 -07:00
parent e76bf0f444
commit fcde85c642
2 changed files with 4 additions and 8 deletions

View file

@ -143,19 +143,15 @@ Vector3::dot(Vector3 v)
* *
* Normalize this vector in place. That is, make this vector's magnitude (length) 1.0. * Normalize this vector in place. That is, make this vector's magnitude (length) 1.0.
*/ */
void Vector3 &
Vector3::normalize() Vector3::normalize()
{ {
float len2 = length2();
if (len2 <= 0.0) {
return;
}
// Multiplying by the inverse of the length is more efficient than dividing by the length. // Multiplying by the inverse of the length is more efficient than dividing by the length.
float inverse_length = 1.0 / sqrtf(len2); float inverse_length = 1.0 / sqrtf(length2());
x *= inverse_length; x *= inverse_length;
y *= inverse_length; y *= inverse_length;
z *= inverse_length; z *= inverse_length;
return *this;
} }

View file

@ -28,7 +28,7 @@ struct Vector3
float length() const; float length() const;
float dot(Vector3 v) const; float dot(Vector3 v) const;
void normalize(); Vector3 &normalize();
static const Vector3 Zero; static const Vector3 Zero;
float x, y, z; float x, y, z;