home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 3 / RISC_DISC_3.iso / resources / etexts / gems / gemsv / ch5_3 / vector.h < prev   
Encoding:
C/C++ Source or Header  |  1995-03-08  |  4.6 KB  |  169 lines

  1. /***********************************************************
  2. * vector.h - a vector class written in c++                 *
  3. * functions for +, -, dotproduct, crossproduct, scaling,   *
  4. * length & normalizing, many of these are operators        *
  5. * By Tomas Moller                                          *
  6. ***********************************************************/
  7. #ifndef VECTOR_H
  8. #define VECTOR_H
  9.  
  10. #include <stream.h>
  11. #include <string.h>
  12. #include <math.h>
  13.  
  14. #define Xi 0            // indices into vector
  15. #define Yi 1
  16. #define Zi 2
  17.  
  18. class Vector
  19. {
  20.     protected:
  21.         float fx,fy,fz;
  22.     public:
  23.         Vector() {fx=0.0;fy=0.0;fz=0.0;}        // constructor with no argument
  24.         Vector(float x,float y,float z);            // constructor with coords
  25.         Vector(Vector& a);                        // constructor with vector
  26.         void Set(float x,float y,float z);            // assign new values to vector
  27.         void SetX(float x);                        // set x
  28.         void SetY(float y);                        // set y
  29.         void SetZ(float z);                        // set z;
  30.         void SetIndex(int index,float value);    // set x,y or z to value depending on index
  31.         float X(void);                            // return fx
  32.         float Y(void);                            // return fy
  33.         float Z(void);                            // return fz
  34.         void Add(float x,float y,float z);            // addition to this vector
  35.         void Sub(float x,float y,float z);            // subtraction
  36.         void Scale(float a);                        // scaling of vector
  37.         float Length(void);                        // length of vector
  38.         void Normalize(void);                    // normalize vector
  39.  
  40.         void operator=(Vector& a);                // operator: assignment
  41.         Vector operator*(float t);                // operator: scaling
  42.         Vector operator+(Vector& a);            // operator: addition 
  43.         Vector operator-(Vector& a);            // operator: subtraction
  44.         Vector operator+(void);                    // unary +
  45.         Vector operator-(void);                    // unary -
  46.         void operator+=(Vector& a);                // operator: +=
  47.         void operator-=(Vector& a);                // operator: -=
  48.         void operator*=(float t);                // operator: *= (scaling)
  49.         float operator*(Vector& a);                // operator: dot product
  50.         Vector operator%(Vector& a);            // operator: cross product
  51.         float operator[](short index);            // if short=0 then X, short=1 then Y, else Z, see constants above
  52.         void print(void);                        // print coords
  53.  
  54. };
  55.  
  56. /* here follows the inline functions and operators */
  57.  
  58. inline Vector::Vector(float x,float y,float z)
  59. { fx=x; fy=y; fz=z; }
  60.  
  61. inline Vector::Vector(Vector& a)
  62. { fx=a.fx; fy=a.fy; fz=a.fz; }
  63.  
  64. inline void Vector::Set(float x,float y,float z)
  65. { fx=x; fy=y; fz=z; }
  66.  
  67. inline void Vector::SetX(float x)
  68. { fx=x;}
  69.  
  70. inline void Vector::SetY(float y)
  71. { fy=y; }
  72.  
  73. inline void Vector::SetZ(float z)
  74. { fz=z; }
  75.  
  76. inline void Vector::SetIndex(int index,float value)
  77. {
  78.     switch(index)
  79.     {
  80.         case Xi: fx=value;
  81.         case Yi: fy=value;
  82.         case Zi: fz=value;
  83.     }
  84. }
  85.  
  86. inline float Vector::X(void)
  87. { return fx; }
  88.  
  89. inline float Vector::Y(void)
  90. { return fy; }
  91.  
  92. inline float Vector::Z(void)
  93. { return fz; }
  94.  
  95. inline void Vector::Add(float x,float y,float z)
  96. { fx+=x; fy+=y; fz+=z; }
  97.  
  98. inline void Vector::Sub(float x,float y,float z)
  99. { fx-=x; fy-=y; fz-=z; }
  100.  
  101. inline void Vector::Scale(float a)
  102. { fx*=a; fy*=a; fz*=a; }
  103.  
  104. inline float Vector::Length(void)
  105. { return sqrt((*this)*(*this));    // square root of Dot(this,this)
  106. }
  107.  
  108. inline void Vector::Normalize(void)
  109.     if(Length()==0.0) cout<<"Error:normalize\n";
  110.      else Scale(1.0/Length());
  111. }
  112.  
  113. /****************** Operators *********************/
  114. inline void Vector::operator=(Vector& a)    // assignment
  115. { fx=a.fx; fy=a.fy; fz=a.fz; }
  116.  
  117. inline Vector Vector::operator+(void)        // unary +
  118. { return *this; }
  119.  
  120. inline Vector Vector::operator*(float t)        // scaling
  121. { Vector temp; temp.Set(fx*t,fy*t,fz*t); return temp; }
  122.  
  123. inline Vector Vector::operator+(Vector& a)
  124. { Vector sum; sum.Set(fx+a.fx,fy+a.fy,fz+a.fz); return sum; }
  125.  
  126. inline Vector Vector::operator-(Vector& a)
  127. { Vector sum; sum.Set(fx-a.fx,fy-a.fy,fz-a.fz); return sum; }
  128.  
  129. inline Vector Vector::operator-(void)            // unary -
  130. { Vector neg; neg.Set(-fx,-fy,-fz); return neg; }
  131.  
  132. inline void Vector::operator+=(Vector& a)
  133. { Set(fx+a.fx,fy+a.fy,fz+a.fz); }
  134.  
  135. inline void Vector::operator-=(Vector& a)
  136. { Set(fx-a.fx,fy-a.fy,fz-a.fz); }
  137.  
  138. inline void Vector::operator*=(float t)        // scaling
  139. { Set(fx*t,fy*t,fz*t); }
  140.  
  141. inline float Vector::operator*(Vector& a)        // dot product
  142. { return fx*a.fx+fy*a.fy+fz*a.fz; }
  143.  
  144. inline Vector Vector::operator%(Vector& a)        // cross product
  145. {
  146.     Vector cross;
  147.     cross.Set(fy*a.fz-fz*a.fy,fz*a.fx-fx*a.fz,fx*a.fy-fy*a.fx);
  148.     return cross;
  149. }
  150.  
  151. inline float Vector::operator[](short index)
  152. {
  153.     switch(index)
  154.     {
  155.         case Xi: return fx;
  156.         case Yi: return fy;
  157.         case Zi: return fz;
  158.     }
  159.     return 0.0;                    // if invalid index
  160. }
  161. /*************** End of Operators *****************/
  162.  
  163. inline void Vector::print(void)
  164. {
  165.     cout<<form("x:%.6f y:%.6f z:%.6f\n",fx,fy,fz);
  166. }
  167. #endif
  168.