Repository: josdejong/mathjs
Pull request #449 (2015-08-30 17:06:28)

Adding distance to geometry
Adding source + test + docs + fixing index.


Patch:
diff --git a/docs/reference/functions/distance.md b/docs/reference/functions/distance.md
new file mode 100644
index 000000000..d22ce61de
--- /dev/null
+++ b/docs/reference/functions/distance.md
@@ -0,0 +1,36 @@
+# Function distance
+
+Calculates the distance between two 2-dimensional points. The inputs are in the form of arrays.
+
+## Syntax
+
+```js
+math.distance([x1, y1], [x2, y2]);
+```
+
+### Parameters
+
+Parameter | Type | Description
+--------- | ---- | -----------
+`a` | Array &#124; Matrix | Co-ordinates of first end-point
+`b` | Array &#124; Matrix | Co-ordinates of second end-point
+
+### Returns
+
+Type | Description
+---- | -----------
+Number &#124; BigNumber | Returns the distance between two points
+
+
+## Examples
+
+```js
+math.distance([0, 0], [10, 10]) // Returns 14.142135623730951
+math.distance(math.matrix([0,0]),math.matrix([10,10])) // Returns 14.142135623730951
+```
+
+## See also
+
+[intersect](intersect.md)
+
+<!-- Note: This file is automatically generated from source code comments. Changes made in this file will be overridden. -->
diff --git a/lib/function/geometry/distance.js b/lib/function/geometry/distance.js
new file mode 100644
index 000000000..dd10d0b6c
--- /dev/null
+++ b/lib/function/geometry/distance.js
@@ -0,0 +1,53 @@
+'use strict';
+
+var math = require('../../../index'),
+flatten = math.flatten;
+
+function factory (type, config, load, typed) {
+
+
+  /**
+   * Calculates the Euclidean distance between two points.
+   *
+   * Syntax:
+   *
+   *    math.distance([x1, y1], [x2, y2]);
+   *    math.distance([[x1, y1], [x2, y2]);
+   *
+   * Examples:
+   *
+   *    math.distance([0,0], [4,4]) //returns 5.6569
+   *    math.distance([[0,0], [4,4]]) //returns 5.6569
+   *
+   * @param  {Array | Matrix} x   Co-ordinates of first end-point of first line
+   * @param  {Array | Matrix} y   Co-ordinates of second end-point of first line
+   * @return {Number | BigNumber} Returns the distance from two points
+   */
+
+   var distance = typed('distance', {
+    'Array, Array': function (x, y) {
+      if (!_2d(x)) { throw new TypeError('Array with 2 numbers expected for first argument'); }
+      if (!_2d(y)) { throw new TypeError('Array with 2 numbers expected for second argument'); }
+
+      return _distance2d(x[0], x[1], y[0], y[1]);
+    }
+
+  });
+   return distance;
+ }
+
+ function _2d(x) {
+  return x.length === 2 && typeof x[0] === 'number' && typeof x[1] === 'number';
+}
+
+function _distance2d(x1, y1, x2, y2){
+  var yDiff = y2 - y1;
+  var xDiff = x2 - x1;
+  var radicant = yDiff * yDiff + xDiff * xDiff;
+  var result = Math.pow(radicant, 0.5);
+  return result;
+}
+
+
+exports.name = 'distance';
+exports.factory = factory;
diff --git a/lib/function/geometry/index.js b/lib/function/geometry/index.js
index 9f531e773..267024ba6 100644
--- a/lib/function/geometry/index.js
+++ b/lib/function/geometry/index.js
@@ -1,3 +1,4 @@
 module.exports = [
-  require('./intersect')
+  require('./intersect'),
+  require('./distance')
 ];


Test patch:
diff --git a/test/function/geometry/distance.test.js b/test/function/geometry/distance.test.js
new file mode 100644
index 000000000..b34afd7d0
--- /dev/null
+++ b/test/function/geometry/distance.test.js
@@ -0,0 +1,27 @@
+var assert = require('assert');
+var error = require('../../../lib/error/index');
+var math = require('../../../index');
+describe('distance', function() {
+  it('should calculate the distance of two 2D points', function() {
+    assert.equal(math.distance([0, 0], [10, 10]), 14.142135623730951);
+    assert.equal(math.distance(math.matrix([0,0]),math.matrix([10,10])), 14.142135623730951);
+  });
+
+  it('should calculate distance for non-zero values', function() {
+    assert.equal(math.distance([1, 1], [10,10]), 12.727922061357855);
+    assert.equal(math.distance([-1, -1], [10,10]), 15.556349186104045);
+    assert.equal(math.distance([-1, 8], [5,10]), 6.324555320336759);
+    assert.equal(math.distance([-100, 60], [0,500]), 451.22056690713913);
+    assert.equal(math.distance([-100.78, 60.04], [0.3,500.09]), 451.5098768576386);
+  });
+
+  it('should throw an error for incompatible parameter types', function() {
+    assert.throws(function() {math.distance(0.5)}, TypeError);
+    assert.throws(function() {math.distance('1')}, TypeError);
+  });
+
+  it('should throw an error for different number of parameters', function() {
+    assert.throws(function() {math.distance([0,0])}, TypeError);
+    assert.throws(function() {math.distance([0,0],[0,0],[0,0])}, TypeError);
+  });
+});
\ No newline at end of file