intersect-line.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Returns the point at which two lines, p and q, intersect or returns
  3. * undefined if they do not intersect.
  4. */
  5. function intersectLine (p1, p2, q1, q2) {
  6. // Algorithm from J. Avro, (ed.) Graphics Gems, No 2, Morgan Kaufmann, 1994,
  7. // p7 and p473.
  8. // Compute a1, b1, c1, where line joining points 1 and 2 is F(x,y) = a1 x +
  9. // b1 y + c1 = 0.
  10. const a1 = p2.y - p1.y
  11. const b1 = p1.x - p2.x
  12. const c1 = (p2.x * p1.y) - (p1.x * p2.y)
  13. // Compute r3 and r4.
  14. const r3 = ((a1 * q1.x) + (b1 * q1.y) + c1)
  15. const r4 = ((a1 * q2.x) + (b1 * q2.y) + c1)
  16. // Check signs of r3 and r4. If both point 3 and point 4 lie on
  17. // same side of line 1, the line segments do not intersect.
  18. if ((r3 !== 0) && (r4 !== 0) && sameSign(r3, r4)) {
  19. return /* DONT_INTERSECT */
  20. }
  21. // Compute a2, b2, c2 where line joining points 3 and 4 is G(x,y) = a2 x + b2 y + c2 = 0
  22. const a2 = q2.y - q1.y
  23. const b2 = q1.x - q2.x
  24. const c2 = (q2.x * q1.y) - (q1.x * q2.y)
  25. // Compute r1 and r2
  26. const r1 = (a2 * p1.x) + (b2 * p1.y) + c2
  27. const r2 = (a2 * p2.x) + (b2 * p2.y) + c2
  28. // Check signs of r1 and r2. If both point 1 and point 2 lie
  29. // on same side of second line segment, the line segments do
  30. // not intersect.
  31. if ((r1 !== 0) && (r2 !== 0) && (sameSign(r1, r2))) {
  32. return /* DONT_INTERSECT */
  33. }
  34. // Line segments intersect: compute intersection point.
  35. const denom = (a1 * b2) - (a2 * b1)
  36. if (denom === 0) {
  37. return /* COLLINEAR */
  38. }
  39. const offset = Math.abs(denom / 2)
  40. // The denom/2 is to get rounding instead of truncating. It
  41. // is added or subtracted to the numerator, depending upon the
  42. // sign of the numerator.
  43. let num = (b1 * c2) - (b2 * c1)
  44. const x = (num < 0) ? ((num - offset) / denom) : ((num + offset) / denom)
  45. num = (a2 * c1) - (a1 * c2)
  46. const y = (num < 0) ? ((num - offset) / denom) : ((num + offset) / denom)
  47. return { x, y }
  48. }
  49. function sameSign (r1, r2) {
  50. return r1 * r2 > 0
  51. }
  52. export default intersectLine