vec2.cxx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #include <stdbool.h>
  2. #include <math.h>
  3. #include "vec2.h"
  4. // Vec2 Vec2_unit(const Vec2& v) {
  5. // Vec2 u(v.x, v.y);
  6. // u.normalize();
  7. // return u;
  8. // }
  9. // // Returns true if point b lies between points a and c
  10. // bool Vec2_colinear(const Vec2& a, const Vec2& b, const Vec2& c) {
  11. // float ac = a.dist(c);
  12. // float ab = a.dist(b);
  13. // float bc = b.dist(c);
  14. // return fabsf(ac - ab - bc) <= VEC2_EPSILON;
  15. // }
  16. // Returns the closest point to the line segment ab and p
  17. Vec2 Vec2_closest(const Vec2& a, const Vec2& b, const Vec2& p) {
  18. // vector along line ab
  19. Vec2 ab = b - a;
  20. float t = ab.dot(ab);
  21. if(t == 0.0f) {
  22. return a;
  23. }
  24. t = fmax(0.0f, fmin(1.0f, (p.dot(ab) - a.dot(ab)) / t));
  25. return a + ab * t;
  26. }
  27. // // // Ehhh - this should work??
  28. // // // Returns the closest point to the line segment ab and p
  29. // // Vec2 Vec2_closest(const Vec2& a, const Vec2& b, const Vec2& p) {
  30. // // // vector along line ab
  31. // // Vec2 ab = b - a;
  32. // // // vector aline line ap
  33. // // Vec2 ap = p - a;
  34. // // // projection of p onto ab
  35. // // float proj = ap.dot(ab);
  36. // // // line segment ab length, squared
  37. // // float ab_l2 = a.dist2(b);
  38. // // // ratio of the projection along line. segment is [0,1] - beyond isn't on line
  39. // // float d = proj / ab_l2;
  40. // // if(d <= 0) {
  41. // // return a;
  42. // // } else if(d >= 1) {
  43. // // return b;
  44. // // }
  45. // // return a + ab * d;
  46. // // }
  47. // // Returns the closest point to the infinite ray ab and p
  48. // Vec2 Vec2_closest_ray(const Vec2& a, const Vec2& b, const Vec2& p) {
  49. // // vector along line ab
  50. // Vec2 ab = b - a;
  51. // // vector along line ap
  52. // Vec2 ap = p - a;
  53. // // projection of p onto ab
  54. // float proj = ap.dot(ab);
  55. // // line segment ab length, squared
  56. // float ab_l2 = a.dist2(b);
  57. // // ratio of the projection along line. segment is [0,1] - beyond isn't on line
  58. // float d = proj / ab_l2;
  59. // return a + ab * d;
  60. // }
  61. // // Returns if A, B, C are listed in counterclockwise order
  62. // // Also tells us if C is "to the left of" line AB
  63. // bool Vec2_ccw(const Vec2& A, const Vec2& B, const Vec2& C) {
  64. // return (C.y - A.y) * (B.x - A.x) > (B.y - A.y) * (C.x - A.x);
  65. // }
  66. // // Returns true if line AB intersects with line CD
  67. // bool Vec2_intersect(const Vec2& A, const Vec2& B, const Vec2& C, const Vec2& D) {
  68. // return Vec2_ccw(A, C, D) != Vec2_ccw(B, C, D) && Vec2_ccw(A, B, C) != Vec2_ccw(A, B, D);
  69. // }
  70. // // Returns intersection point of two lines AB and CD.
  71. // // The intersection point may not lie on either segment.
  72. // Vec2 Vec2_intersection(const Vec2& A, const Vec2& B, const Vec2& C, const Vec2& D) {
  73. // float a = (D.x - C.x) * (C.y - A.y) - (D.y - C.y) * (C.x - A.x);
  74. // float b = (D.x - C.x) * (B.y - A.y) - (D.y - C.y) * (B.x - A.x);
  75. // // int c = (B.x - A.x) * (C.y - A.y) - (B.y - A.y) * (C.x - A.x);
  76. // if(b == 0) {
  77. // // lines are parallel
  78. // return (Vec2){NAN, NAN};
  79. // }
  80. // float alpha = a / b;
  81. // // float beta = c / b;
  82. // return (Vec2){A.x + alpha * (B.x - A.x), A.y + alpha * (B.y - A.y)};
  83. // }
  84. // // Returns distance of ray origin to intersection point on line segment. -1 if no intersection
  85. // float Vec2_ray_line_segment_intersect(
  86. // const Vec2& origin,
  87. // const Vec2& dir,
  88. // const Vec2& l1,
  89. // const Vec2& l2) {
  90. // Vec2 v1 = origin - l1;
  91. // Vec2 v2 = l2 - l1;
  92. // Vec2 v3 = Vec2(-dir.y, dir.x);
  93. // float dot = v2.dot(v3);
  94. // if(fabsf(dot) < (float)0.00001) {
  95. // return -1.0f;
  96. // }
  97. // float t1 = v2.cross(v1) / dot;
  98. // float t2 = v1.dot(v3) / dot;
  99. // if(t1 >= 0 && (t2 >= 0 && t2 <= 1)) {
  100. // return t1;
  101. // }
  102. // return -1.0f;
  103. // }
  104. // // Projects P onto AB and returns true if P lies on line segment AB
  105. // // [dst] is the location of P projected onto AB
  106. // bool Vec2_project(const Vec2& A, const Vec2& B, const Vec2& P, Vec2& dst) {
  107. // Vec2 AB(B - A);
  108. // Vec2 AP(P - A);
  109. // float k = AP.dot(AB) / AB.dot(AB);
  110. // if(k < 0 || k > 1) return false;
  111. // dst = A + AB * k;
  112. // return true;
  113. // }
  114. // bool Vec2_point_circle_collide(const Vec2& point, const Vec2& circle, float radius) {
  115. // Vec2 d = {circle.x - point.x, circle.y - point.y};
  116. // return d.mag() <= radius * radius;
  117. // }
  118. // bool Vec2_line_circle_collide(
  119. // const Vec2& a,
  120. // const Vec2& b,
  121. // const Vec2& circle,
  122. // float radius,
  123. // Vec2* nearest) {
  124. // // check if start or end points lie within circle
  125. // if(Vec2_point_circle_collide(a, circle, radius)) {
  126. // if(nearest) {
  127. // *nearest = a;
  128. // }
  129. // return true;
  130. // }
  131. // if(Vec2_point_circle_collide(b, circle, radius)) {
  132. // if(nearest) {
  133. // *nearest = b;
  134. // }
  135. // return true;
  136. // }
  137. // float x1 = a.x;
  138. // float y1 = a.y;
  139. // float x2 = b.x;
  140. // float y2 = b.y;
  141. // float cx = circle.x;
  142. // float cy = circle.y;
  143. // float dx = x2 - x1;
  144. // float dy = y2 - y1;
  145. // float lcx = cx - x1;
  146. // float lcy = cy - y1;
  147. // // project lc onto d, resulting in vector p
  148. // float d_len2 = dx * dx + dy * dy;
  149. // float px = dx;
  150. // float py = dy;
  151. // if(d_len2 > 0) {
  152. // float dp = (lcx * dx + lcy * dy) / d_len2;
  153. // px *= dp;
  154. // py *= dp;
  155. // }
  156. // Vec2 nearest_tmp(x1 + px, y1 + py);
  157. // float p_len2 = px * px + py * py;
  158. // if(nearest) {
  159. // *nearest = nearest_tmp;
  160. // }
  161. // return Vec2_point_circle_collide(nearest_tmp, circle, radius) && p_len2 <= d_len2 &&
  162. // (px * dx + py * dy) >= 0;
  163. // }