dml.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include "dml.h"
  2. #include <math.h>
  3. float lerp(float v0, float v1, float t) {
  4. if (t > 1) return v1;
  5. return (1 - t) * v0 + t * v1;
  6. }
  7. Vector lerp_2d(Vector start, Vector end, float t) {
  8. return (Vector) {
  9. lerp(start.x, end.x, t),
  10. lerp(start.y, end.y, t),
  11. };
  12. }
  13. Vector quadratic_2d(Vector start, Vector control, Vector end, float t) {
  14. return lerp_2d(
  15. lerp_2d(start, control, t),
  16. lerp_2d(control, end, t),
  17. t
  18. );
  19. }
  20. Vector vector_add(Vector a, Vector b) {
  21. return (Vector) {a.x + b.x, a.y + b.y};
  22. }
  23. Vector vector_sub(Vector a, Vector b) {
  24. return (Vector) {a.x - b.x, a.y - b.y};
  25. }
  26. Vector vector_mul_components(Vector a, Vector b) {
  27. return (Vector) {a.x * b.x, a.y * b.y};
  28. }
  29. Vector vector_div_components(Vector a, Vector b) {
  30. return (Vector) {a.x / b.x, a.y / b.y};
  31. }
  32. Vector vector_normalized(Vector a) {
  33. float length = vector_magnitude(a);
  34. return (Vector) {
  35. a.x / length,
  36. a.y / length
  37. };
  38. }
  39. float vector_magnitude(Vector a) {
  40. return sqrt(a.x * a.x + a.y * a.y);
  41. }
  42. float vector_distance(Vector a, Vector b) {
  43. return vector_magnitude(vector_sub(a, b));
  44. }
  45. float vector_dot(Vector a, Vector b) {
  46. Vector _a = vector_normalized(a);
  47. Vector _b = vector_normalized(b);
  48. return _a.x * _b.x + _a.y * _b.y;
  49. }