ps2vector4.cpp

00001 /*
00002 * "PS2" Application Framework
00003 *
00004 * University of Abertay Dundee
00005 * May be used for educational purposed only
00006 *
00007 * Author - Dr Henry S Fortuna
00008 *
00009 * $Revision: 1.4 $
00010 * $Date: 2007/08/27 19:37:12 $
00011 *
00012 */#include "ps2vector4.h"
00013 
00014 Vector4::Vector4(void)
00015 {
00016 }
00017 
00018 Vector4::Vector4(const float _x, const float _y, const float _z, const float _w)
00019 :x(_x),y(_y),z(_z),w(_w)
00020 {
00021 }
00022 
00023 // Copy constructor
00024 Vector4::Vector4(const Vector4 &rhs)
00025 :x(rhs.x),y(rhs.y),z(rhs.z),w(rhs.w)
00026 {
00027 }
00028 
00029 Vector4::~Vector4(void)
00030 {
00031 }
00032 
00033 Vector4 & Vector4::operator +=(const Vector4 &rhs)
00034 {
00035         x += rhs.x;
00036         y += rhs.y;
00037         z += rhs.z;
00038         w = 1.0f;
00039 
00040         return *this;
00041 }
00042 
00043 Vector4 & Vector4::operator -=(const Vector4 &rhs)
00044 {
00045         x -= rhs.x;
00046         y -= rhs.y;
00047         z -= rhs.z;
00048         w = 1.0f;
00049 
00050         return *this;
00051 }
00052 
00053 float Vector4::Dot4(const Vector4 &rhs) const
00054 {
00055         return (x * rhs.x + y * rhs.y + z * rhs.z + w * rhs.w);
00056 }
00057 
00058 float Vector4::Dot3(const Vector4 &rhs) const
00059 {
00060         return (x * rhs.x + y * rhs.y + z * rhs.z);
00061 }
00062 
00063 Vector4 Vector4::Cross(const Vector4 &rhs) const
00064 {
00065         return Vector4(y * rhs.z - z * rhs.y, z * rhs.x - x * rhs.z, x * rhs.y - y * rhs.x, 1.0f); 
00066 }
00067 
00068 Vector4 & Vector4::operator *=(const float s)
00069 {
00070         x *= s;
00071         y *= s;
00072         z *= s;
00073         w  = 1.0f;
00074 
00075         return *this;
00076 }
00077 
00078 Vector4 & Vector4::operator /=(const float s)
00079 {
00080         x /= s;
00081         y /= s;
00082         z /= s;
00083         w = 1.0f;;
00084 
00085         return *this;
00086 }
00087 
00088 float Vector4::Length() const
00089 {
00090         return Sqrt(x * x + y * y + z * z);
00091 }
00092 
00093 float Vector4::LengthSqr() const
00094 {
00095         return (x * x + y * y + z * z);
00096 }
00097 
00098 
00099 bool Vector4::operator ==(const Vector4 & rhs) const
00100 {
00101         return ((x == rhs.x) && (y == rhs.y) && (z == rhs.z) && (w == rhs.w));
00102 }
00103 
00104 // Return the ZeroVector if the lenth of the vector is very small
00105 Vector4 Vector4::Normalise()
00106 {
00107         float tmp_Length;
00108         Vector4 ZeroVector = Vector4(0.0f, 0.0f, 0.0f, 0.0f);
00109         
00110         tmp_Length = this->Length();
00111         if(tmp_Length < 0.0001f)
00112                 return ZeroVector;
00113         
00114         return (*this / this->Length());
00115 }
00116 
00117 // Set to the ZeroVector if the lenth of the vector is very small
00118 void Vector4::NormaliseSelf()
00119 {
00120         float tmp_Length;
00121         Vector4 ZeroVector = Vector4(0.0f, 0.0f, 0.0f, 0.0f);
00122         
00123         tmp_Length = this->Length();
00124         if(tmp_Length < 0.0001f)
00125                 *this = ZeroVector;
00126         else
00127                 *this /= this->Length();
00128 }
00129 
00130 // return 1 if the vector is a zero vector, 0 otherwise
00131 int Vector4::IsZeroVector(void)
00132 {
00133         if(x != 0.0)return 0;
00134         if(y != 0.0)return 0;
00135         if(z != 0.0)return 0;
00136         if(w != 0.0)return 0;
00137         return 1;
00138 }
00139 
00140 void Vector4::DumpVector4(char * s)
00141 {
00142         if(s != NULL)printf("\n%f %f %f %f %s\n\n", x, y, z, w, s);
00143         else printf("\n%f %f %f %f\n\n", x, y, z, w);
00144 }

Generated on Sun May 18 21:45:09 2008 for PS2X by  doxygen 1.5.4