From 04d3c6ce538e007a107bc94ab9baccb715b737db Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Sat, 23 Mar 2013 16:49:30 +0100 Subject: [PATCH 1/3] Geometry: Added along attribute to distanceToSegment() function --- lib/OpenLayers/Geometry.js | 6 ++++-- tests/Geometry.html | 13 ++++++------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/OpenLayers/Geometry.js b/lib/OpenLayers/Geometry.js index 9cc0c5d11c..bc624ecd91 100644 --- a/lib/OpenLayers/Geometry.js +++ b/lib/OpenLayers/Geometry.js @@ -441,7 +441,8 @@ OpenLayers.Geometry.segmentsIntersect = function(seg1, seg2, options) { * {Object} An object with distance, x, and y properties. The distance * will be the shortest distance between the input point and segment. * The x and y properties represent the coordinates along the segment - * where the shortest distance meets the segment. + * where the shortest distance meets the segment. The along attribute + * describes how far between the two segment points the given point is. */ OpenLayers.Geometry.distanceToSegment = function(point, segment) { var x0 = point.x; @@ -467,6 +468,7 @@ OpenLayers.Geometry.distanceToSegment = function(point, segment) { } return { distance: Math.sqrt(Math.pow(x - x0, 2) + Math.pow(y - y0, 2)), - x: x, y: y + x: x, y: y, + along: along }; }; diff --git a/tests/Geometry.html b/tests/Geometry.html index 6bae0ca16f..2a4b4c46c3 100644 --- a/tests/Geometry.html +++ b/tests/Geometry.html @@ -257,24 +257,23 @@ var cases = [{ got: dist({x: 0, y: 0}, {x1: 0, y1: 1, x2: 1, y2: 1}), - expected: {distance: 1, x: 0, y: 1} + expected: {distance: 1, x: 0, y: 1, along: 0} }, { got: dist({x: 0, y: 0}, {x1: -1, y1: -1, x2: 0, y2: -1}), - expected: {distance: 1, x: 0, y: -1} + expected: {distance: 1, x: 0, y: -1, along: 1} }, { got: dist({x: 0, y: 0}, {x1: -1, y1: -1, x2: 1, y2: 1}), - expected: {distance: 0, x: 0, y: 0} + expected: {distance: 0, x: 0, y: 0, along: 0.5} }, { got: dist({x: 1, y: 1}, {x1: 2, y1: 0, x2: 2, y2: 3}), - expected: {distance: 1, x: 2, y: 1} + expected: {distance: 1, x: 2, y: 1, along: 1/3.} }, { got: dist({x: -1, y: -1}, {x1: -2, y1: -2, x2: -1, y2: -3}), - expected: {distance: Math.sqrt(2), x: -2, y: -2} + expected: {distance: Math.sqrt(2), x: -2, y: -2, along: 0} }, { got: dist({x: -1, y: 1}, {x1: -3, y1: 1, x2: -1, y2: 3}), - expected: {distance: Math.sqrt(2), x: -2, y: 2} + expected: {distance: Math.sqrt(2), x: -2, y: 2, along: 0.5} }]; - t.plan(cases.length); for(var i=0; i Date: Sat, 23 Mar 2013 16:56:32 +0100 Subject: [PATCH 2/3] Geometry: Added distanceSquaredToSegment() function --- lib/OpenLayers/Geometry.js | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/lib/OpenLayers/Geometry.js b/lib/OpenLayers/Geometry.js index bc624ecd91..fbab941601 100644 --- a/lib/OpenLayers/Geometry.js +++ b/lib/OpenLayers/Geometry.js @@ -445,6 +445,31 @@ OpenLayers.Geometry.segmentsIntersect = function(seg1, seg2, options) { * describes how far between the two segment points the given point is. */ OpenLayers.Geometry.distanceToSegment = function(point, segment) { + var result = OpenLayers.Geometry.distanceSquaredToSegment(point, segment); + result.distance = Math.sqrt(result.distance); + return result; +}; + +/** + * Function: OpenLayers.Geometry.distanceSquaredToSegment + * + * Usually the distanceToSegment function should be used. This variant however + * can be used for comparisons where the exact distance is not important. + * + * Parameters: + * point - {Object} An object with x and y properties representing the + * point coordinates. + * segment - {Object} An object with x1, y1, x2, and y2 properties + * representing endpoint coordinates. + * + * Returns: + * {Object} An object with squared distance, x, and y properties. The distance + * will be the shortest distance between the input point and segment. + * The x and y properties represent the coordinates along the segment + * where the shortest distance meets the segment. The along attribute + * describes how far between the two segment points the given point is. + */ +OpenLayers.Geometry.distanceSquaredToSegment = function(point, segment) { var x0 = point.x; var y0 = point.y; var x1 = segment.x1; @@ -467,7 +492,7 @@ OpenLayers.Geometry.distanceToSegment = function(point, segment) { y = y1 + along * dy; } return { - distance: Math.sqrt(Math.pow(x - x0, 2) + Math.pow(y - y0, 2)), + distance: Math.pow(x - x0, 2) + Math.pow(y - y0, 2), x: x, y: y, along: along }; From 6b79391b5ef5fb0aa92896b9a6b753418fa71421 Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Mon, 25 Mar 2013 19:10:50 +0100 Subject: [PATCH 3/3] Geometry: Added "along" to the API documentation --- lib/OpenLayers/Geometry.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/OpenLayers/Geometry.js b/lib/OpenLayers/Geometry.js index fbab941601..e7b8e59dcf 100644 --- a/lib/OpenLayers/Geometry.js +++ b/lib/OpenLayers/Geometry.js @@ -438,7 +438,7 @@ OpenLayers.Geometry.segmentsIntersect = function(seg1, seg2, options) { * representing endpoint coordinates. * * Returns: - * {Object} An object with distance, x, and y properties. The distance + * {Object} An object with distance, along, x, and y properties. The distance * will be the shortest distance between the input point and segment. * The x and y properties represent the coordinates along the segment * where the shortest distance meets the segment. The along attribute @@ -463,11 +463,12 @@ OpenLayers.Geometry.distanceToSegment = function(point, segment) { * representing endpoint coordinates. * * Returns: - * {Object} An object with squared distance, x, and y properties. The distance - * will be the shortest distance between the input point and segment. - * The x and y properties represent the coordinates along the segment - * where the shortest distance meets the segment. The along attribute - * describes how far between the two segment points the given point is. + * {Object} An object with squared distance, along, x, and y properties. + * The distance will be the shortest distance between the input point and + * segment. The x and y properties represent the coordinates along the + * segment where the shortest distance meets the segment. The along + * attribute describes how far between the two segment points the given + * point is. */ OpenLayers.Geometry.distanceSquaredToSegment = function(point, segment) { var x0 = point.x;