segseg

2d segment to segment intersection detection

npm install segseg
9 downloads in the last week
14 downloads in the last month

segseg

Intersection detection between two line segments in 2d space

Signature

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

returns:

  • [x, y] - intersection
  • true - colinear
  • undefined - no intersection

Usage

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

Credits

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

npm loves you