Created
October 9, 2015 20:10
-
-
Save gordonwoodhull/50eb65d2f048789f9558 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Adapted from http://stackoverflow.com/questions/563198/how-do-you-detect-where-two-line-segments-intersect/1968345#1968345 | |
var eps = 0.0000001; | |
function between(a, b, c) { | |
return a-eps <= b && b <= c+eps; | |
} | |
function segment_intersection(x1,y1,x2,y2, x3,y3,x4,y4) { | |
var x=((x1*y2-y1*x2)*(x3-x4)-(x1-x2)*(x3*y4-y3*x4)) / | |
((x1-x2)*(y3-y4)-(y1-y2)*(x3-x4)); | |
var y=((x1*y2-y1*x2)*(y3-y4)-(y1-y2)*(x3*y4-y3*x4)) / | |
((x1-x2)*(y3-y4)-(y1-y2)*(x3-x4)); | |
if (isNaN(x)||isNaN(y)) { | |
return false; | |
} else { | |
if (x1>=x2) { | |
if (!between(x2, x, x1)) {return false;} | |
} else { | |
if (!between(x1, x, x2)) {return false;} | |
} | |
if (y1>=y2) { | |
if (!between(y2, y, y1)) {return false;} | |
} else { | |
if (!between(y1, y, y2)) {return false;} | |
} | |
if (x3>=x4) { | |
if (!between(x4, x, x3)) {return false;} | |
} else { | |
if (!between(x3, x, x4)) {return false;} | |
} | |
if (y3>=y4) { | |
if (!between(y4, y, y3)) {return false;} | |
} else { | |
if (!between(y3, y, y4)) {return false;} | |
} | |
} | |
return {x: x, y: y}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@YesIDont If I understand you correctly I think you're looking for something like this?
(defined on a Line class so that's what this is referring to)