Moving squaredDistanceToSegment to the coordinate package
This commit is contained in:
@@ -1,31 +0,0 @@
|
||||
goog.provide('ol.geom');
|
||||
|
||||
goog.require('ol.coordinate');
|
||||
|
||||
|
||||
/**
|
||||
* Calculate the squared distance from a point to a line segment.
|
||||
*
|
||||
* @param {ol.Coordinate} coordinate Coordinate of the point.
|
||||
* @param {Array.<ol.Coordinate>} segment Line segment (2 coordinates).
|
||||
* @return {number} Squared distance from the point to the line segment.
|
||||
*/
|
||||
ol.geom.squaredDistanceToSegment = function(coordinate, segment) {
|
||||
// http://de.softuses.com/103478, Kommentar #1
|
||||
var v = segment[0];
|
||||
var w = segment[1];
|
||||
var l2 = ol.coordinate.squaredDistance(v, w);
|
||||
if (l2 === 0) {
|
||||
return ol.coordinate.squaredDistance(coordinate, v);
|
||||
}
|
||||
var t = ((coordinate[0] - v[0]) * (w[0] - v[0]) +
|
||||
(coordinate[1] - v[1]) * (w[1] - v[1])) / l2;
|
||||
if (t < 0) {
|
||||
return ol.coordinate.squaredDistance(coordinate, v);
|
||||
}
|
||||
if (t > 1) {
|
||||
return ol.coordinate.squaredDistance(coordinate, w);
|
||||
}
|
||||
return ol.coordinate.squaredDistance(coordinate,
|
||||
[v[0] + t * (w[0] - v[0]), v[1] + t * (w[1] - v[1])]);
|
||||
};
|
||||
@@ -3,6 +3,7 @@ goog.provide('ol.geom.LineString');
|
||||
goog.require('goog.asserts');
|
||||
goog.require('goog.events.EventType');
|
||||
goog.require('ol.CoordinateArray');
|
||||
goog.require('ol.coordinate');
|
||||
goog.require('ol.extent');
|
||||
goog.require('ol.geom');
|
||||
goog.require('ol.geom.Geometry');
|
||||
@@ -104,7 +105,7 @@ ol.geom.LineString.prototype.distanceFromCoordinate = function(coordinate) {
|
||||
var coordinates = this.getCoordinates();
|
||||
var dist2 = Infinity;
|
||||
for (var i = 0, j = 1, len = coordinates.length; j < len; i = j++) {
|
||||
dist2 = Math.min(dist2, ol.geom.squaredDistanceToSegment(coordinate,
|
||||
dist2 = Math.min(dist2, ol.coordinate.squaredDistanceToSegment(coordinate,
|
||||
[coordinates[i], coordinates[j]]));
|
||||
}
|
||||
return Math.sqrt(dist2);
|
||||
|
||||
Reference in New Issue
Block a user