Skip to content

Instantly share code, notes, and snippets.

@mitselek
Created November 23, 2016 11:05
Show Gist options
  • Save mitselek/ef53e8d5775559114ad8d1fc8c07115f to your computer and use it in GitHub Desktop.
Save mitselek/ef53e8d5775559114ad8d1fc8c07115f to your computer and use it in GitHub Desktop.
Test if two segments intersect
function intersect(a,b,c,d) {
var a_cd = (d.x - c.x) * (a.y - c.y) > (d.y - c.y) * (a.x - c.x)
var b_cd = (d.x - c.x) * (b.y - c.y) > (d.y - c.y) * (b.x - c.x)
var c_ab = (b.x - a.x) * (c.y - a.y) > (b.y - a.y) * (c.x - a.x)
var d_ab = (b.x - a.x) * (d.y - a.y) > (b.y - a.y) * (d.x - a.x)
return a_cd !== b_cd && c_ab !== d_ab
}
console.log(intersect({x:0,y:0},{x:1,y:1},{x:1,y:0},{x:0,y:1}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment