UNPKG

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