Move squaredDistance and squaredSegmentDistance into ol.math

This commit is contained in:
Tom Payne
2014-03-12 13:12:25 +01:00
parent 65dcc38fad
commit 6a00c14841
6 changed files with 55 additions and 53 deletions

View File

@@ -3,6 +3,7 @@ goog.provide('ol.geom.flat.closest');
goog.require('goog.asserts');
goog.require('goog.math');
goog.require('ol.geom.flat');
goog.require('ol.math');
/**
@@ -65,7 +66,7 @@ ol.geom.flat.closest.getMaxSquaredDelta =
for (offset += stride; offset < end; offset += stride) {
var x2 = flatCoordinates[offset];
var y2 = flatCoordinates[offset + 1];
var squaredDelta = ol.geom.flat.squaredDistance(x1, y1, x2, y2);
var squaredDelta = ol.math.squaredDistance(x1, y1, x2, y2);
if (squaredDelta > maxSquaredDelta) {
maxSquaredDelta = squaredDelta;
}
@@ -141,7 +142,7 @@ ol.geom.flat.closest.getClosestPoint = function(flatCoordinates, offset, end,
var i, squaredDistance;
if (maxDelta === 0) {
// All points are identical, so just test the first point.
squaredDistance = ol.geom.flat.squaredDistance(
squaredDistance = ol.math.squaredDistance(
x, y, flatCoordinates[offset], flatCoordinates[offset + 1]);
if (squaredDistance < minSquaredDistance) {
for (i = 0; i < stride; ++i) {
@@ -159,8 +160,7 @@ ol.geom.flat.closest.getClosestPoint = function(flatCoordinates, offset, end,
while (index < end) {
ol.geom.flat.closest.point(
flatCoordinates, index - stride, index, stride, x, y, tmpPoint);
squaredDistance = ol.geom.flat.squaredDistance(
x, y, tmpPoint[0], tmpPoint[1]);
squaredDistance = ol.math.squaredDistance(x, y, tmpPoint[0], tmpPoint[1]);
if (squaredDistance < minSquaredDistance) {
minSquaredDistance = squaredDistance;
for (i = 0; i < stride; ++i) {
@@ -188,8 +188,7 @@ ol.geom.flat.closest.getClosestPoint = function(flatCoordinates, offset, end,
// Check the closing segment.
ol.geom.flat.closest.point(
flatCoordinates, end - stride, offset, stride, x, y, tmpPoint);
squaredDistance = ol.geom.flat.squaredDistance(
x, y, tmpPoint[0], tmpPoint[1]);
squaredDistance = ol.math.squaredDistance(x, y, tmpPoint[0], tmpPoint[1]);
if (squaredDistance < minSquaredDistance) {
minSquaredDistance = squaredDistance;
for (i = 0; i < stride; ++i) {

View File

@@ -27,6 +27,7 @@
goog.provide('ol.geom.flat.simplify');
goog.require('ol.geom.flat');
goog.require('ol.math');
/**
@@ -101,7 +102,7 @@ ol.geom.flat.simplify.douglasPeucker = function(flatCoordinates, offset, end,
for (i = first + stride; i < last; i += stride) {
var x = flatCoordinates[i];
var y = flatCoordinates[i + 1];
var squaredDistance = ol.geom.flat.squaredSegmentDistance(
var squaredDistance = ol.math.squaredSegmentDistance(
x, y, x1, y1, x2, y2);
if (squaredDistance > maxSquaredDistance) {
index = i;
@@ -219,7 +220,7 @@ ol.geom.flat.simplify.radialDistance = function(flatCoordinates, offset, end,
for (offset += stride; offset < end; offset += stride) {
x2 = flatCoordinates[offset];
y2 = flatCoordinates[offset + 1];
if (ol.geom.flat.squaredDistance(x1, y1, x2, y2) > squaredTolerance) {
if (ol.math.squaredDistance(x1, y1, x2, y2) > squaredTolerance) {
// copy point at offset
simplifiedFlatCoordinates[simplifiedOffset++] = x2;
simplifiedFlatCoordinates[simplifiedOffset++] = y2;

View File

@@ -28,49 +28,6 @@ ol.geom.flat.linearRingssGetFlatCenters =
};
/**
* 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.geom.flat.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.geom.flat.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.geom.flat.squaredDistance = function(x1, y1, x2, y2) {
var dx = x2 - x1;
var dy = y2 - y1;
return dx * dx + dy * dy;
};
/**
* @param {Array.<number>} flatCoordinates Flat coordinates.
* @param {number} stride Stride.

View File

@@ -9,6 +9,7 @@ goog.require('ol.geom.SimpleGeometry');
goog.require('ol.geom.flat');
goog.require('ol.geom.flat.deflate');
goog.require('ol.geom.flat.inflate');
goog.require('ol.math');
@@ -63,7 +64,7 @@ ol.geom.MultiPoint.prototype.closestPointXY =
var stride = this.stride;
var i, ii, j;
for (i = 0, ii = flatCoordinates.length; i < ii; i += stride) {
var squaredDistance = ol.geom.flat.squaredDistance(
var squaredDistance = ol.math.squaredDistance(
x, y, flatCoordinates[i], flatCoordinates[i + 1]);
if (squaredDistance < minSquaredDistance) {
minSquaredDistance = squaredDistance;

View File

@@ -6,6 +6,7 @@ goog.require('ol.geom.GeometryType');
goog.require('ol.geom.SimpleGeometry');
goog.require('ol.geom.flat');
goog.require('ol.geom.flat.deflate');
goog.require('ol.math');
@@ -39,7 +40,7 @@ ol.geom.Point.prototype.clone = function() {
ol.geom.Point.prototype.closestPointXY =
function(x, y, closestPoint, minSquaredDistance) {
var flatCoordinates = this.flatCoordinates;
var squaredDistance = ol.geom.flat.squaredDistance(
var squaredDistance = ol.math.squaredDistance(
x, y, flatCoordinates[0], flatCoordinates[1]);
if (squaredDistance < minSquaredDistance) {
var stride = this.stride;

View File

@@ -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.