Vector3D.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include "Vector3D.h"
  2. #include <math.h>
  3. void vectorSet(Vector3D* v, float x, float y, float z) {
  4. v->x = x;
  5. v->y = y;
  6. v->z = z;
  7. }
  8. void vectorSetTo(Vector3D* v1, Vector3D* v2) {
  9. v1->x = v2->x;
  10. v1->y = v2->y;
  11. v1->z = v2->z;
  12. }
  13. void vectorSetToInverse(Vector3D* v1, Vector3D* v2) {
  14. v1->x = -v2->x;
  15. v1->y = -v2->y;
  16. v1->z = -v2->z;
  17. }
  18. void vectorSetMul(Vector3D* v1, Vector3D* v2, float f) {
  19. v1->x = v2->x * f;
  20. v1->y = v2->y * f;
  21. v1->z = v2->z * f;
  22. }
  23. void vectorSetAngles(Vector3D* v, float lengthAngle, float widthAngle) {
  24. lengthAngle = lengthAngle * M_PI / 180.0f;
  25. widthAngle = widthAngle * M_PI / 180.0f;
  26. v->x = cosf(widthAngle) * sinf(lengthAngle);
  27. v->z = cosf(widthAngle) * cosf(lengthAngle);
  28. v->y = sinf(widthAngle);
  29. }
  30. void vectorAdd(Vector3D* v1, Vector3D* v2) {
  31. v1->x += v2->x;
  32. v1->y += v2->y;
  33. v1->z += v2->z;
  34. }
  35. void vectorSub(Vector3D* v1, Vector3D* v2) {
  36. v1->x -= v2->x;
  37. v1->y -= v2->y;
  38. v1->z -= v2->z;
  39. }
  40. void vectorMul(Vector3D* v1, float f) {
  41. v1->x *= f;
  42. v1->y *= f;
  43. v1->z *= f;
  44. }
  45. void vectorAddMul(Vector3D* v1, Vector3D* v2, float f) {
  46. v1->x += v2->x * f;
  47. v1->y += v2->y * f;
  48. v1->z += v2->z * f;
  49. }
  50. void vectorCross(Vector3D* v, float x, float y, float z) {
  51. vectorSet(v, v->y * z - v->z * y, v->z * x - v->x * z, v->x * y - v->y * x);
  52. }
  53. void vectorCrossWith(Vector3D* v1, Vector3D* v2) {
  54. vectorSet(v1, v1->y * v2->z - v1->z * v2->y, v1->z * v2->x - v1->x * v2->z, v1->x * v2->y - v1->y * v2->x);
  55. }
  56. void vectorNormalize(Vector3D* v) {
  57. float f = vectorLength(v);
  58. v->x /= f;
  59. v->y /= f;
  60. v->z /= f;
  61. }
  62. float vectorLength(Vector3D* v) {
  63. return sqrt(v->x * v->x + v->y * v->y + v->z * v->z);
  64. }
  65. float vectorDot(Vector3D* v1, Vector3D* v2) {
  66. return v1->x * v2->x + v1->y * v2->y + v1->z * v2->z;
  67. }
  68. float vectorDotInverse(Vector3D* v1, Vector3D* v2) {
  69. return v1->x * (-v2->x) + v1->y * (-v2->y) + v1->z * (-v2->z);
  70. }