dml.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //
  2. // Doofy's Math library
  3. //
  4. #pragma once
  5. typedef struct {
  6. float x;
  7. float y;
  8. } Vector;
  9. #define min(a, b) ((a) < (b) ? (a) : (b))
  10. #define max(a, b) ((a) > (b) ? (a) : (b))
  11. #define abs(x) ((x) > 0 ? (x) : -(x))
  12. /**
  13. * Lerp function
  14. *
  15. * @param v0 Start value
  16. * @param v1 End value
  17. * @param t Time (0-1 range)
  18. * @return Point between v0-v1 at a given time
  19. */
  20. float lerp(float v0, float v1, float t);
  21. /**
  22. * 2D lerp function
  23. *
  24. * @param start Start vector
  25. * @param end End vector
  26. * @param t Time (0-1 range)
  27. * @return 2d Vector between start and end at time
  28. */
  29. Vector lerp_2d(Vector start, Vector end, float t);
  30. /**
  31. * Quadratic lerp function
  32. *
  33. * @param start Start vector
  34. * @param control Control point
  35. * @param end End vector
  36. * @param t Time (0-1 range)
  37. * @return 2d Vector at time
  38. */
  39. Vector quadratic_2d(Vector start, Vector control, Vector end, float t);
  40. /**
  41. * Add vector components together
  42. *
  43. * @param a First vector
  44. * @param b Second vector
  45. * @return Resulting vector
  46. */
  47. Vector vector_add(Vector a, Vector b);
  48. /**
  49. * Subtract vector components together
  50. *
  51. * @param a First vector
  52. * @param b Second vector
  53. * @return Resulting vector
  54. */
  55. Vector vector_sub(Vector a, Vector b);
  56. /**
  57. * Multiplying vector components together
  58. *
  59. * @param a First vector
  60. * @param b Second vector
  61. * @return Resulting vector
  62. */
  63. Vector vector_mul_components(Vector a, Vector b);
  64. /**
  65. * Dividing vector components
  66. *
  67. * @param a First vector
  68. * @param b Second vector
  69. * @return Resulting vector
  70. */
  71. Vector vector_div_components(Vector a, Vector b);
  72. /**
  73. * Calculating Vector length
  74. *
  75. * @param a Direction vector
  76. * @return Length of the vector
  77. */
  78. float vector_magnitude(Vector a);
  79. /**
  80. * Get a normalized vector (length of 1)
  81. *
  82. * @param a Direction vector
  83. * @return Normalized vector
  84. */
  85. Vector vector_normalized(Vector a);
  86. /**
  87. * Calculate two vector's distance
  88. *
  89. * @param a First vector
  90. * @param b Second vector
  91. * @return Distance between vectors
  92. */
  93. float vector_distance(Vector a, Vector b);
  94. /**
  95. * Calculate the dot product of the vectors.
  96. * No need to normalize, it will do it
  97. *
  98. * @param a First vector
  99. * @param b Second vector
  100. * @return value from -1 to 1
  101. */
  102. float vector_dot(Vector a, Vector b);