计算与单位向量的夹角-JavaScript

多维数据分类处理,计算夹角

//计算几何误差修正,与单位向量[1,1,…,1]的夹角
const EPS=0.00000001;
const vectorAngle = (x) => {
  let mX = Math.sqrt(x.reduce((acc, n) => acc + Math.pow(n, 2), 0));
  let mY = Math.sqrt(x.length);
  const d1=x.reduce((acc, n) => acc + n , 0)
  const d2=(mX * mY)
  if(Math.abs(d1-d2)<EPS){
      return 0
  }
  return Math.acos(d1/d2)*180/Math.PI;
};

 

console.log(vectorAngle([2,1,1]))
posted @ 2020-07-28 11:48  巅峰蜗牛  阅读(684)  评论(0编辑  收藏  举报