home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 3 / RISC_DISC_3.iso / resources / etexts / gems / gemsv / ch7_4 / vector.h < prev   
Encoding:
C/C++ Source or Header  |  1994-11-22  |  1.2 KB  |  50 lines

  1. // -*- C++ -*-
  2. // vector.h by George Vanecek Jr. June 1994
  3.  
  4. #ifndef _VECTOR_H_
  5. #define _VECTOR_H_
  6.  
  7. #ifndef _POINT_H_
  8. #include "point.h"
  9. #endif
  10.  
  11. class Vector : public Point {
  12. public:
  13.   Vector( const Point&  p ) : Point(p) {}
  14.   Vector( double a, double b, double c ) : Point(a, b, c) {}
  15.   inline double operator * ( const Vector& v ) const;
  16.   inline Vector operator ^ ( const Vector& v ) const;
  17.   void          normalize  ( );
  18. };
  19.  
  20. inline Vector operator + ( const Point& p, const Point& q )
  21. { return Point(p.x()+q.x(), p.y()+q.y(), p.z()+q.z()); }
  22.  
  23. inline Vector operator - ( const Point& p, const Point& q )
  24. { return Point(p.x()-q.x(), p.y()-q.y(), p.z()-q.z()); }
  25.  
  26. inline Vector operator * ( const double s, const Vector& v )
  27. { return Vector( s*v.x(), s*v.y(), s*v.z() ); }
  28.  
  29. // Vector Dot Product.
  30. inline double Vector::operator * (  const Vector& v ) const
  31. { return x() * v.x() + y() * v.y() + z() * v.z(); }
  32.  
  33. // Vector Cross product.
  34. inline Vector Vector::operator ^ ( const Vector& v ) const
  35. { return Vector(y() * v.z() - z() * v.y(),
  36.         z() * v.x() - x() * v.z(),
  37.         x() * v.y() - y() * v.x());
  38. }
  39.  
  40. inline void Vector::normalize()
  41.   const double n = *this * *this;
  42.   assert( n != 0.0 );
  43.   x() /= n;
  44.   y() /= n;
  45.   z() /= n;
  46. }
  47.  
  48. #endif
  49.