mathlib.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // mathlib.c -- math primitives
  2. #include "mathlib.h"
  3. #include <math.h>
  4. vec3_t vec3_origin = {0,0,0};
  5. double VectorLength(vec3_t v)
  6. {
  7. int i;
  8. double length;
  9. length = 0;
  10. for (i=0 ; i< 3 ; i++)
  11. length += v[i]*v[i];
  12. length = sqrt (length); // FIXME
  13. return length;
  14. }
  15. void VectorMA (vec3_t va, double scale, vec3_t vb, vec3_t vc)
  16. {
  17. vc[0] = va[0] + scale*vb[0];
  18. vc[1] = va[1] + scale*vb[1];
  19. vc[2] = va[2] + scale*vb[2];
  20. }
  21. boolean VectorCompare (vec3_t v1, vec3_t v2)
  22. {
  23. int i;
  24. for (i=0 ; i<3 ; i++)
  25. if (v1[i] != v2[i])
  26. return false;
  27. return true;
  28. }
  29. void CrossProduct (vec3_t v1, vec3_t v2, vec3_t cross)
  30. {
  31. cross[0] = v1[1]*v2[2] - v1[2]*v2[1];
  32. cross[1] = v1[2]*v2[0] - v1[0]*v2[2];
  33. cross[2] = v1[0]*v2[1] - v1[1]*v2[0];
  34. }
  35. vec_t _DotProduct (vec3_t v1, vec3_t v2)
  36. {
  37. return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
  38. }
  39. void _VectorSubtract (vec3_t va, vec3_t vb, vec3_t out)
  40. {
  41. out[0] = va[0]-vb[0];
  42. out[1] = va[1]-vb[1];
  43. out[2] = va[2]-vb[2];
  44. }
  45. void _VectorAdd (vec3_t va, vec3_t vb, vec3_t out)
  46. {
  47. out[0] = va[0]+vb[0];
  48. out[1] = va[1]+vb[1];
  49. out[2] = va[2]+vb[2];
  50. }
  51. void _VectorCopy (vec3_t in, vec3_t out)
  52. {
  53. out[0] = in[0];
  54. out[1] = in[1];
  55. out[2] = in[2];
  56. }
  57. void VectorNormalize (vec3_t v)
  58. {
  59. int i;
  60. float length;
  61. length = 0;
  62. for (i=0 ; i< 3 ; i++)
  63. length += v[i]*v[i];
  64. length = sqrt (length);
  65. for (i=0 ; i< 3 ; i++)
  66. v[i] /= length;
  67. }
  68. void VectorScale (vec3_t v, vec_t scale, vec3_t out)
  69. {
  70. out[0] = v[0] * scale;
  71. out[1] = v[1] * scale;
  72. out[2] = v[2] * scale;
  73. }