iron_vec2.c 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include "iron_vec2.h"
  2. #include <math.h>
  3. #include <iron_math.h>
  4. vec2_t vec2_create(float x, float y) {
  5. vec2_t v;
  6. v.x = x;
  7. v.y = y;
  8. return v;
  9. }
  10. float vec2_len(vec2_t v) {
  11. return sqrtf(v.x * v.x + v.y * v.y);
  12. }
  13. vec2_t vec2_set_len(vec2_t v, float length) {
  14. float current_length = vec2_len(v);
  15. if (current_length == 0) {
  16. return v;
  17. }
  18. float mul = length / current_length;
  19. v.x *= mul;
  20. v.y *= mul;
  21. return v;
  22. }
  23. vec2_t vec2_mult(vec2_t v, float f) {
  24. v.x *= f;
  25. v.y *= f;
  26. return v;
  27. }
  28. vec2_t vec2_add(vec2_t a, vec2_t b) {
  29. a.x += b.x;
  30. a.y += b.y;
  31. return a;
  32. }
  33. vec2_t vec2_sub(vec2_t a, vec2_t b) {
  34. a.x -= b.x;
  35. a.y -= b.y;
  36. return a;
  37. }
  38. float vec2_cross(vec2_t a, vec2_t b) {
  39. return a.x * b.y - a.y * b.x;
  40. }
  41. vec2_t vec2_norm(vec2_t v) {
  42. float n = vec2_len(v);
  43. if (n > 0.0) {
  44. float inv_n = 1.0f / n;
  45. v.x *= inv_n;
  46. v.y *= inv_n;
  47. }
  48. return v;
  49. }
  50. float vec2_dot(vec2_t a, vec2_t b) {
  51. return a.x * b.x + a.y * b.y;
  52. }
  53. vec2_t vec2_nan() {
  54. vec2_t v;
  55. v.x = NAN;
  56. return v;
  57. }
  58. bool vec2_isnan(vec2_t v) {
  59. return isnan(v.x);
  60. }