최태호의 벡터 클래스 입니다. 참고 하세요.
~cpp
# include "math.h"
///////////////////////////////////////////////////////////////////////
//CGX_Vector
# define DG 4
# define EPSILON 0.000001
# define GX_V_E_MULT(AA,B,C){AA[0]=B[0]*C[0]; AA[1]=B[1]*C[1]; AA[2]=B[2]*C[2]; AA[3]=1;}
# define M_PI 3.141592
template <class T>
class CGX_Vector
{
public:
T e[DG];
public:
CGX_Vector();
CGX_Vector<T> operator+(CGX_Vector<T> in);
CGX_Vector<T> operator+(T* in);
CGX_Vector<T> operator-(CGX_Vector<T> in);
CGX_Vector<T> operator-(T* in);
CGX_Vector<T> operator*(T in);
CGX_Vector<T> operator/(T in);
void operator=(CGX_Vector<T> in);
void operator=(T* in);
T& operator[](int in);
CGX_Vector<T> operator*(CGX_Vector<T> in);//cross product
T operator^(CGX_Vector<T> in);//dot product
CGX_Vector<T> Normalize();
CGX_Vector<T> Clip(T v);
T Max();
BOOL equal(CGX_Vector<T> b);
T* GetElement();
float Length();
friend CGX_Vector<T> operator-(CGX_Vector<T> in);
};
//////////////////////////////////////////////////////////////////////////////
//implement
template<class T>
BOOL CGX_Vector<T>::equal( CGX_Vector<T> b)
{
for(int i=0;i<3;i++) if (e[i]- b[i] > EPSILON) return FALSE;
return TRUE;
}
template<class T>
T* CGX_Vector<T>::GetElement()
{
return e;
}
template<class T>
CGX_Vector<T>::CGX_Vector()
{
for (int i=0;i<DG;i++) e[i]=0;
}
template<class T>
CGX_Vector<T> CGX_Vector<T>::operator+(CGX_Vector<T> in)
{
CGX_Vector<T> temp;
for(int count=0;count<DG;count++){
temp.e[count]=e[count] + in.e[count];
}
return temp;
}
template<class T>
CGX_Vector<T> CGX_Vector<T>::operator+(T* in)
{
CGX_Vector<T> temp;
for(int count=0;count<DG;count++){
temp.e[count]=e[count] + in[count];
}
return temp;
}
template<class T>
CGX_Vector<T> CGX_Vector<T>::operator-(CGX_Vector<T> in)
{
CGX_Vector<T> temp;
for(int count=0;count<DG;count++){
temp.e[count]=e[count] - in.e[count];
}
return temp;
}
template<class T>
CGX_Vector<T> CGX_Vector<T>::operator-(T* in)
{
CGX_Vector<T> temp;
for(int count=0;count<DG;count++){
temp.e[count]=e[count] - in[count];
}
return temp;
}
template<class T>
CGX_Vector<T> CGX_Vector<T>::operator*(T in)
{
CGX_Vector<T> temp;
for (int i=0;i<DG;i++) temp.e[i]= e[i]*in;
return temp;
}
template<class T>
CGX_Vector<T> CGX_Vector<T>::operator/(T in)
{
CGX_Vector<T> temp;
for (int i=0;i<DG;i++) temp.e[i]= e[i]/in;
return temp;
}
template<class T>
void CGX_Vector<T>::operator=(CGX_Vector<T> in)
{
for(int count=0;count<DG;count++){
e[count] = in.e[count];
}
}
template<class T>
void CGX_Vector<T>::operator=(T* in)
{
for(int count=0;count<DG;count++){
e[count] = in[count];
}
}
template<class T>
T& CGX_Vector<T>::operator[](int in)
{
return e[in];
}
template<class T>
CGX_Vector<T> CGX_Vector<T>::operator*(CGX_Vector<T> in)
{
CGX_Vector<T> temp;
temp.e[0]=e[1]*in.e[2] - e[2]*in.e[1];
temp.e[1]=e[2]*in.e[0] - e[0]*in.e[2];
temp.e[2]=e[0]*in.e[1] - e[1]*in.e[0];
return temp;
}
///도트 프로덕트..
template<class T>
T CGX_Vector<T>::operator^(CGX_Vector<T> in)
{
T total=0;
for (int i =0;i<DG-1;i++)//homogenious coordinate system 이므로..
total= total + ( e[i] * in.e[i] );
return total;
}
template<class T>
CGX_Vector<T> CGX_Vector<T>::Normalize()
{
CGX_Vector<T> temp;
float length = (float)Length();
for (int i=0;i<DG-1;i++) {
temp.e[i]= e[i]/length;
e[i] = e[i]/length;
}
temp.e[3]=1.0f;
e[3]=1.0f;
return temp;
}
template<class T>
CGX_Vector<T> CGX_Vector<T>::Clip(T v)
{
CGX_Vector<T> temp;
for (int i=0;i<DG-1;i++) {
if (e[i] > v) temp.e[i]=v;
else temp.e[i]=e[i];
}
return temp;
}
template<class T>
T CGX_Vector<T>::Max()
{
T max=-9999999.0f;
for(int i=0;i<DG-1;i++){
if (e[i]>max) max= e[i];
}
return max;
}
template<class T>
float CGX_Vector<T>::Length()
{
return (float)sqrt(e[0]*e[0] +e[1]*e[1]+ e[2]*e[2] );
}
/////////////////////////////////
//많은 define들의 집합..
typedef CGX_Vector<float> GX_V;