From 328a0a4d699d23c0ce17f70b7decef6d7da2821a Mon Sep 17 00:00:00 2001 From: GRZ4NA Date: Fri, 1 Oct 2021 22:25:35 +0200 Subject: [PATCH] added distance calculation function --- src/utils/calculateDistance.js | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 src/utils/calculateDistance.js diff --git a/src/utils/calculateDistance.js b/src/utils/calculateDistance.js new file mode 100644 index 0000000..3f4811f --- /dev/null +++ b/src/utils/calculateDistance.js @@ -0,0 +1,8 @@ +export const calculateDistance = (posA, posB) => { + // Check input data + // Expected input value [number, number] + if (!Array.isArray(posA) || !Array.isArray(posB) || isNaN(posA[0]) || isNaN(posA[1]) || isNaN(posB[0]) || isNaN(posB[1])) throw new TypeError("Incorrect argument type"); + const distanceX = posA[0] - posB[0]; + const distanceY = posA[1] - posB[1]; + return Math.sqrt(Math.pow(distanceX, 2) + Math.pow(distanceY, 2)); +};