distance.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. import { isBigNumber } from '../../utils/is.js';
  2. import { factory } from '../../utils/factory.js';
  3. var name = 'distance';
  4. var dependencies = ['typed', 'addScalar', 'subtract', 'divideScalar', 'multiplyScalar', 'unaryMinus', 'sqrt', 'abs'];
  5. export var createDistance = /* #__PURE__ */factory(name, dependencies, _ref => {
  6. var {
  7. typed,
  8. addScalar,
  9. subtract,
  10. multiplyScalar,
  11. divideScalar,
  12. unaryMinus,
  13. sqrt,
  14. abs
  15. } = _ref;
  16. /**
  17. * Calculates:
  18. * The eucledian distance between two points in N-dimensional spaces.
  19. * Distance between point and a line in 2 and 3 dimensional spaces.
  20. * Pairwise distance between a set of 2D or 3D points
  21. * NOTE:
  22. * When substituting coefficients of a line(a, b and c), use ax + by + c = 0 instead of ax + by = c
  23. * For parametric equation of a 3D line, x0, y0, z0, a, b, c are from: (x−x0, y−y0, z−z0) = t(a, b, c)
  24. *
  25. * Syntax:
  26. * math.distance([x1, y1], [x2, y2])
  27. *- math.distance({pointOneX: 4, pointOneY: 5}, {pointTwoX: 2, pointTwoY: 7})
  28. * math.distance([x1, y1, z1], [x2, y2, z2])
  29. * math.distance({pointOneX: 4, pointOneY: 5, pointOneZ: 8}, {pointTwoX: 2, pointTwoY: 7, pointTwoZ: 9})
  30. * math.distance([x1, y1, ... , N1], [x2, y2, ... , N2])
  31. * math.distance([[A], [B], [C]...])
  32. * math.distance([x1, y1], [LinePtX1, LinePtY1], [LinePtX2, LinePtY2])
  33. * math.distance({pointX: 1, pointY: 4}, {lineOnePtX: 6, lineOnePtY: 3}, {lineTwoPtX: 2, lineTwoPtY: 8})
  34. * math.distance([x1, y1, z1], [LinePtX1, LinePtY1, LinePtZ1], [LinePtX2, LinePtY2, LinePtZ2])
  35. * math.distance({pointX: 1, pointY: 4, pointZ: 7}, {lineOnePtX: 6, lineOnePtY: 3, lineOnePtZ: 4}, {lineTwoPtX: 2, lineTwoPtY: 8, lineTwoPtZ: 5})
  36. * math.distance([x1, y1], [xCoeffLine, yCoeffLine, constant])
  37. * math.distance({pointX: 10, pointY: 10}, {xCoeffLine: 8, yCoeffLine: 1, constant: 3})
  38. * math.distance([x1, y1, z1], [x0, y0, z0, a-tCoeff, b-tCoeff, c-tCoeff]) point and parametric equation of 3D line
  39. * math.distance([x, y, z], [x0, y0, z0, a, b, c])
  40. * math.distance({pointX: 2, pointY: 5, pointZ: 9}, {x0: 4, y0: 6, z0: 3, a: 4, b: 2, c: 0})
  41. *
  42. * Examples:
  43. * math.distance([0,0], [4,4]) // Returns 5.656854249492381
  44. * math.distance(
  45. * {pointOneX: 0, pointOneY: 0},
  46. * {pointTwoX: 10, pointTwoY: 10}) // Returns 14.142135623730951
  47. * math.distance([1, 0, 1], [4, -2, 2]) // Returns 3.7416573867739413
  48. * math.distance(
  49. * {pointOneX: 4, pointOneY: 5, pointOneZ: 8},
  50. * {pointTwoX: 2, pointTwoY: 7, pointTwoZ: 9}) // Returns 3
  51. * math.distance([1, 0, 1, 0], [0, -1, 0, -1]) // Returns 2
  52. * math.distance([[1, 2], [1, 2], [1, 3]]) // Returns [0, 1, 1]
  53. * math.distance([[1,2,4], [1,2,6], [8,1,3]]) // Returns [2, 7.14142842854285, 7.681145747868608]
  54. * math.distance([10, 10], [8, 1, 3]) // Returns 11.535230316796387
  55. * math.distance([10, 10], [2, 3], [-8, 0]) // Returns 8.759953130362847
  56. * math.distance(
  57. * {pointX: 1, pointY: 4},
  58. * {lineOnePtX: 6, lineOnePtY: 3},
  59. * {lineTwoPtX: 2, lineTwoPtY: 8}) // Returns 2.720549372624744
  60. * math.distance([2, 3, 1], [1, 1, 2, 5, 0, 1]) // Returns 2.3204774044612857
  61. * math.distance(
  62. * {pointX: 2, pointY: 3, pointZ: 1},
  63. * {x0: 1, y0: 1, z0: 2, a: 5, b: 0, c: 1} // Returns 2.3204774044612857
  64. *
  65. * @param {Array | Matrix | Object} x Co-ordinates of first point
  66. * @param {Array | Matrix | Object} y Co-ordinates of second point
  67. * @return {Number | BigNumber} Returns the distance from two/three points
  68. */
  69. return typed(name, {
  70. 'Array, Array, Array': function ArrayArrayArray(x, y, z) {
  71. // Point to Line 2D (x=Point, y=LinePoint1, z=LinePoint2)
  72. if (x.length === 2 && y.length === 2 && z.length === 2) {
  73. if (!_2d(x)) {
  74. throw new TypeError('Array with 2 numbers or BigNumbers expected for first argument');
  75. }
  76. if (!_2d(y)) {
  77. throw new TypeError('Array with 2 numbers or BigNumbers expected for second argument');
  78. }
  79. if (!_2d(z)) {
  80. throw new TypeError('Array with 2 numbers or BigNumbers expected for third argument');
  81. }
  82. var m = divideScalar(subtract(z[1], z[0]), subtract(y[1], y[0]));
  83. var xCoeff = multiplyScalar(multiplyScalar(m, m), y[0]);
  84. var yCoeff = unaryMinus(multiplyScalar(m, y[0]));
  85. var constant = x[1];
  86. return _distancePointLine2D(x[0], x[1], xCoeff, yCoeff, constant);
  87. } else {
  88. throw new TypeError('Invalid Arguments: Try again');
  89. }
  90. },
  91. 'Object, Object, Object': function ObjectObjectObject(x, y, z) {
  92. if (Object.keys(x).length === 2 && Object.keys(y).length === 2 && Object.keys(z).length === 2) {
  93. if (!_2d(x)) {
  94. throw new TypeError('Values of pointX and pointY should be numbers or BigNumbers');
  95. }
  96. if (!_2d(y)) {
  97. throw new TypeError('Values of lineOnePtX and lineOnePtY should be numbers or BigNumbers');
  98. }
  99. if (!_2d(z)) {
  100. throw new TypeError('Values of lineTwoPtX and lineTwoPtY should be numbers or BigNumbers');
  101. }
  102. if ('pointX' in x && 'pointY' in x && 'lineOnePtX' in y && 'lineOnePtY' in y && 'lineTwoPtX' in z && 'lineTwoPtY' in z) {
  103. var m = divideScalar(subtract(z.lineTwoPtY, z.lineTwoPtX), subtract(y.lineOnePtY, y.lineOnePtX));
  104. var xCoeff = multiplyScalar(multiplyScalar(m, m), y.lineOnePtX);
  105. var yCoeff = unaryMinus(multiplyScalar(m, y.lineOnePtX));
  106. var constant = x.pointX;
  107. return _distancePointLine2D(x.pointX, x.pointY, xCoeff, yCoeff, constant);
  108. } else {
  109. throw new TypeError('Key names do not match');
  110. }
  111. } else {
  112. throw new TypeError('Invalid Arguments: Try again');
  113. }
  114. },
  115. 'Array, Array': function ArrayArray(x, y) {
  116. // Point to Line 2D (x=[pointX, pointY], y=[x-coeff, y-coeff, const])
  117. if (x.length === 2 && y.length === 3) {
  118. if (!_2d(x)) {
  119. throw new TypeError('Array with 2 numbers or BigNumbers expected for first argument');
  120. }
  121. if (!_3d(y)) {
  122. throw new TypeError('Array with 3 numbers or BigNumbers expected for second argument');
  123. }
  124. return _distancePointLine2D(x[0], x[1], y[0], y[1], y[2]);
  125. } else if (x.length === 3 && y.length === 6) {
  126. // Point to Line 3D
  127. if (!_3d(x)) {
  128. throw new TypeError('Array with 3 numbers or BigNumbers expected for first argument');
  129. }
  130. if (!_parametricLine(y)) {
  131. throw new TypeError('Array with 6 numbers or BigNumbers expected for second argument');
  132. }
  133. return _distancePointLine3D(x[0], x[1], x[2], y[0], y[1], y[2], y[3], y[4], y[5]);
  134. } else if (x.length === y.length && x.length > 0) {
  135. // Point to Point N-dimensions
  136. if (!_containsOnlyNumbers(x)) {
  137. throw new TypeError('All values of an array should be numbers or BigNumbers');
  138. }
  139. if (!_containsOnlyNumbers(y)) {
  140. throw new TypeError('All values of an array should be numbers or BigNumbers');
  141. }
  142. return _euclideanDistance(x, y);
  143. } else {
  144. throw new TypeError('Invalid Arguments: Try again');
  145. }
  146. },
  147. 'Object, Object': function ObjectObject(x, y) {
  148. if (Object.keys(x).length === 2 && Object.keys(y).length === 3) {
  149. if (!_2d(x)) {
  150. throw new TypeError('Values of pointX and pointY should be numbers or BigNumbers');
  151. }
  152. if (!_3d(y)) {
  153. throw new TypeError('Values of xCoeffLine, yCoeffLine and constant should be numbers or BigNumbers');
  154. }
  155. if ('pointX' in x && 'pointY' in x && 'xCoeffLine' in y && 'yCoeffLine' in y && 'constant' in y) {
  156. return _distancePointLine2D(x.pointX, x.pointY, y.xCoeffLine, y.yCoeffLine, y.constant);
  157. } else {
  158. throw new TypeError('Key names do not match');
  159. }
  160. } else if (Object.keys(x).length === 3 && Object.keys(y).length === 6) {
  161. // Point to Line 3D
  162. if (!_3d(x)) {
  163. throw new TypeError('Values of pointX, pointY and pointZ should be numbers or BigNumbers');
  164. }
  165. if (!_parametricLine(y)) {
  166. throw new TypeError('Values of x0, y0, z0, a, b and c should be numbers or BigNumbers');
  167. }
  168. if ('pointX' in x && 'pointY' in x && 'x0' in y && 'y0' in y && 'z0' in y && 'a' in y && 'b' in y && 'c' in y) {
  169. return _distancePointLine3D(x.pointX, x.pointY, x.pointZ, y.x0, y.y0, y.z0, y.a, y.b, y.c);
  170. } else {
  171. throw new TypeError('Key names do not match');
  172. }
  173. } else if (Object.keys(x).length === 2 && Object.keys(y).length === 2) {
  174. // Point to Point 2D
  175. if (!_2d(x)) {
  176. throw new TypeError('Values of pointOneX and pointOneY should be numbers or BigNumbers');
  177. }
  178. if (!_2d(y)) {
  179. throw new TypeError('Values of pointTwoX and pointTwoY should be numbers or BigNumbers');
  180. }
  181. if ('pointOneX' in x && 'pointOneY' in x && 'pointTwoX' in y && 'pointTwoY' in y) {
  182. return _euclideanDistance([x.pointOneX, x.pointOneY], [y.pointTwoX, y.pointTwoY]);
  183. } else {
  184. throw new TypeError('Key names do not match');
  185. }
  186. } else if (Object.keys(x).length === 3 && Object.keys(y).length === 3) {
  187. // Point to Point 3D
  188. if (!_3d(x)) {
  189. throw new TypeError('Values of pointOneX, pointOneY and pointOneZ should be numbers or BigNumbers');
  190. }
  191. if (!_3d(y)) {
  192. throw new TypeError('Values of pointTwoX, pointTwoY and pointTwoZ should be numbers or BigNumbers');
  193. }
  194. if ('pointOneX' in x && 'pointOneY' in x && 'pointOneZ' in x && 'pointTwoX' in y && 'pointTwoY' in y && 'pointTwoZ' in y) {
  195. return _euclideanDistance([x.pointOneX, x.pointOneY, x.pointOneZ], [y.pointTwoX, y.pointTwoY, y.pointTwoZ]);
  196. } else {
  197. throw new TypeError('Key names do not match');
  198. }
  199. } else {
  200. throw new TypeError('Invalid Arguments: Try again');
  201. }
  202. },
  203. Array: function Array(arr) {
  204. if (!_pairwise(arr)) {
  205. throw new TypeError('Incorrect array format entered for pairwise distance calculation');
  206. }
  207. return _distancePairwise(arr);
  208. }
  209. });
  210. function _isNumber(a) {
  211. // distance supports numbers and bignumbers
  212. return typeof a === 'number' || isBigNumber(a);
  213. }
  214. function _2d(a) {
  215. // checks if the number of arguments are correct in count and are valid (should be numbers)
  216. if (a.constructor !== Array) {
  217. a = _objectToArray(a);
  218. }
  219. return _isNumber(a[0]) && _isNumber(a[1]);
  220. }
  221. function _3d(a) {
  222. // checks if the number of arguments are correct in count and are valid (should be numbers)
  223. if (a.constructor !== Array) {
  224. a = _objectToArray(a);
  225. }
  226. return _isNumber(a[0]) && _isNumber(a[1]) && _isNumber(a[2]);
  227. }
  228. function _containsOnlyNumbers(a) {
  229. // checks if the number of arguments are correct in count and are valid (should be numbers)
  230. if (!Array.isArray(a)) {
  231. a = _objectToArray(a);
  232. }
  233. return a.every(_isNumber);
  234. }
  235. function _parametricLine(a) {
  236. if (a.constructor !== Array) {
  237. a = _objectToArray(a);
  238. }
  239. return _isNumber(a[0]) && _isNumber(a[1]) && _isNumber(a[2]) && _isNumber(a[3]) && _isNumber(a[4]) && _isNumber(a[5]);
  240. }
  241. function _objectToArray(o) {
  242. var keys = Object.keys(o);
  243. var a = [];
  244. for (var i = 0; i < keys.length; i++) {
  245. a.push(o[keys[i]]);
  246. }
  247. return a;
  248. }
  249. function _pairwise(a) {
  250. // checks for valid arguments passed to _distancePairwise(Array)
  251. if (a[0].length === 2 && _isNumber(a[0][0]) && _isNumber(a[0][1])) {
  252. if (a.some(aI => aI.length !== 2 || !_isNumber(aI[0]) || !_isNumber(aI[1]))) {
  253. return false;
  254. }
  255. } else if (a[0].length === 3 && _isNumber(a[0][0]) && _isNumber(a[0][1]) && _isNumber(a[0][2])) {
  256. if (a.some(aI => aI.length !== 3 || !_isNumber(aI[0]) || !_isNumber(aI[1]) || !_isNumber(aI[2]))) {
  257. return false;
  258. }
  259. } else {
  260. return false;
  261. }
  262. return true;
  263. }
  264. function _distancePointLine2D(x, y, a, b, c) {
  265. var num = abs(addScalar(addScalar(multiplyScalar(a, x), multiplyScalar(b, y)), c));
  266. var den = sqrt(addScalar(multiplyScalar(a, a), multiplyScalar(b, b)));
  267. return divideScalar(num, den);
  268. }
  269. function _distancePointLine3D(x, y, z, x0, y0, z0, a, b, c) {
  270. var num = [subtract(multiplyScalar(subtract(y0, y), c), multiplyScalar(subtract(z0, z), b)), subtract(multiplyScalar(subtract(z0, z), a), multiplyScalar(subtract(x0, x), c)), subtract(multiplyScalar(subtract(x0, x), b), multiplyScalar(subtract(y0, y), a))];
  271. num = sqrt(addScalar(addScalar(multiplyScalar(num[0], num[0]), multiplyScalar(num[1], num[1])), multiplyScalar(num[2], num[2])));
  272. var den = sqrt(addScalar(addScalar(multiplyScalar(a, a), multiplyScalar(b, b)), multiplyScalar(c, c)));
  273. return divideScalar(num, den);
  274. }
  275. function _euclideanDistance(x, y) {
  276. var vectorSize = x.length;
  277. var result = 0;
  278. var diff = 0;
  279. for (var i = 0; i < vectorSize; i++) {
  280. diff = subtract(x[i], y[i]);
  281. result = addScalar(multiplyScalar(diff, diff), result);
  282. }
  283. return sqrt(result);
  284. }
  285. function _distancePairwise(a) {
  286. var result = [];
  287. var pointA = [];
  288. var pointB = [];
  289. for (var i = 0; i < a.length - 1; i++) {
  290. for (var j = i + 1; j < a.length; j++) {
  291. if (a[0].length === 2) {
  292. pointA = [a[i][0], a[i][1]];
  293. pointB = [a[j][0], a[j][1]];
  294. } else if (a[0].length === 3) {
  295. pointA = [a[i][0], a[i][1], a[i][2]];
  296. pointB = [a[j][0], a[j][1], a[j][2]];
  297. }
  298. result.push(_euclideanDistance(pointA, pointB));
  299. }
  300. }
  301. return result;
  302. }
  303. });