home *** CD-ROM | disk | FTP | other *** search
/ Current Shareware 1994 January / SHAR194.ISO / graphuti / frgen14.zip / SOURCE.ZIP / VECT.CPP < prev    next >
C/C++ Source or Header  |  1993-08-01  |  1KB  |  64 lines

  1. #include <fstream.h>
  2. #include <math.h>
  3. #include "vect.h"
  4.  
  5. char Vector::delim = ' ';
  6.  
  7. ostream& operator<< (ostream &f, const Vector &v)
  8. {
  9.     f.setf(ios::showpoint | ios::fixed);
  10.     f.width(8);
  11.     f.precision(4);
  12.  
  13.     f << v.x << v.delim << v.y << v.delim << v.z;
  14.  
  15.     return f;
  16. }
  17.  
  18.  
  19. Vector min (const Vector &v1, const Vector &v2)
  20. {
  21.     Vector v;
  22.  
  23.     v.x = (v1.x < v2.x) ? v1.x : v2.x;
  24.     v.y = (v1.y < v2.y) ? v1.y : v2.y;
  25.     v.z = (v1.z < v2.z) ? v1.z : v2.z;
  26.  
  27.     return v;
  28. }
  29.  
  30. Vector max (const Vector &v1, const Vector &v2)
  31. {
  32.     Vector v;
  33.  
  34.     v.x = (v1.x > v2.x) ? v1.x : v2.x;
  35.     v.y = (v1.y > v2.y) ? v1.y : v2.y;
  36.     v.z = (v1.z > v2.z) ? v1.z : v2.z;
  37.  
  38.     return v;
  39. }
  40.  
  41.  
  42. float angle (const Vector &v1, const Vector &v2)
  43. {
  44.     float angle;
  45.  
  46.     float mag1 = mag(v1);
  47.     float mag2 = mag(v2);
  48.  
  49.     if (mag1 * mag2 == 0.0)
  50.     angle = 0.0;
  51.     else {
  52.     float cos_theta = (v1 DOT v2) / (mag1 * mag2);
  53.  
  54.     if (cos_theta <= -1.0)
  55.         angle = M_PI;
  56.     else if (cos_theta >= +1.0)
  57.         angle = 0.0;
  58.     else
  59.         angle = acos(cos_theta);
  60.     }
  61.  
  62.     return angle;
  63. }
  64.