vector.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include "vector.h"
  2. #include <math.h>
  3. #undef vector_add
  4. #undef vector_sub
  5. #undef vector_mul
  6. #undef vector_div
  7. Vector vector_add(Vector a, Vector b) {
  8. return (Vector){.x = a.x + b.x, .y = a.y + b.y};
  9. }
  10. Vector vector_sub(Vector a, Vector b) {
  11. return (Vector){.x = a.x - b.x, .y = a.y - b.y};
  12. }
  13. Vector vector_mul(Vector a, Vector b) {
  14. return (Vector){.x = a.x * b.x, .y = a.y * b.y};
  15. }
  16. Vector vector_div(Vector a, Vector b) {
  17. return (Vector){.x = a.x / b.x, .y = a.y / b.y};
  18. }
  19. Vector vector_addf(Vector a, float b) {
  20. return (Vector){.x = a.x + b, .y = a.y + b};
  21. }
  22. Vector vector_subf(Vector a, float b) {
  23. return (Vector){.x = a.x - b, .y = a.y - b};
  24. }
  25. Vector vector_mulf(Vector a, float b) {
  26. return (Vector){.x = a.x * b, .y = a.y * b};
  27. }
  28. Vector vector_divf(Vector a, float b) {
  29. return (Vector){.x = a.x / b, .y = a.y / b};
  30. }
  31. float vector_length(Vector v) {
  32. return sqrtf(v.x * v.x + v.y * v.y);
  33. }
  34. Vector vector_normalize(Vector v) {
  35. float length = vector_length(v);
  36. return (Vector){v.x / length, v.y / length};
  37. }
  38. float vector_dot(Vector a, Vector b) {
  39. return a.x * b.x + a.y * b.y;
  40. }
  41. Vector vector_rand() {
  42. float x = (rand() % __INT_MAX__) / (float)__INT_MAX__;
  43. float y = (rand() % __INT_MAX__) / (float)__INT_MAX__;
  44. return (Vector){x, y};
  45. }