Template: myPoint
A point can be (x,y), or (x,y,z). Their data type can be int, long, float, double, sign/unsigned.
But operations are the same. So use template!
// myPoint.h: interface for the myPoint class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_MYPOINT_LINGFA_YANG_INCLUDED_)
#define AFX_MYPOINT_LINGFA_YANG_INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include
template
class MyPoint2D
{
public :
T x,y;
MyPoint2D(const CPoint& p) { x = p.x; y = p.y;}
MyPoint2D(MyPoint2D *p){x = p->x ; y = p->y; }
MyPoint2D(T a, T b) : x(a), y(b){}
MyPoint2D(){x=y=0;}
virtual ~MyPoint2D(){}
MyPoint2D* Get() {return this;}
// operators' overloading
MyPoint2D operator*(const T a)
{
return MyPoint2D (x*a,y*a);
}
MyPoint2D operator/(const T a)
{
return MyPoint2D (x/a,y/a);
}
MyPoint2D operator+(const MyPoint2D p){return MyPoint2D (x + p.x, y + p.y);}
MyPoint2D operator-(const MyPoint2D p){return MyPoint2D (x - p.x, y - p.y);}
MyPoint2D operator*(const MyPoint2D p){return MyPoint2D (x * p.x, y * p.y);} // dot product
// unary operator definition
MyPoint2D operator-()
{
return MyPoint2D (-x,-y);
}
MyPoint2D& operator=(const MyPoint2D &p){x = p.x; y = p.y; return (*this);} // Copy constructor
MyPoint2D& operator+=(const MyPoint2D &p){*this = *this + p; return (*this);}
MyPoint2D& operator-=(const MyPoint2D &p){*this = *this - p; return (*this);}
MyPoint2D& operator*=(const MyPoint2D &p){*this = *this * p; return (*this);}
BOOL operator==(MyPoint2D p)
{
if( x != p.x ) return FALSE;
if( y != p.y ) return FALSE;
return TRUE;
}
BOOL operator!=(MyPoint2D p)
{
return !operator==( p );
}
double radius(){return ( sqrt( x*x + y*y ) );}
MyPoint2D& normalize()
{
double r = this->radius();
x /= r;
y /= r;
return *this;
}
double d2( MyPoint2D p) // Distance between this point to the specified point
{
return sqrt( (x - p.x)*(x - p.x) + (y - p.y)*(y - p.y));
}
};
template
class MyPoint3D
{
public :
T x, y, z;
MyPoint3D(MyPoint3D *p) { x = p -> x ; y = p -> y ; z = p -> z; }
MyPoint3D(T x1, T y1, T z1) : x(x1), y(y1), z(z1) {}
MyPoint3D& operator=(const MyPoint3D &p){ x = p.x; y = p.y; z = p.z; return (*this);}
virtual ~MyPoint3D() {}
MyPoint3D() { x = y = z = 0; }
MyPoint3D* Get() { return this; }
// binary operators overloading
MyPoint3D operator*( const T a ){return MyPoint3D (x*a, y*a, z*a);}
MyPoint3D operator/( const T a ){return MyPoint3D (x/a, y/a, z/a);}
MyPoint3D operator*( const MyPoint3D p){return MyPoint3D (x*p.x, y*p.y, z*p.z);}
MyPoint3D operator+( const MyPoint3D p){return MyPoint3D (x+p.x, y+p.y, z+p.z);}
MyPoint3D operator-( const MyPoint3D p){return MyPoint3D (x-p.x, y-p.y, z-p.z);}
// unary operator definition
MyPoint3D operator-(){ return MyPoint3D (-x,-y,-z);}
MyPoint3D operator+(){ return MyPoint3D (x,y,z);}
MyPoint3D& operator+=(const MyPoint3D &p){*this = *this + p; return (*this); }
MyPoint3D& operator*=(const MyPoint3D &p){*this = *this * p; return (*this); }
MyPoint3D& operator-=(const MyPoint3D &p){*this = *this - p; return (*this); }
BOOL operator==(MyPoint3D p)
{
if(x != p.x) return FALSE;
if(y != p.y) return FALSE;
if(z != p.z) return FALSE;
return TRUE;
}
BOOL operator!=(MyPoint3D p)
{
return !operator==( p );
}
double radius() {return sqrt( x*x + y*y + z*z );}
MyPoint3D& normalize()
{
double r = this->radius();
x /= r;
y /= r;
z /= r;
return *this;
}
double d2( MyPoint3D p ) // Distance between
{
return sqrt( (x - p.x)*(x - p.x)+
(x - p.y)*(x - p.y)+
(x - p.z)*(x - p.z));
}
};
typedef MyPoint3D Point3d;
typedef MyPoint3D Point3f;
typedef MyPoint3D Point3i;
typedef MyPoint2D Point2d;
typedef MyPoint2D Point2f;
typedef MyPoint2D Point2i;
#endif // !defined(AFX_MYPOINT_LINGFA_YANG_INCLUDED_)