向量点积-角度计算

  getAngle(pointA, pointB, pointC) {
    //  pointC = {
    //    x: pointA.x + 100,
    //    y: pointA.y
    //  }
    //向量
    const BA = {
      x: pointB.x - pointA.x,
      y: pointB.y - pointA.y
    }
    const CA = {
      x: pointC.x - pointA.x,
      y: pointC.y - pointA.y
    }
    // 计算叉积
    const crossProduct = BA.x * CA.y - BA.y * CA.x;
    let direction;
    if(crossProduct > 0){
      direction = -1; //逆时针 
    }else if(crossProduct < 0){
      direction = 1;  //顺时针
    }else{
      direction = 0;  //同线
    }
    // 计算向量点积/模长
    const dot = BA.x * CA.x + BA.y * CA.y;
    const mat1 = Math.sqrt(BA.x * BA.x + BA.y * BA.y);
    const mat2 = Math.sqrt(CA.x * CA.x + CA.y * CA.y);

    // 计算角度(以弧度为单位)  
    const angle = Math.acos(dot / (mat1 * mat2));

    // 将弧度转换为角度(以度为单位)  
    const Angle = angle * (180 / Math.PI);

    return Angle.toFixed(); //取整字符串
  }

direction是存在角度顺逆时针需要考虑的,比如顺时针转90度其实是逆时针270度,具体依据使用情况而定。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值