home *** CD-ROM | disk | FTP | other *** search
/ Cutting-Edge 3D Game Programming with C++ / CE3DC++.ISO / BOOK / CHAP12 / POINT3D.HPP < prev    next >
C/C++ Source or Header  |  1996-05-14  |  6KB  |  271 lines

  1. //
  2. // File name: Point3D.HPP
  3. //
  4. // Description: The header file a 3D point class
  5. //
  6. // Author: John De Goes
  7. //
  8. // Project: Cutting Edge 3D Game Programming
  9. //
  10.  
  11. #ifndef POINT3DHPP
  12. #define POINT3DHPP
  13.  
  14. #include <Math.H>
  15. #include <Stdio.H>
  16.  
  17. // The 3-dimensional point class:
  18. class Point3D {
  19. public:
  20.   float Lx, Ly, Lz;  // The local X, Y and Z
  21.   float Wx, Wy, Wz;  // The world X, Y and Z
  22.   Point3D () { Lx = Ly = Lz = 0.0F; }
  23.   // The equality operator:
  24.   inline int operator == ( Point3D &V );
  25.   // The inequality operator:
  26.   inline int operator != ( Point3D &V );
  27.   // The subtraction operator:
  28.   inline Point3D operator - ( Point3D &V );
  29.   // The addition operator:  
  30.   inline Point3D operator + ( Point3D &V );
  31.   // The multiplication operator:  
  32.   inline Point3D operator * ( Point3D &V );
  33.   // The division operator:  
  34.   inline Point3D operator / ( Point3D &V );
  35.   // The subtraction/assignment operator:  
  36.   inline Point3D &operator -= ( Point3D &V );
  37.   // The addition/assignment operator:  
  38.   inline Point3D &operator += ( Point3D &V );
  39.   // The multiplication/assignment operator:  
  40.   inline Point3D &operator *= ( Point3D &V );
  41.   // The division/assignment operator:  
  42.   inline Point3D &operator /= ( Point3D &V );
  43.   inline Point3D operator - ( double V );
  44.   // The addition operator: 
  45.   inline Point3D operator + ( double V );
  46.   // The multiplication operator:  
  47.   inline Point3D operator * ( double V );
  48.   // The division operator:  
  49.   inline Point3D operator / ( double V );
  50.   // The subtraction/assignment operator:  
  51.   inline Point3D &operator -= ( double V );
  52.   // The addition/assignment operator:  
  53.   inline Point3D &operator += ( double V );
  54.   // The multiplication/assignment operator:  
  55.   inline Point3D &operator *= ( double V );
  56.   // The division/assignment operator:  
  57.   inline Point3D &operator /= ( double V );
  58.   inline float Mag (); // Returns the magnitute of the 3D point
  59.   inline float DotUnit ( Point3D &V );
  60.   inline float DotNonUnit ( Point3D &V );
  61.   void Read ( FILE *File );
  62.   void Write ( FILE *File );
  63. };
  64.  
  65. int UniqueVert ( Point3D &V, Point3D *List, int Range );
  66. unsigned int GetVertIndex ( Point3D &V, Point3D *List, unsigned int Range );
  67.  
  68. // Function section:
  69. int Point3D::operator == ( Point3D &V )
  70.   {
  71.   int RValue = 0;
  72.   if ( V.Lx == Lx )
  73.      if ( V.Ly == Ly )
  74.         if ( V.Lz == Lz )
  75.            RValue = 1;
  76.   return RValue;
  77.   }
  78.  
  79. // The inequality operator:
  80. int Point3D::operator != ( Point3D &V )
  81.   {
  82.   int RValue = 0;
  83.   if ( ( V.Lx != Lx ) || ( V.Ly != Ly ) || ( V.Lz != Lz ) )
  84.      RValue = 1;
  85.   return RValue;
  86.   }
  87.  
  88. // The subtraction operator:
  89. Point3D Point3D::operator - ( Point3D &V )
  90.   {
  91.   Point3D Temp;
  92.   Temp.Lx = Lx - V.Lx;
  93.   Temp.Ly = Ly - V.Ly;
  94.   Temp.Lz = Lz - V.Lz;
  95.   return Temp;
  96.   }
  97.  
  98. // The addition operator:  
  99. Point3D Point3D::operator + ( Point3D &V )
  100.   {
  101.   Point3D Temp;
  102.   Temp.Lx = Lx + V.Lx;
  103.   Temp.Ly = Ly + V.Ly;
  104.   Temp.Lz = Lz + V.Lz;
  105.   return Temp;
  106.   }
  107.  
  108. // The multiplication operator:  
  109. Point3D Point3D::operator * ( Point3D &V )
  110.   {
  111.   Point3D Temp;
  112.   Temp.Lx = Lx * V.Lx;
  113.   Temp.Ly = Ly * V.Ly;
  114.   Temp.Lz = Lz * V.Lz;
  115.   return Temp;
  116.   }
  117.  
  118. // The division operator:  
  119. Point3D Point3D::operator / ( Point3D &V )
  120.   {
  121.   Point3D Temp;
  122.   Temp.Lx = Lx / V.Lx;
  123.   Temp.Ly = Ly / V.Ly;
  124.   Temp.Lz = Lz / V.Lz;
  125.   return Temp;
  126.   }
  127.  
  128. // The subtraction/assignment operator:  
  129. Point3D &Point3D::operator -= ( Point3D &V )
  130.   {
  131.   Lx -= V.Lx;
  132.   Ly -= V.Ly;
  133.   Lz -= V.Lz;
  134.   return *this;
  135.   }
  136.  
  137. // The addition/assignment operator:  
  138. Point3D &Point3D::operator += ( Point3D &V )
  139.   {
  140.   Lx += V.Lx;
  141.   Ly += V.Ly;
  142.   Lz += V.Lz;
  143.   return *this;
  144.   }
  145. // The multiplication/assignment operator:  
  146. Point3D &Point3D::operator *= ( Point3D &V )
  147.   {
  148.   Lx *= V.Lx;
  149.   Ly *= V.Ly;
  150.   Lz *= V.Lz;
  151.   return *this;
  152.   }
  153.  
  154. // The division/assignment operator:  
  155. Point3D &Point3D::operator /= ( Point3D &V )
  156.   {
  157.   Lx /= V.Lx;
  158.   Ly /= V.Ly;
  159.   Lz /= V.Lz;
  160.   return *this;
  161.   }
  162. // Subtraction operator:
  163. Point3D Point3D::operator - ( double V )
  164.   {
  165.   Point3D Temp;
  166.   Temp.Lx = Lx - V;
  167.   Temp.Ly = Ly - V;
  168.   Temp.Lz = Lz - V;
  169.   return Temp;
  170.   }
  171.  
  172. // The addition operator:  
  173. Point3D Point3D::operator + ( double V )
  174.   {
  175.   Point3D Temp;
  176.   Temp.Lx = Lx + V;
  177.   Temp.Ly = Ly + V;
  178.   Temp.Lz = Lz + V;
  179.   return Temp;
  180.   }
  181.  
  182. // The multiplication operator:  
  183. Point3D Point3D::operator * ( double V )
  184.   {
  185.   Point3D Temp;
  186.   Temp.Lx = Lx * V;
  187.   Temp.Ly = Ly * V;
  188.   Temp.Lz = Lz * V;
  189.   return Temp;
  190.   }
  191.  
  192. // The division operator:  
  193. Point3D Point3D::operator / ( double V )
  194.   {
  195.   Point3D Temp;
  196.   Temp.Lx = Lx / V;
  197.   Temp.Ly = Ly / V;
  198.   Temp.Lz = Lz / V;
  199.   return Temp;
  200.   }
  201.  
  202. // The subtraction/assignment operator:  
  203. Point3D &Point3D::operator -= ( double V )
  204.   {
  205.   Lx -= V;
  206.   Ly -= V;
  207.   Lz -= V;
  208.   return *this;
  209.   }
  210.  
  211. // The addition/assignment operator:  
  212. Point3D &Point3D::operator += ( double V )
  213.   {
  214.   Lx += V;
  215.   Ly += V;
  216.   Lz += V;
  217.   return *this;
  218.   }
  219.  
  220. // The multiplication/assignment operator:  
  221. Point3D &Point3D::operator *= ( double V )
  222.   {
  223.   Lx *= V;
  224.   Ly *= V;
  225.   Lz *= V;
  226.   return *this;
  227.   }
  228.  
  229. // The division/assignment operator:  
  230. Point3D &Point3D::operator /= ( double V )
  231.   {
  232.   Lx /= V;
  233.   Ly /= V;
  234.   Lz /= V;
  235.   return *this;
  236.   }
  237.  
  238. float Point3D::Mag ()
  239.   {
  240.   return sqrt ( Lx * Lx + Ly * Ly + Lz * Lz );
  241.   }
  242.  
  243. float Point3D::DotUnit ( Point3D &V )
  244.   {
  245.   return ( Lx * V.Lx + Ly * V.Ly + Lz * V.Lz );
  246.   }
  247.  
  248. float Point3D::DotNonUnit ( Point3D &V )
  249.   {
  250.   float Dot = ( Lx * V.Lx +
  251.                 Ly * V.Ly +
  252.                 Lz * V.Lz ) / ( Mag () * V.Mag () );
  253.   return Dot;
  254.   }
  255.  
  256. void Point3D::Read ( FILE *File )
  257.    {
  258.    fread ( &Lx, sizeof Lx, 1, File );
  259.    fread ( &Ly, sizeof Ly, 1, File );
  260.    fread ( &Lz, sizeof Lz, 1, File );
  261.    }
  262.  
  263. void Point3D::Write ( FILE *File )
  264.    {
  265.    fwrite ( &Lx, sizeof Lx, 1, File );
  266.    fwrite ( &Ly, sizeof Ly, 1, File );
  267.    fwrite ( &Lz, sizeof Lz, 1, File );
  268.    }
  269.  
  270. #endif
  271.