Creating a 3D Point Class

Intro

Three dimensional points are the basic founding elements in any type of 3d application. All objects must have some basic unit to transform and base calculations from, 3d points gives us this power. We will first take a quick look at the header file and then go into each function in more detail. For smaller programs we can get away with just using x,y,z for a 3 dimensional point, but instead we are going to make our class more powerful by overloading several operators so we can do point comparison without having to write out the nasty details every time.Without further delay, here’s the listing:

  1. class Point3d
  2. {
  3.     public:
  4.     long lx,ly,lz,lt;
  5.     long wx,wy,wz,wt;
  6.     long ax,ay,az,at;
  7.     long sx,sy,sz,st;
  8.     Point3d();
  9.     ~Point3d();   
  10.  
  11.     int operator == (Point3d &V);
  12.     int operator != (Point3d &V);
  13.     Point3d operator -  (Point3d &p);
  14.     Point3d operator +  (Point3d &p);
  15.     Point3d operator *  (Point3d &p);
  16.     Point3d operator /  (Point3d &p);
  17.     Point3d &operator -= (Point3d &p);
  18.     Point3d &operator += (Point3d &p);
  19.     Point3d &operator *= (Point3d &p);
  20.     Point3d &operator /= (Point3d &p);
  21.     int operator == (double p);
  22.     int operator != (double p);
  23.     Point3d operator -  (double p);
  24.     Point3d operator +  (double p);
  25.     Point3d operator *  (double p);
  26.     Point3d operator /  (double p);
  27.     Point3d &operator -= (double p);
  28.     Point3d &operator += (double p);
  29.     Point3d &operator *= (double p);
  30.     Point3d &operator /= (double p);
  31.     void ReadData(FILE *file);
  32.     void WriteData(FILE *file);
  33.     void Print();
  34. };

That’s quite a few functions considering we are just trying to keep track of one point! Notice that instead of just having an x,y,z for members we have a 16! We will go into the reasons for this in another tutorial, just know that those members are very neccessary!

  1. Point3d::Point3d()
  2. {
  3.     lx=ly=lz=ax=ay=az=at=wx=wy=wz=sx=sy=sz=0;
  4.     lt=wt=st=1;
  5. }
  6. Point3d::~Point3d()
  7. {
  8. }

Our Constructor and Destructor are very simple right now. Notice that we set all the point members equal to zero except the t items (lt,wt,st). This is because with the transformations we will be doing (ie matrices operations) we know that we must have them set to 1 for it to work! For now our destructor is just defined and does nothing! Now on to our overloaded operators!

  1. int Point3d::operator == (Point3d &p)
  2. {
  3.     int rvalue=0;
  4.     if(p.lx==lx)
  5.       if(p.ly==ly)
  6.       if(p.lz==lz)
  7.               rvalue=1;
  8.     return rvalue;
  9. }
  10.  
  11. int Point3d::operator != (Point3d &p)
  12. {
  13.     int rvalue=0;
  14.     if((p.lx!=lx)||(p.ly != ly)||(p.lz != lz))
  15.         rvalue=1;
  16.     return rvalue;
  17. }

The equality and inquality operators are very easy to read. We simply to a component by component comparison between the two points to see if they are equal or inequal, returning the proper flag.

  1. Point3d Point3d::operator( Point3d &p )
  2. {
  3.     Point3d Temp;
  4.     Temp.lx = lx – p.lx;
  5.     Temp.ly = ly – p.ly;
  6.     Temp.lz = lz – p.lz;
  7.     return Temp;
  8. }
  9. Point3d Point3d::operator + ( Point3d &p )
  10. {
  11.     Point3d Temp;
  12.     Temp.lx = lx + p.lx;
  13.     Temp.ly = ly + p.ly;
  14.     Temp.lz = lz + p.lz;
  15.     return Temp;
  16. }
  17. Point3d Point3d::operator * ( Point3d &p )
  18. {
  19.     Point3d Temp;
  20.     Temp.lx = lx * p.lx;
  21.     Temp.ly = ly * p.ly;
  22.     Temp.lz = lz * p.lz;
  23.     return Temp;
  24. }
  25. Point3d Point3d::operator / ( Point3d &p )
  26. {
  27.     Point3d Temp;
  28.     Temp.lx = lx / p.lx;
  29.     Temp.ly = ly / p.ly;
  30.     Temp.lz = lz / p.lz;
  31.     return Temp;
  32. }

As you can see, these functions are very similiar. Each uses its operation in an component by component manor. For instance, the division operator simply divides the current lx,ly,lz by the lx,ly,lz of the new point and returns the new point so the original point remains intact.

  1. Point3d &Point3d::operator -= ( Point3d &p )
  2. {
  3.     lx -= p.lx;
  4.     ly -= p.ly;
  5.     lz -= p.lz;
  6.     return *this;
  7. }
  8. Point3d &Point3d::operator += ( Point3d &p )
  9. {
  10.     lx += p.lx;
  11.     ly += p.ly;
  12.     lz += p.lz;
  13.     return *this;
  14. }
  15. Point3d &Point3d::operator *= ( Point3d &p )
  16. {
  17.     lx *= p.lx;
  18.     ly *= p.ly;
  19.     lz *= p.lz;
  20.     return *this;
  21. }
  22. Point3d &Point3d::operator /= ( Point3d &p )
  23. {
  24.     lx /= p.lx;
  25.     ly /= p.ly;
  26.     lz /= p.lz;
  27.     return *this;
  28. }

As you can see, this overloading operator stuff is pretty easy! Here we simply overload the operation/assignment operators (-=, +=, *=, /=) to work with our points, notice that so far, we can only do these operations with other points, and not static data.

  1. Point3d Point3d::operator( double Value )
  2. {
  3.     Point3d Temp;
  4.     Temp.lx = (long)(lx- Value);
  5.     Temp.ly = (long)(ly – Value);
  6.     Temp.lz = (long)(lz – Value);
  7.     return Temp;
  8. }
  9. Point3d Point3d::operator + ( double Value )
  10. {
  11.     Point3d Temp;
  12.     Temp.lx = (long)(lx + Value);
  13.     Temp.ly = (long)(ly + Value);
  14.     Temp.lz = (long)(lz + Value);
  15.     return Temp;
  16. }
  17. Point3d Point3d::operator * ( double Value )
  18. {
  19.     Point3d Temp;
  20.     Temp.lx = (long)(lx * Value);
  21.     Temp.ly = (long)(ly * Value);
  22.     Temp.lz = (long)(lz * Value);
  23.     return Temp;
  24. }
  25. Point3d Point3d::operator / ( double Value )
  26. {
  27.     Point3d Temp;
  28.     Temp.lx = (long)(lx / Value);
  29.     Temp.ly = (long)(ly / Value);
  30.     Temp.lz = (long)(lz / Value);
  31.     return Temp;
  32. }

Now we finally have our operators overloaded so we can either add 2 points together or add a value to all components of our point!

  1. Point3d &Point3d::operator -= ( double Value )
  2. {
  3.     lx -= (long)Value;
  4.     ly -= (long)Value;
  5.     lz -= (long)Value;
  6.     return *this;
  7. }
  8. Point3d &Point3d::operator += ( double V )
  9. {
  10.     lx += (long)V;
  11.     ly += (long)V;
  12.     lz += (long)V;
  13.     return *this;
  14. }
  15. Point3d &Point3d::operator *= ( double Value )
  16. {
  17.     lx *= (long)Value;
  18.     ly *= (long)Value;
  19.     lz *= (long)Value;
  20.     return *this;
  21. }
  22. Point3d &Point3d::operator /= ( double Value )
  23. {
  24.     lx /= (long)V;
  25.     ly /= (long)V;
  26.     lz /= (long)V;
  27.     return *this;
  28. }

Again, very simple. We overload the operation/assignment operators once again, but this time we can pass a static value to be used.

  1. void Point3d::ReadData ( FILE *File )
  2. {
  3.     fread ( &lx, sizeof lx, 1, File );
  4.     fread ( &ly, sizeof ly, 1, File );
  5.     fread ( &lz, sizeof lz, 1, File );
  6. }
  7.  
  8. void Point3d::WriteData ( FILE *File )
  9. {
  10.     fwrite ( &lx, sizeof lx, 1, File );
  11.     fwrite ( &ly, sizeof ly, 1, File );
  12.     fwrite ( &lz, sizeof lz, 1, File );
  13. }
  14.  
  15. void Point3d::Print()
  16. {
  17.     cout<<dec<<"\t\t\tLx= "<<lx<<" Ly= "<<ly<<" Lz= "<<lz<<endl;
  18. }

These are a couple of goodie functions that allow us to save points to a data file and to load them up. These functions make this class very self sefficient and extremely flexible. There's the whole Point3d class. Although we only needed to store a couple of coordinates we decided to overload some operators to increase the re-usability and flexibility of our code!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>