www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - vector

reply tm030127d galeb.etf.bg.ac.yu writes:
How to rewrite this class in D?

template<class T>
struct Vec3
{
	T x, y, z;

	Vec3(T X = 0, T Y = 0, T Z = 0): x(X), y(Y), z(Z) {}
	Vec3<T> operator =(T a) {x=a; y=a; z=a; return *this;}
	void operator()(T X, T Y, T Z) {x=X; y=Y; z=Z;}

	Vec3<T> operator +(const Vec3<T>& V) const {return Vec3<T>(x+V.x, y+V.y,  
z+V.z);}
	Vec3<T> operator -(const Vec3<T>& V) const {return Vec3<T>(x-V.x, y-V.y,  
z-V.z);}
	Vec3<T> operator -() const {return Vec3<T>(-x,-y,-z);}

	Vec3<T> operator *(T a) const {return Vec3<T>(x*a, y*a, z*a);}
	Vec3<T> operator /(T a) const {return Vec3<T>(x/a, y/a, z/a);}

	void operator +=(const Vec3<T>& V) {x+=V.x; y+=V.y; z+=V.z;}
	void operator -=(const Vec3<T>& V) {x-=V.x; y-=V.y; z-=V.z;}

	void operator *=(T a) {x*=a; y*=a; z*=a;}
	void operator /=(T a) {x/=a; y/=a; z/=a;}

	T operator *(const Vec3<T>& V) const {return x*V.x + y*V.y + z*V.z;}
	Vec3<T> operator ^(const Vec3<T>& V) const {return Vec3<T>(y*V.z - z*V.y,  
z*V.x - x*V.z, x*V.y - y*V.x);}

	T sqr() const {return x*x + y*y + z*z;}
	T operator ~() const {return sqrt(sqr());}
	Vec3<T> operator !() const {return (*this)/~(*this);}
};
Aug 31 2004
parent Stephen Waits <steve waits.net> writes:
tm030127d galeb.etf.bg.ac.yu wrote:

     T operator *(const Vec3<T>& V) const {return x*V.x + y*V.y + z*V.z;}
     Vec3<T> operator ^(const Vec3<T>& V) const {return Vec3<T>(y*V.z - 
 z*V.y,  z*V.x - x*V.z, x*V.y - y*V.x);}
Icky. s/operator \*/Dot/ s/operator \^/Cross/ --Steve
Aug 31 2004