segment to segment intersection (2d)
JavaScript
Switch branches/tags
Latest commit 4134758 Jun 10, 2016 @tmpvar 0.2.2
Permalink
Failed to load latest commit information.
test
.gitignore
.travis.yml
README.md
index.js
package.json

README.md

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.