distance.js 13 KB

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