FZ_math.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #ifndef FZ_MATH_H
  2. #define FZ_MATH_H
  3. #include "FZ_types.h"
  4. double distance_2(FZ_vector_2 a, FZ_vector_2 b){
  5. return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
  6. }
  7. double squared_distance_2(FZ_vector_2 a, FZ_vector_2 b){
  8. return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
  9. }
  10. FZ_vector_2 v2_mul(FZ_vector_2 vector, double value){
  11. vector.x *= value;
  12. vector.y *= value;
  13. return vector;
  14. }
  15. // element-wise add
  16. FZ_vector_2 v2_ew_add(FZ_vector_2 vector_a, FZ_vector_2 vector_b){
  17. vector_a.x += vector_b.x;
  18. vector_a.y += vector_b.y;
  19. return vector_a;
  20. }
  21. // element-wise subtract
  22. FZ_vector_2 v2_ew_sub(FZ_vector_2 vector_a, FZ_vector_2 vector_b){
  23. vector_a.x -= vector_b.x;
  24. vector_a.y -= vector_b.y;
  25. return vector_a;
  26. }
  27. // element-wise multiply
  28. FZ_vector_2 v2_ew_mul(FZ_vector_2 vector_a, FZ_vector_2 vector_b){
  29. vector_a.x *= vector_b.x;
  30. vector_a.y *= vector_b.y;
  31. return vector_a;
  32. }
  33. FZ_vector_2 vector_2_lerp(FZ_vector_2 vector_a, FZ_vector_2 vector_b, double w1){
  34. double w0 = 1.0 - w1;
  35. FZ_vector_2 result = (FZ_vector_2){0, 0};
  36. vector_a = v2_mul(vector_a, w0);
  37. vector_b = v2_mul(vector_b, w1);
  38. result = v2_ew_add(result, vector_a);
  39. result = v2_ew_add(result, vector_b);
  40. return result;
  41. }
  42. FZ_vector_2 v2_rot(FZ_vector_2 vector, FZ_vector_2 origin, double angle){
  43. FZ_vector_2 new_vec = vector;
  44. new_vec = v2_ew_sub(new_vec, origin);
  45. new_vec.x = vector.x * cos(angle) - vector.y * sin(angle);
  46. new_vec.y = vector.x * sin(angle) + vector.y * cos(angle);
  47. new_vec = v2_ew_add(new_vec, origin);
  48. return new_vec;
  49. }
  50. double v2_dot(FZ_vector_2 vector_a, FZ_vector_2 vector_b){
  51. double result = vector_a.x * vector_b.x + vector_a.y * vector_b.y;
  52. return result;
  53. }
  54. FZ_vector_2 v2_normalize(FZ_vector_2 vector){
  55. return v2_mul(vector, 1 / sqrt(v2_dot(vector, vector)));
  56. }
  57. FZ_vector_2 v2_rotate_neg_90(FZ_vector_2 vector){
  58. return (FZ_vector_2){-vector.y, vector.x};
  59. }
  60. FZ_vector_2 v2_rotate_90(FZ_vector_2 vector){
  61. return (FZ_vector_2){vector.y, -vector.x};
  62. }
  63. // where a and b form line a, project c onto line a, return the scalar
  64. double project_along_vectors_normal(FZ_vector_2 a, FZ_vector_2 b, FZ_vector_2 c){
  65. b = (FZ_vector_2){b.y - a.y, -(b.x - a.x)};
  66. c = v2_ew_sub(c, a);
  67. return (v2_dot(c, b) / v2_dot(b, b));
  68. }
  69. FZ_vector_2 v2_scalar_cross(FZ_vector_2 vector, double value){
  70. return v2_mul(v2_rotate_neg_90(vector), value);
  71. }
  72. double v2_v2_cross(FZ_vector_2 a, FZ_vector_2 b) {
  73. return a.x * b.y - a.y * b.x;
  74. }
  75. #endif