src/index.js
function lineSegmentsIntersect(p1, p2, q1, q2){
	var dx = p2[0] - p1[0];
	var dy = p2[1] - p1[1];
	var da = q2[0] - q1[0];
	var db = q2[1] - q1[1];

	// segments are parallel
	if((da*dy - db*dx) === 0){
		return false;
	}

	var s = (dx * (q1[1] - p1[1]) + dy * (p1[0] - q1[0])) / (da * dy - db * dx);
	var t = (da * (p1[1] - q1[1]) + db * (q1[0] - p1[0])) / (db * dx - da * dy);

	return (s>=0 && s<=1 && t>=0 && t<=1);
}
function lineSegmentsIntersect(p1, p2, q1, q2){
	var dx = p2[0] - p1[0];
	var dy = p2[1] - p1[1];
	var da = q2[0] - q1[0];
	var db = q2[1] - q1[1];

	// segments are parallel
	if((da*dy - db*dx) === 0){
		return false;
	}

	var s = (dx * (q1[1] - p1[1]) + dy * (p1[0] - q1[0])) / (da * dy - db * dx);
	var t = (da * (p1[1] - q1[1]) + db * (q1[0] - p1[0])) / (db * dx - da * dy);

	return (s>=0 && s<=1 && t>=0 && t<=1);
}
function lineSegmentsIntersect ( p1 , p2 , q1 , q2 ) { var dx = p2 [ 0 ] - p1 [ 0 ] ; var dy = p2 [ 1 ] - p1 [ 1 ] ; var da = q2 [ 0 ] - q1 [ 0 ] ; var db = q2 [ 1 ] - q1 [ 1 ] ; // segments are parallel if ( ( da * dy - db * dx ) === 0 ) { return false ; } var s = ( dx * ( q1 [ 1 ] - p1 [ 1 ] ) + dy * ( p1 [ 0 ] - q1 [ 0 ] ) ) / ( da * dy - db * dx ) ; var t = ( da * ( p1 [ 1 ] - q1 [ 1 ] ) + db * ( q1 [ 0 ] - p1 [ 0 ] ) ) / ( db * dx - da * dy ) ; return ( s >= 0 && s <= 1 && t >= 0 && t <= 1 ) ; }
Checks if two line segments intersects.
@method segmentsIntersect
@param {Array} p1 The start vertex of the first line segment.
@param {Array} p2 The end vertex of the first line segment.
@param {Array} q1 The start vertex of the second line segment.
@param {Array} q2 The end vertex of the second line segment.
@return {Boolean} True if the two line segments intersect
Checks if two line segments intersects .