This repository has been archived on 2026-09-07. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
eco-hack/src/utils/calculateDistance.js
T

9 lines
450 B
JavaScript

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));
};