added normalize() to Vector, made Ray normalize its direction

git-svn-id: svn://anubis/gvsu@369 45c1a28c-8058-47b2-ae61-ca45b979098e
This commit is contained in:
josh 2009-01-17 19:49:12 +00:00
parent a970d30fed
commit aca3651366
3 changed files with 13 additions and 0 deletions

View File

@ -9,6 +9,7 @@ Ray::Ray(const Vector & origin, const Vector & direction)
{
m_origin = origin;
m_direction = direction;
m_direction.normalize();
}
/*

View File

@ -1,6 +1,7 @@
#include "Vector.h"
#include <iostream>
#include <math.h>
Vector::Vector()
{
@ -9,6 +10,16 @@ Vector::Vector()
m_array[2] = 0.0;
}
void Vector::normalize()
{
double length = sqrt(m_array[0] * m_array[0]
+ m_array[1] * m_array[1]
+ m_array[2] * m_array[2]);
m_array[0] /= length;
m_array[1] /= length;
m_array[2] /= length;
}
std::ostream & operator<<(std::ostream & out, const Vector & v)
{
out << "[" << v[0] << ", " << v[1] << ", " << v[2] << "]";

View File

@ -11,6 +11,7 @@ class Vector
~Vector();
double & operator[](int idx) { return m_array[idx]; }
double operator[](int idx) const { return m_array[idx]; }
void normalize();
private:
double m_array[3];