fart/util/Vector.cc
Josh Holtrop 20eafaf9f4 fixed view plane distance calculation to use tan() and convert degrees to radians
git-svn-id: svn://anubis/fart/trunk@41 7f9b0f55-74a9-4bce-be96-3c2cd072584d
2009-01-23 16:44:39 +00:00

64 lines
1.3 KiB
C++

#include "Vector.h"
#include <iostream>
#include <math.h>
Vector::Vector()
{
m_array[0] = 0.0;
m_array[1] = 0.0;
m_array[2] = 0.0;
}
Vector::Vector(double x, double y, double z)
{
m_array[0] = x;
m_array[1] = y;
m_array[2] = z;
}
Vector & Vector::normalize()
{
double length = mag();
m_array[0] /= length;
m_array[1] /= length;
m_array[2] /= length;
return *this;
}
double Vector::mag()
{
return sqrt(m_array[0] * m_array[0]
+ m_array[1] * m_array[1]
+ m_array[2] * m_array[2]);
}
double Vector::mag2()
{
return m_array[0] * m_array[0]
+ m_array[1] * m_array[1]
+ m_array[2] * m_array[2];
}
std::ostream & operator<<(std::ostream & out, const Vector & v)
{
out << "[" << v[0] << ", " << v[1] << ", " << v[2] << "]";
return out;
}
/* Compute the dot-product of two vectors */
double operator%(const Vector & v1, const Vector & v2)
{
return v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2];
}
/* Compute the cross-product of two vectors */
Vector operator*(const Vector & v1, const Vector & v2)
{
Vector result;
result[0] = v1[1] * v2[2] - v1[2] * v2[1];
result[1] = v1[2] * v2[0] - v1[0] * v2[2];
result[2] = v1[0] * v2[1] - v1[1] * v2[0];
return result;
}