From 93f3631f7f329354be0bde47bb8998b7d540ca30 Mon Sep 17 00:00:00 2001 From: Patrick Cozzi Date: Thu, 7 Mar 2013 09:28:40 -0500 Subject: [PATCH] Added distance to Cartesian2 and Cartesian4 for consistency --- Source/Core/Cartesian2.js | 26 ++++++++++++++++++++++++++ Source/Core/Cartesian4.js | 26 ++++++++++++++++++++++++++ Specs/Core/Cartesian2Spec.js | 17 +++++++++++++++++ Specs/Core/Cartesian4Spec.js | 17 +++++++++++++++++ 4 files changed, 86 insertions(+) diff --git a/Source/Core/Cartesian2.js b/Source/Core/Cartesian2.js index 447d14f2497..b0c5376c73e 100644 --- a/Source/Core/Cartesian2.js +++ b/Source/Core/Cartesian2.js @@ -188,6 +188,32 @@ define([ return Math.sqrt(Cartesian2.magnitudeSquared(cartesian)); }; + var distanceScratch = new Cartesian2(); + + /** + * Computes the distance between two points + * @memberof Cartesian2 + * + * @param {Cartesian2} left The first point to compute the distance from. + * @param {Cartesian2} right The second point to compute the distance to. + * + * @return {Number} The distance between two points. + * + * @exception {DeveloperError} left and right are required. + * + * @example + * // Returns 1.0 + * var d = Cartesian2.distance(new Cartesian2(1.0, 0.0), new Cartesian2(2.0, 0.0)); + */ + Cartesian2.distance = function(left, right) { + if ((typeof left === 'undefined') || (typeof right === 'undefined')) { + throw new DeveloperError('left and right are required.'); + } + + Cartesian2.subtract(left, right, distanceScratch); + return Cartesian2.magnitude(distanceScratch); + }; + /** * Computes the normalized form of the supplied Cartesian. * @memberof Cartesian2 diff --git a/Source/Core/Cartesian4.js b/Source/Core/Cartesian4.js index 670c1640440..356b1870e38 100644 --- a/Source/Core/Cartesian4.js +++ b/Source/Core/Cartesian4.js @@ -179,6 +179,32 @@ define([ return Math.sqrt(Cartesian4.magnitudeSquared(cartesian)); }; + var distanceScratch = new Cartesian4(); + + /** + * Computes the 4-space distance between two points + * @memberof Cartesian4 + * + * @param {Cartesian4} left The first point to compute the distance from. + * @param {Cartesian4} right The second point to compute the distance to. + * + * @return {Number} The distance between two points. + * + * @exception {DeveloperError} left and right are required. + * + * @example + * // Returns 1.0 + * var d = Cartesian4.distance(new Cartesian4(1.0, 0.0, 0.0, 0.0), new Cartesian4(2.0, 0.0, 0.0, 0.0)); + */ + Cartesian4.distance = function(left, right) { + if ((typeof left === 'undefined') || (typeof right === 'undefined')) { + throw new DeveloperError('left and right are required.'); + } + + Cartesian4.subtract(left, right, distanceScratch); + return Cartesian4.magnitude(distanceScratch); + }; + /** * Computes the normalized form of the supplied Cartesian. * @memberof Cartesian4 diff --git a/Specs/Core/Cartesian2Spec.js b/Specs/Core/Cartesian2Spec.js index 4f46ddd8f41..111205170a5 100644 --- a/Specs/Core/Cartesian2Spec.js +++ b/Specs/Core/Cartesian2Spec.js @@ -107,6 +107,23 @@ defineSuite([ expect(cartesian.magnitude()).toEqual(Math.sqrt(13.0)); }); + it('distance', function() { + var distance = Cartesian2.distance(new Cartesian2(1.0, 0.0), new Cartesian2(2.0, 0.0)); + expect(distance).toEqual(1.0); + }); + + it('distance throws without left', function() { + expect(function() { + Cartesian2.distance(); + }).toThrow(); + }); + + it('distance throws without right', function() { + expect(function() { + Cartesian2.distance(Cartesian2.UNIT_X); + }).toThrow(); + }); + it('normalize works without a result parameter', function() { var cartesian = new Cartesian2(2.0, 0.0); var expectedResult = new Cartesian2(1.0, 0.0); diff --git a/Specs/Core/Cartesian4Spec.js b/Specs/Core/Cartesian4Spec.js index cc3c21ff302..1055ad93a0c 100644 --- a/Specs/Core/Cartesian4Spec.js +++ b/Specs/Core/Cartesian4Spec.js @@ -125,6 +125,23 @@ defineSuite([ expect(cartesian.magnitude()).toEqual(Math.sqrt(86.0)); }); + it('distance', function() { + var distance = Cartesian4.distance(new Cartesian4(1.0, 0.0, 0.0, 0.0), new Cartesian4(2.0, 0.0, 0.0, 0.0)); + expect(distance).toEqual(1.0); + }); + + it('distance throws without left', function() { + expect(function() { + Cartesian4.distance(); + }).toThrow(); + }); + + it('distance throws without right', function() { + expect(function() { + Cartesian4.distance(Cartesian4.UNIT_X); + }).toThrow(); + }); + it('normalize works without a result parameter', function() { var cartesian = new Cartesian4(2.0, 0.0, 0.0, 0.0); var expectedResult = new Cartesian4(1.0, 0.0, 0.0, 0.0);