dml.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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(lerp_2d(start, control, t), lerp_2d(control, end, t), t);
  15. }
  16. Vector vector_add(Vector a, Vector b) {
  17. return (Vector){a.x + b.x, a.y + b.y};
  18. }
  19. Vector vector_sub(Vector a, Vector b) {
  20. return (Vector){a.x - b.x, a.y - b.y};
  21. }
  22. Vector vector_mul_components(Vector a, Vector b) {
  23. return (Vector){a.x * b.x, a.y * b.y};
  24. }
  25. Vector vector_div_components(Vector a, Vector b) {
  26. return (Vector){a.x / b.x, a.y / b.y};
  27. }
  28. Vector vector_normalized(Vector a) {
  29. float length = vector_magnitude(a);
  30. return (Vector){a.x / length, a.y / length};
  31. }
  32. float vector_magnitude(Vector a) {
  33. return sqrt(a.x * a.x + a.y * a.y);
  34. }
  35. float vector_distance(Vector a, Vector b) {
  36. return vector_magnitude(vector_sub(a, b));
  37. }
  38. float vector_dot(Vector a, Vector b) {
  39. Vector _a = vector_normalized(a);
  40. Vector _b = vector_normalized(b);
  41. return _a.x * _b.x + _a.y * _b.y;
  42. }