home *** CD-ROM | disk | FTP | other *** search
/ Quake++ for Quake / Quake++.iso / quake / qkview / vector.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-17  |  1.5 KB  |  99 lines

  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <stdlib.h>
  4. #include <windows.h>
  5. #include <applicat.h>
  6.  
  7. #include "vector.h"
  8.  
  9. // Magnitude of a vector:
  10. double Mag(VECTOR& v)
  11. {
  12.     return sqrt(v.x*v.x + v.y*v.y + v.z*v.z);
  13. }
  14.  
  15. // Add two vectors
  16. VECTOR Add(VECTOR& v1, VECTOR& v2)
  17. {
  18.     VECTOR res;
  19.  
  20.     res.x = v1.x + v2.x;
  21.     res.y = v1.y + v2.y;
  22.     res.z = v1.z + v2.z;
  23.  
  24.     return res;
  25. }
  26.  
  27.  
  28. // Subtract the two vectors
  29. VECTOR Subtract(VECTOR& v1, VECTOR& v2)
  30. {
  31.     VECTOR res;
  32.  
  33.     res.x = v1.x - v2.x;
  34.     res.y = v1.y - v2.y;
  35.     res.z = v1.z - v2.z;
  36.  
  37.     return res;
  38. }
  39.  
  40. // Cross multiply two vectors
  41. VECTOR Cross(VECTOR& v1, VECTOR& v2)
  42. {
  43.     VECTOR res;
  44.  
  45.     res.x = v1.y * v2.z - v2.y * v1.z;
  46.     res.y = v1.z * v2.x - v2.z * v1.x;
  47.     res.z = v1.x * v2.y - v2.x * v1.y;
  48.  
  49.     return res;
  50. }
  51.  
  52. // Multiply by constant
  53. VECTOR Multiply(VECTOR& v, double num)
  54. {
  55.     VECTOR res;
  56.  
  57.     res.x = v.x * num;
  58.     res.y = v.y * num;
  59.     res.z = v.z * num;
  60.  
  61.     return res;
  62. }
  63.  
  64.  
  65. // Divide the vector with a scalar
  66. VECTOR Divide(VECTOR& v, double num)
  67. {
  68.     VECTOR res;
  69.  
  70.     if (num == 0) {
  71.         MessageBox(NULL,"Division by zero error in vector.cpp: Divide.","Closedown Query",MB_OK+MB_ICONEXCLAMATION);
  72.     }
  73.  
  74.     res.x = v.x / num;
  75.     res.y = v.y / num;
  76.     res.z = v.z / num;
  77.  
  78.     return res;
  79. }
  80.  
  81. // Normalize the vector
  82. void Normalize(VECTOR& v)
  83. {
  84.     double len = Mag(v);
  85.  
  86.     if (len != 0) {
  87.         v = Divide(v,len);
  88.     }
  89. }
  90.  
  91. void PrintVector(LPSTR text, VECTOR& v)
  92. {
  93.  
  94.     while (*text) {
  95.         cout << *text;
  96.         text++;
  97.     }
  98.     cout << " (" << v.x << ", " << v.y << ", " << v.z << ")\n";
  99. }