# AnalyticalGraphicsInc/cesium

Added distance to Cartesian2 and Cartesian4 for consistency

1 parent 65d3639 commit 93f3631f7f329354be0bde47bb8998b7d540ca30 pjcozzi committed Mar 7, 2013
Showing with 86 additions and 0 deletions.
1. +26 −0 Source/Core/Cartesian2.js
2. +26 −0 Source/Core/Cartesian4.js
3. +17 −0 Specs/Core/Cartesian2Spec.js
4. +17 −0 Specs/Core/Cartesian4Spec.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
 @@ -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
 @@ -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);
 @@ -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);