From b6cc99be8fbd23f7b42b086dd16d8b15c63b58f3 Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Fri, 25 Feb 2011 13:34:59 +0000 Subject: [PATCH] Adding a distanceTo for pixels. p=bbinet, r=me (closes #3119) git-svn-id: http://svn.openlayers.org/trunk/openlayers@11491 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf --- lib/OpenLayers/BaseTypes/Pixel.js | 18 ++++++++++++++++++ tests/BaseTypes/Pixel.html | 13 ++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/lib/OpenLayers/BaseTypes/Pixel.js b/lib/OpenLayers/BaseTypes/Pixel.js index 19dcf6acfd..5426bb8d4c 100644 --- a/lib/OpenLayers/BaseTypes/Pixel.js +++ b/lib/OpenLayers/BaseTypes/Pixel.js @@ -85,6 +85,24 @@ OpenLayers.Pixel = OpenLayers.Class({ return equals; }, + /** + * APIMethod: distanceTo + * Returns the distance to the pixel point passed in as a parameter. + * + * Parameters: + * px - {} + * + * Returns: + * {Float} The pixel point passed in as parameter to calculate the + * distance to. + */ + distanceTo:function(px) { + return Math.sqrt( + Math.pow(this.x - px.x, 2) + + Math.pow(this.y - px.y, 2) + ); + }, + /** * APIMethod: add * diff --git a/tests/BaseTypes/Pixel.html b/tests/BaseTypes/Pixel.html index 561549e2c6..1a8bf47303 100644 --- a/tests/BaseTypes/Pixel.html +++ b/tests/BaseTypes/Pixel.html @@ -40,11 +40,22 @@ t.eq( pixel.x, 5, "changing oldPixel.x doesn't change pixel.x"); } + function test_Pixel_distanceTo(t) { + t.plan( 2 ); + var px = new OpenLayers.Pixel(0,-2); + pixel = new OpenLayers.Pixel(0,0); + t.eq( pixel.distanceTo(px), 2, "(0,0) distanceTo (0,-2) = 2"); + + px = new OpenLayers.Pixel(-4,6); + pixel = new OpenLayers.Pixel(4,6); + t.eq( pixel.distanceTo(px), 8, "(4,6) distanceTo (-4,6) = 8"); + } + function test_Pixel_equals(t) { t.plan( 5 ); pixel = new OpenLayers.Pixel(5,6); - px = new OpenLayers.Pixel(5,6); + var px = new OpenLayers.Pixel(5,6); t.eq( pixel.equals(px), true, "(5,6) equals (5,6)"); px = new OpenLayers.Pixel(1,6);