Skip to content

Instantly share code, notes, and snippets.

@rapodaca
Created September 26, 2011 04:31
Show Gist options
  • Save rapodaca/1241613 to your computer and use it in GitHub Desktop.
Save rapodaca/1241613 to your computer and use it in GitHub Desktop.
Distance from a point to a line or segment.
/**
* See: http://jsfromhell.com/math/dot-line-length
*
* Distance from a point to a line or segment.
*
* @param {number} x point's x coord
* @param {number} y point's y coord
* @param {number} x0 x coord of the line's A point
* @param {number} y0 y coord of the line's A point
* @param {number} x1 x coord of the line's B point
* @param {number} y1 y coord of the line's B point
* @param {boolean} overLine specifies if the distance should respect the limits
* of the segment (overLine = true) or if it should consider the segment as an
* infinite line (overLine = false), if false returns the distance from the point to
* the line, otherwise the distance from the point to the segment.
*/
var dotLineLength = function(x, y, x0, y0, x1, y1, o) {
function lineLength(x, y, x0, y0){
return Math.sqrt((x -= x0) * x + (y -= y0) * y);
}
if(o && !(o = function(x, y, x0, y0, x1, y1){
if(!(x1 - x0)) return {x: x0, y: y};
else if(!(y1 - y0)) return {x: x, y: y0};
var left, tg = -1 / ((y1 - y0) / (x1 - x0));
return {x: left = (x1 * (x * tg - y + y0) + x0 * (x * - tg + y - y1)) / (tg * (x1 - x0) + y0 - y1), y: tg * left - tg * x + y};
}(x, y, x0, y0, x1, y1), o.x >= Math.min(x0, x1) && o.x <= Math.max(x0, x1) && o.y >= Math.min(y0, y1) && o.y <= Math.max(y0, y1))){
var l1 = lineLength(x, y, x0, y0), l2 = lineLength(x, y, x1, y1);
return l1 > l2 ? l2 : l1;
}
else {
var a = y0 - y1, b = x1 - x0, c = x0 * y1 - y0 * x1;
return Math.abs(a * x + b * y + c) / Math.sqrt(a * a + b * b);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment