From 83ecc77e1a9e46924de5625d41d6ee778d7d5618 Mon Sep 17 00:00:00 2001 From: Diya Date: Wed, 8 Nov 2023 16:00:36 +0530 Subject: [PATCH] removes hypot, changes function calls --- src/math/calculation.js | 48 +++-------------------------------------- 1 file changed, 3 insertions(+), 45 deletions(-) diff --git a/src/math/calculation.js b/src/math/calculation.js index b086ea11d5..efe6b6733e 100644 --- a/src/math/calculation.js +++ b/src/math/calculation.js @@ -174,10 +174,10 @@ p5.prototype.dist = function(...args) { p5._validateParameters('dist', args); if (args.length === 4) { //2D - return hypot(args[2] - args[0], args[3] - args[1]); + return Math.hypot(args[2] - args[0], args[3] - args[1]); } else if (args.length === 6) { //3D - return hypot(args[3] - args[0], args[4] - args[1], args[5] - args[2]); + return Math.hypot(args[3] - args[0], args[4] - args[1], args[5] - args[2]); } }; @@ -341,7 +341,7 @@ p5.prototype.log = Math.log; */ p5.prototype.mag = function(x, y) { p5._validateParameters('mag', arguments); - return hypot(x, y); + return Math.hypot(x, y); }; /** @@ -727,48 +727,6 @@ p5.prototype.sq = n => n * n; */ p5.prototype.sqrt = Math.sqrt; -// Calculate the length of the hypotenuse of a right triangle -// This won't under- or overflow in intermediate steps -// https://en.wikipedia.org/wiki/Hypot -function hypot(x, y, z) { - // Use the native implementation if it's available - if (typeof Math.hypot === 'function') { - return Math.hypot.apply(null, arguments); - } - - // Otherwise use the V8 implementation - // https://github.com/v8/v8/blob/8cd3cf297287e581a49e487067f5cbd991b27123/src/js/math.js#L217 - const length = arguments.length; - const args = []; - let max = 0; - for (let i = 0; i < length; i++) { - let n = arguments[i]; - n = +n; - if (n === Infinity || n === -Infinity) { - return Infinity; - } - n = Math.abs(n); - if (n > max) { - max = n; - } - args[i] = n; - } - - if (max === 0) { - max = 1; - } - let sum = 0; - let compensation = 0; - for (let j = 0; j < length; j++) { - const m = args[j] / max; - const summand = m * m - compensation; - const preliminary = sum + summand; - compensation = preliminary - sum - summand; - sum = preliminary; - } - return Math.sqrt(sum) * max; -} - /** * Calculates the fractional part of a number. For example, * `fract(12.34)` returns 0.34.