Skip to content

Instantly share code, notes, and snippets.

@lengstrom
lengstrom / doSegmentsIntersect.js
Created January 19, 2014 01:46
Algorithm for checking whether two line segments intersect, written in Javascript.
function intersect(x1, y1, x2, y2, x3, y3, x4, y4){
var a1, a2, b1, b2, c1, c2;
var r1, r2 , r3, r4;
var denom, offset, num;
// Compute a1, b1, c1, where line joining points 1 and 2
// is "a1 x + b1 y + c1 = 0".
a1 = y2 - y1;
b1 = x1 - x2;
c1 = (x2 * y1) - (x1 * y2);