From ee58e334a536b61cf1f18b8a1841c5548bbd15e8 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Sat, 7 Dec 2013 10:10:02 +0100 Subject: [PATCH] Move squaredDistance and squaredSegmentDistance in to ol.geom.flat --- src/ol/geom/flatgeom.js | 43 +++++++++++++++++++++++++++++++ src/ol/geom/simplifygeom.js | 51 ++++--------------------------------- 2 files changed, 48 insertions(+), 46 deletions(-) diff --git a/src/ol/geom/flatgeom.js b/src/ol/geom/flatgeom.js index 60ca92c914..8e87578e94 100644 --- a/src/ol/geom/flatgeom.js +++ b/src/ol/geom/flatgeom.js @@ -656,6 +656,49 @@ ol.geom.flat.reverseCoordinates = }; +/** + * 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.} flatCoordinates Flat coordinates. * @param {number} stride Stride. diff --git a/src/ol/geom/simplifygeom.js b/src/ol/geom/simplifygeom.js index eb00ba57bf..d7cab5ea77 100644 --- a/src/ol/geom/simplifygeom.js +++ b/src/ol/geom/simplifygeom.js @@ -26,6 +26,8 @@ goog.provide('ol.geom.simplify'); +goog.require('ol.geom.flat'); + /** * @param {Array.} flatCoordinates Flat coordinates. @@ -98,8 +100,8 @@ ol.geom.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.simplify.squaredSegmentDistance(x, y, x1, y1, x2, y2); + var squaredDistance = ol.geom.flat.squaredSegmentDistance( + x, y, x1, y1, x2, y2); if (squaredDistance > maxSquaredDistance) { index = i; maxSquaredDistance = squaredDistance; @@ -216,7 +218,7 @@ ol.geom.simplify.radialDistance = function(flatCoordinates, offset, end, for (offset += stride; offset < end; offset += stride) { x2 = flatCoordinates[offset]; y2 = flatCoordinates[offset + 1]; - if (ol.geom.simplify.squaredDistance(x1, y1, x2, y2) > squaredTolerance) { + if (ol.geom.flat.squaredDistance(x1, y1, x2, y2) > squaredTolerance) { // copy point at offset simplifiedFlatCoordinates[simplifiedOffset++] = x2; simplifiedFlatCoordinates[simplifiedOffset++] = y2; @@ -231,46 +233,3 @@ ol.geom.simplify.radialDistance = function(flatCoordinates, offset, end, } return simplifiedOffset; }; - - -/** - * 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.simplify.squaredDistance = function(x1, y1, x2, y2) { - var dx = x2 - x1; - var dy = y2 - y1; - return dx * dx + dy * dy; -}; - - -/** - * 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.simplify.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.simplify.squaredDistance(x, y, x1, y1); -};