Skip to content

Instantly share code, notes, and snippets.

@swannodette
Created November 5, 2012 03:47
Show Gist options
  • Save swannodette/4015208 to your computer and use it in GitHub Desktop.
Save swannodette/4015208 to your computer and use it in GitHub Desktop.
intersect.js
Intersection.intersectLineLine = function(a1, a2, b1, b2) {
var result;
var ua_t = (b2.x - b1.x) * (a1.y - b1.y) - (b2.y - b1.y) * (a1.x - b1.x);
var ub_t = (a2.x - a1.x) * (a1.y - b1.y) - (a2.y - a1.y) * (a1.x - b1.x);
var u_b = (b2.y - b1.y) * (a2.x - a1.x) - (b2.x - b1.x) * (a2.y - a1.y);
if ( u_b != 0 ) {
var ua = ua_t / u_b;
var ub = ub_t / u_b;
if ( 0 <= ua && ua <= 1 && 0 <= ub && ub <= 1 ) {
result = new Intersection("Intersection");
result.points.push(
new Point2D(
a1.x + ua * (a2.x - a1.x),
a1.y + ua * (a2.y - a1.y)
)
);
} else {
result = new Intersection("No Intersection");
}
} else {
if ( ua_t == 0 || ub_t == 0 ) {
result = new Intersection("Coincident");
} else {
result = new Intersection("Parallel");
}
}
return result;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment