Skip to content

Instantly share code, notes, and snippets.

@Aleksey-Danchin
Created January 21, 2016 22:49
Show Gist options
  • Save Aleksey-Danchin/bd09b7659a14e71a3bc5 to your computer and use it in GitHub Desktop.
Save Aleksey-Danchin/bd09b7659a14e71a3bc5 to your computer and use it in GitHub Desktop.
isIntersection (v2 teory)
function isIntersection (x1, y1, x2, y2, x3, y3, x4, y4) {
var angles = [
getAngleOf(x2, y2, x1, y1, x3, y3) + getAngleOf(x2, y2, x1, y1, x4, y4),
getAngleOf(x4, y4, x3, y3, x1, y1) + getAngleOf(x4, y4, x3, y3, x2, y2),
getAngleOf(x1, y1, x2, y2, x3, y3) + getAngleOf(x1, y1, x2, y2, x4, y4),
getAngleOf(x3, y3, x4, y4, x2, y2) + getAngleOf(x3, y3, x4, y4, x1, y1)
];
var sum = angles[0] + angles[1] + angles[2] + angles[3];
return (sum !== 2 * Math.PI || angles.some(function (a) { return a > Math.PI; })) ? false : (angles.every(function (a) { return a < Math.PI }) || angles.some(function (a) { return a === Math.PI; }));
function dist (x1, y1, x2, y2) {
return Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
}
function getAngleOf(x1, y1, x2, y2, x3, y3) {
var a = dist(x1, y1, x2, y2),
b = dist(x2, y2, x3, y3),
c = dist(x1, y1, x3, y3);
return Math.acos((a*a + b*b - c*c) / (2*a*b)) || 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment