Move squaredDistance and squaredSegmentDistance into ol.math
This commit is contained in:
@@ -59,6 +59,49 @@ ol.math.sinh = function(x) {
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Returns the square of the closest distance between the point (x, y) and the
|
||||
* line segment (x1, y1) to (x2, y2).
|
||||
* @param {number} x X.
|
||||
* @param {number} y Y.
|
||||
* @param {number} x1 X1.
|
||||
* @param {number} y1 Y1.
|
||||
* @param {number} x2 X2.
|
||||
* @param {number} y2 Y2.
|
||||
* @return {number} Squared distance.
|
||||
*/
|
||||
ol.math.squaredSegmentDistance = function(x, y, x1, y1, x2, y2) {
|
||||
var dx = x2 - x1;
|
||||
var dy = y2 - y1;
|
||||
if (dx !== 0 || dy !== 0) {
|
||||
var t = ((x - x1) * dx + (y - y1) * dy) / (dx * dx + dy * dy);
|
||||
if (t > 1) {
|
||||
x1 = x2;
|
||||
y1 = y2;
|
||||
} else if (t > 0) {
|
||||
x1 += dx * t;
|
||||
y1 += dy * t;
|
||||
}
|
||||
}
|
||||
return ol.math.squaredDistance(x, y, x1, y1);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Returns the square of the distance between the points (x1, y1) and (x2, y2).
|
||||
* @param {number} x1 X1.
|
||||
* @param {number} y1 Y1.
|
||||
* @param {number} x2 X2.
|
||||
* @param {number} y2 Y2.
|
||||
* @return {number} Squared distance.
|
||||
*/
|
||||
ol.math.squaredDistance = function(x1, y1, x2, y2) {
|
||||
var dx = x2 - x1;
|
||||
var dy = y2 - y1;
|
||||
return dx * dx + dy * dy;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {number} x X.
|
||||
* @return {number} Hyperbolic tangent of x.
|
||||
|
||||
Reference in New Issue
Block a user