segseg

2d segment to segment intersection detection

segseg

Intersection detection between two line segments in 2d space

segseg(x1, y1, x2, y2, x3, y3, x4, y4)

returns:

  • [x, y] - intersection
  • true - colinear
  • undefined - no intersection
var segseg = require('segseg');
/*
                (0, 5)
                   |
  (-10, 0) ----------------  (10, 0)
                   |
                (0, -5)
*/
var isect = segseg(-10, 0, 10, 0, 0, 5, 0, -5);
 
console.log(isect.join(',')) // outputs: 0,0 
 

This code was ported from Mukesh Prasad's example implementation which was included in graphics gems 2.