rtsPoint3d.h 3.07 KB
#ifndef RTSPOINT3D_H
#define RTSPOINT3D_H
#include "rtsVector3d.h"

///This class is used to store information about a 3D point.  It is designed to work with the class vector3D<T> for linear algebra operations.

template <class T> class point3D
{
	//friend class vector3D<T>;
public:
	//data values
	T x;
	T y;
	T z;

	point3D();									///<Constructor initializes the point at the origin (0,0,0).
	point3D(T new_x, T new_y, T new_z);			///<Constructor initializes the point to the position specified by (new_x, new_y, new_z).

	//operators
	bool operator==(point3D<T> param);			///<Overloaded boolean operator.  Returns true if the two oparands are at the same position.
	bool operator!=(point3D<T> param);			///<Overloaded boolean operator for !=
	point3D<T> operator+(vector3D<T> param);	///<Overloaded arithmetic oparator.  Returns the sum of the current point and a given vector.
	point3D<T> operator-(vector3D<T> param);	///<Overloaded arithmetic operator.  Returns the sum of the current point and the negative of a given vector.
	vector3D<T> operator-(point3D<T> param);	///<Overloaded arithmetic operator.  Returns the vector representing the space between two points.

	//casting operators
	template <class U> operator point3D<U>();  ///<Overloaded casting operator.  Allows casting between points of different types.

	//output
	friend ostream& operator<<(ostream& os, const point3D<T> &rhs)
	{
		os<<"("<<rhs.x<<","<<rhs.y<<","<<rhs.z<<")";
		return os;
	}

	/*point3D<T> & operator=(const point3D<T> & rhs)
	{
		if(this != &rhs)
		{
			x = rhs.x;
			y = rhs.y;
			z = rhs.z;
		}
		return *this;
	}*/
	
	void print();
};

template <class T>
template <class U>
point3D<T>::operator point3D<U>()
{
	point3D<U> cast_result(x, y, z);
	return cast_result;
}

template <class T> point3D<T>::point3D()
{
	x=0;
	y=0;
	z=0;
}

template <class T> point3D<T>::point3D(T new_x, T new_y, T new_z)
{
	x=new_x;
	y=new_y;
	z=new_z;
}

template <class T>
point3D<T> point3D<T>::operator -(vector3D<T> param)
{
	point3D<T> result;			//create the result
	
	result.x = x-param.x;		//perform the computation
	result.y = y-param.y;
	result.z = z-param.z;

	return result;
}

template <class T>
vector3D<T> point3D<T>::operator -(point3D<T> param)
{
	vector3D<T> result;
	result.x = x - param.x;
	result.y = y - param.y;
	result.z = z - param.z;
	
	return result;
}

template <class T>
point3D<T> point3D<T>::operator+(vector3D<T> param)
{
	point3D<T> result;			//create the result
	
	result.x = x+param.x;		//perform the computation
	result.y = y+param.y;
	result.z = z+param.z;

	return result;
}

template <class T>
bool point3D<T>::operator ==(point3D<T> param)
{
	if(x == param.x && y == param.y && z == param.z)
		return true;
	else
		return false;
}

template <class T>
bool point3D<T>::operator !=(point3D<T> param)
{
	if(x != param.x || y != param.y || z != param.z)
		return true;
	else
		return false;
}



template <class T>
void point3D<T>::print()
{
	cout<<"("<<x<<","<<y<<","<<z<<")"<<endl;
}

/*template <class T>
ostream& point3D<T>::operator<<(ostream& os, point3D<T> &rhs)
{
	os<<"("<<x<<","<<y<<","<<z<<")";
	return os;
}
*/

#endif