Skip to content

Commit

Permalink
Added distance to Cartesian2 and Cartesian4 for consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
pjcozzi committed Mar 7, 2013
1 parent 65d3639 commit 93f3631
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Source/Core/Cartesian2.js
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions Source/Core/Cartesian4.js
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions Specs/Core/Cartesian2Spec.js
Expand Up @@ -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);
Expand Down
17 changes: 17 additions & 0 deletions Specs/Core/Cartesian4Spec.js
Expand Up @@ -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);
Expand Down

0 comments on commit 93f3631

Please sign in to comment.