From d44fa4dd2a0e25452758a5cda674f77a9d86db44 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Sat, 2 Aug 2014 10:00:00 -0700 Subject: [PATCH] Add Vector3::normalized() Makes a copy of the vector, normalizes it, and returns it. --- src/basics.cc | 13 +++++++++++-- src/basics.h | 4 ++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/basics.cc b/src/basics.cc index 512cd1b..46361bc 100644 --- a/src/basics.cc +++ b/src/basics.cc @@ -220,8 +220,6 @@ Vector3::cross(const Vector3 &v) /* * Vector3::normalize -- - * - * Normalize this vector in place. That is, make this vector's magnitude (length) 1.0. */ Vector3 & Vector3::normalize() @@ -231,6 +229,17 @@ Vector3::normalize() } +/* + * Vector3::normalized -- + */ +Vector3 +Vector3::normalized() + const +{ + return *this / length(); +} + + /* * operator* -- * diff --git a/src/basics.h b/src/basics.h index bfd076e..b9bba89 100644 --- a/src/basics.h +++ b/src/basics.h @@ -47,8 +47,12 @@ struct Vector3 Double dot(const Vector3 &v) const; Vector3 cross(const Vector3 &v) const; + /** Normalize and return a reference to this vector. */ Vector3 &normalize(); + /** Return a copy of this vector, normalized. Does not modify this vector. */ + Vector3 normalized() const; + static const Vector3 Zero; // Unit vectors in each of the three cartesian directions. static const Vector3 X, Y, Z;