Created
May 11, 2012 12:33
-
-
Save albburtsev/2659353 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
/** | |
* @ignore | |
* @description Вычисление расстояния D от точки p, до отрезка, задаваемоого точками p1 и p2 | |
* @description Также вычисляются координаты точки пересечения (xN, yN) перпендикуляра, проведенного из точки к отрезку | |
* @description Если точка пересечение лежит вне отрезка, или отрезок нулевой длины - вернется false | |
*/ | |
vertical: function(p1, p2, p) { | |
var x1 = p1.lon, | |
y1 = p1.lat, | |
x2 = p2.lon, | |
y2 = p2.lat, | |
xMin = Math.min(x1, x2), | |
xMax = Math.max(x1, x2), | |
yMin = Math.min(y1, y2), | |
yMax = Math.max(y1, y2), | |
x = p.lon, | |
y = p.lat, | |
xN, yN, | |
L, D; | |
L = Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2)); | |
if ( !L ) | |
return false; | |
D = L ? ((x - x2) * (y2 - y1) - (y - y2) * (x2 - x1)) / L : false; | |
xN = ((x2-x1) * (y2-y1) * (y-y1) + x1 * Math.pow(y2-y1, 2) + x * Math.pow(x2-x1, 2)) / (Math.pow(y2-y1, 2)+Math.pow(x2-x1, 2)); | |
yN = (y2-y1) * (xN-x1) / (x2-x1) + y1; | |
if ( xN >= xMin && xN <= xMax && yN >= yMin && yN <= yMax ) | |
return [D, xN, yN]; | |
return false; | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment