www.pudn.com > Ray_Tracing_Materials.rar > CVector.cpp, change:2005-03-02,size:3339b
/*
Class Name:
CVector4.
Created by:
Allen Sherrod (Programming Ace of www.UltimateGameProgramming.com).
Description:
This class represents a 3D (well 4D) point for a vertex's x, y, z, and w axis.
*/
#include"CVector.h"
#define M_PI 3.141592654
CVector4::CVector4()
{
// Initialize the variables to 0.
x = y = z = 0.0;
}
CVector4::CVector4(float X, float Y, float Z)
{
// Initialize the varibles to the data in X, Y, and Z.
x = X;
y = Y;
z = Z;
}
CVector4::CVector4(float X, float Y, float Z, float W)
{
// Initialize the varibles to the data in X, Y, and Z.
x = X;
y = Y;
z = Z;
w = W;
}
void CVector4::operator =(CVector4 v)
{
// Make this objects x, y, and z equal to the object on the right of the = sign.
x = v.x;
y = v.y;
z = v.z;
w = v.w;
}
CVector4 CVector4::operator -(CVector4 v)
{
// Return the value of this vector - v.
return CVector4(x - v.x, y - v.y, z - v.z);
}
CVector4 CVector4::operator +(CVector4 v)
{
// Return the value of this vector + v.
return CVector4(x + v.x, y + v.y, z + v.z);
}
CVector4 CVector4::operator *(CVector4 v)
{
// Return the value of this vector * v.
return CVector4(x * v.x, y * v.y, z * v.z);
}
CVector4 CVector4::operator /(CVector4 v)
{
// Return the value of this vector / v.
return CVector4(x / v.x, y / v.y, z / v.z);
}
void CVector4::operator +=(CVector4 v)
{
// Add this by object v and store results here.
x += v.x;
y += v.y;
z += v.z;
}
void CVector4::operator -=(CVector4 v)
{
// Subtract this by object v and store results here.
x -= v.x;
y -= v.y;
z -= v.z;
}
void CVector4::operator *=(CVector4 v)
{
// Mul this by object v and store results here.
x *= v.x;
y *= v.y;
z *= v.z;
}
void CVector4::operator /=(CVector4 v)
{
// Divide this by object v and store results here.
x /= v.x;
y /= v.y;
z /= v.z;
}
bool CVector4::operator ==(CVector4 v)
{
// Return true if all equal each other, false if one or more don't.
return ((x == v.x) && (y== v.y) && (z == v.z));
}
bool CVector4::operator !=(CVector4 v)
{
// Return true if one or all don't equal each other, false if they equal.
return !((x == v.x) && (y== v.y) && (z == v.z));
}
void CVector4::CrossProduct(CVector4 v1, CVector4 v2)
{
// Get the cross product of v1 and v2 and store it in this vector.
x = ((v1.y * v2.z) - (v1.z * v2.y));
y = ((v1.z * v2.x) - (v1.x * v2.z));
z = ((v1.x * v2.y) - (v1.y * v2.x));
}
float CVector4::DotProduct3(CVector4 v1)
{
// Get the dot product of v1 and this object and return it.
return x * v1.x + y * v1.y + z * v1.z;
}
float CVector4::GetLength()
{
// Return the length for this object.
return (float)sqrt((x * x + y * y + z * z));
}
void CVector4::Normal()
{
// Reduce this object to a unit vector.
float lenght = GetLength();
if(lenght == 0.0f) lenght = 1.0f;
x = x/lenght;
y = y/lenght;
z = z/lenght;
w = w/lenght;
}
// Copyright February 2005
// All Rights Reserved!
// Allen Sherrod
// ProgrammingAce@UltimateGameProgramming.com
// www.UltimateGameProgramming.com