Skip to content

Instantly share code, notes, and snippets.

@jonybekov
Last active February 28, 2024 13:10
Show Gist options
  • Save jonybekov/9e8d17b89502b333da0d773779ca55c6 to your computer and use it in GitHub Desktop.
Save jonybekov/9e8d17b89502b333da0d773779ca55c6 to your computer and use it in GitHub Desktop.
Find line-line and polygon-line intersection
function Intersection(status) {
if ( arguments.length > 0 ) {
this.init(status);
}
}
/*****
*
* init
*
*****/
Intersection.prototype.init = function(status) {
this.status = status;
this.points = new Array();
};
/*****
*
* appendPoint
*
*****/
Intersection.prototype.appendPoint = function(point) {
this.points.push(point);
};
/*****
*
* appendPoints
*
*****/
Intersection.prototype.appendPoints = function(points) {
this.points = this.points.concat(points);
};
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(
[
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;
};
Intersection.intersectLinePolygon = function(a1, a2, points) {
var result = new Intersection("No Intersection");
var length = points.length;
for ( var i = 0; i < length; i++ ) {
var b1 = points[i];
var b2 = points[(i+1) % length];
var inter = Intersection.intersectLineLine(a1, a2, b1, b2);
result.appendPoints(inter.points);
}
if ( result.points.length > 0 ) result.status = "Intersection";
return result;
};
@jonybekov
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment