Show Vector3f.cpp syntax highlighted
#include "StdAfx.h"
#include "Vector3f.h"
/** Zero out the vector */
void Vector3f::ZeroOut()
{
for( int i = 0; i < 3;i++ )
m_data[i] = 0;
}
/** Length/magnatude of the vector */
float Vector3f::Length() const
{
return sqrt(m_data[0]*m_data[0] +
m_data[1]*m_data[1] +
m_data[2]*m_data[2] );
}
/** Normalize the vector */
void Vector3f::Normalize()
{
float length = Length();
m_data[0] = m_data[0] / length;
m_data[1] = m_data[1] / length;
m_data[2] = m_data[2] / length;
}
/** Multiply two vectors */
void Vector3f::Multiply( const Vector3f &v )
{
}
/** Multiply by a scalar */
Vector3f Vector3f::Multiply( float scalar ) const
{
return Vector3f( m_data[0] * scalar,
m_data[1] * scalar,
m_data[2] * scalar );
}
/** Divide by a scalar */
Vector3f Vector3f::Divide( float scalar ) const
{ // do not divide by 0
scalar = (scalar) ? scalar : 1;
return Vector3f( m_data[0] / scalar,
m_data[1] / scalar,
m_data[2] / scalar );
}
/** Add */
Vector3f Vector3f::Add( Vector3f &v ) const
{
float* vdata = v.GetData();
return Vector3f( m_data[0] + vdata[0],
m_data[1] + vdata[1],
m_data[2] + vdata[2] );
}
/** Subtract */
Vector3f Vector3f::Subtract( Vector3f &v ) const
{
float* vdata = v.GetData();
return Vector3f( m_data[0] - vdata[0],
m_data[1] - vdata[1],
m_data[2] - vdata[2] );
}
/** Dot Product of two vectors */
float Vector3f::DotProduct( Vector3f &v )
{
float* vdata = v.GetData();
return m_data[0]*vdata[0] +
m_data[1]*vdata[1] +
m_data[2]*vdata[2];
}
/** Cross Product of two vectors */
Vector3f Vector3f::CrossProduct( Vector3f &v ) const
{
float* vdata = v.GetData();
return Vector3f( m_data[1] * vdata[2] - m_data[2] * vdata[1],
m_data[2] * vdata[0] - m_data[0] * vdata[2],
m_data[0] * vdata[1] - m_data[1] * vdata[0] );
}
/** Angle between two vectors */
float Vector3f::Angle( Vector3f &v )
{
// normalize first
Normalize();
v.Normalize();
// get the dotproduct of the two
float dot = DotProduct( v );
float len = Length();
float len2 = v.Length();
// now take the arc cos
return acos( dot/ ( len * len2 ) );
}
/** Set the vector */
void Vector3f::Set( float x, float y, float z )
{
m_data[0] = x; m_data[1] = y; m_data[2] = z;
}
void Vector3f::Set( float* v )
{
Set( v[0], v[1], v[2] );
}
void Vector3f::Set( const Vector3f &v )
{
Set( v.GetX(), v.GetY(), v.GetZ() );
}
See more files for this project here