zoukankan      html  css  js  c++  java
  • 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( ) 的角度与此计算值是相反的 左转为负

  • 相关阅读:
    3、linux网络编程--网络字节序
    1、linux网络编程--网络协议
    第四篇:进程
    第五篇:进程通信
    第六篇:线程原理
    操作系统基本概念
    第一篇:基础原理篇
    第二篇:操作系统历史
    (2)linux下的简单的socket通信实例
    (3)基于linux的socket编程TCP半双工client-server聊天程序
  • 原文地址:https://www.cnblogs.com/qianyuesheng/p/13841190.html
Copyright © 2011-2022 走看看