碎片时间学编程「290]:计算两个向量之间的距离
来源:哔哩哔哩     时间:2023-03-23 12:08:57


(资料图片)

计算两个向量之间的距离。

使用Array.prototype.reduce(),Math.pow()和Math.sqrt()方法计算两个向量之间的距离。

JavaScript

const vectorDistance = (x, y) =>  Math.sqrt(x.reduce((acc, val, i) => acc + Math.pow(val - y[i], 2), 0));

示例:

vectorDistance([10, 0, 5], [20, 0, 10]); // 11.180339887498949

更多内容请访问我的网站:https://www.icoderoad.com

标签:

广告

X 关闭

广告

X 关闭