Processing平台之PVector求角度

问题:在processing 平台,通过给定三个PVector向量,如何求他们之间的夹角,同时确定是在左侧还是右侧?

如图所示,在processing 平台中,PVector表示点的坐标是以原点为起点的向量:

假定有四个点向量PVector,即为:edgeOne, edgeTwo, inter, pre
三个方向向量(通过向量之间的坐标加减得到), 即为:aimOne, aimTwo, intpre 则: 向量aimOne 和 intpre 之间的夹角和方向是多少? 向量aimTwo 和 intpre 之间的夹角和方向是多少?

方法:直接调用以下函数即可:

  float getAngle(PVector pre1, PVector middle, PVector next) {
    // calc PVector
    PVector pre = new PVector(pre1.x, pre1.y);
    PVector inter = new PVector(middle.x, middle.y);

    PVector edgeOne = new PVector(next.x, next.y);
    PVector aimOne = edgeOne.sub(inter);
    PVector intpre = inter.sub(pre);  // change inter

    float deviationAngle = atan2(intpre.y, intpre.x)  - atan2(aimOne.y, aimOne.x);
    if ( deviationAngle > PI ) {
      deviationAngle -= TWO_PI;
    } else if (deviationAngle < -PI) {
      deviationAngle += TWO_PI;
    }

    return degrees(deviationAngle);
  }

getAngle(pre, inter, edgeOne);
getAngle(pre, inter, edgeTwo);

说明: 

此函数将角度值转换为[-180, 180],  右侧为负,左侧为正,同向为0

PS:但是,PVector.rotate( ) 的角度与此计算值是相反的 左转为负

原文地址:https://www.cnblogs.com/qianyuesheng/p/13841190.html