代码之家  ›  专栏  ›  技术社区  ›  Kamran Bigdely

当我们逆时针从V1到V2时,如何确定V3是否在V1和V2之间?

  •  6
  • Kamran Bigdely  · 技术社区  · 17 年前

    我有三个向量V1、V2和V3。它们的原点位于轴的原点上。当我从V1逆时针移动到V2时,我如何确定V3是否在V1和V2之间?

    alt text http://www.freeimagehosting.net/uploads/1448ea8896.jpg

    获取它们的角度并评估这些条件(伪代码)是无法完成的:

    if angle(V3) > angle(V1) && angle(V3) < angle(V2) 
       printf("V3 is between V1 and V2") 
    else 
       printf("out of the interval")
    

    要看到它的缺陷,假设 angle 函数给出了[-pi-pi]范围内的角度。因此,如果角度(V1)=120(以度为单位),角度(V2)=-130,角度(V3)=150,那么答案(根据上述代码)是“超出区间”,尽管如果你从V1逆时针移动到V2,它就在它们之间。

    我用MATLAB编程。

    编辑1:它是2D的。

    5 回复  |  直到 13 年前
        1
  •  8
  •   gnovice    17 年前

    crossProds = [V1(1)*V2(2)-V1(2)*V2(1) ...
                  V1(1)*V3(2)-V1(2)*V3(1) ...
                  V3(1)*V2(2)-V3(2)*V2(1)];
    if (all(crossProds >= 0) || ...
        (crossProds(1) < 0) && ~all(crossProds(2:3) < 0)),
      disp("V3 is between V1 and V2");
    else
      disp("out of the interval");
    end
    

    解释:

    第1版 。如果以下两者之间的逆时针角度 第1版 第2版 第1版 第2版 也大于或等于零。这解释了第一个逻辑检查:

    all(crossProds >= 0)
    

    第1版 第2版 第1版 第2版 第1版 第2版

    (crossProds(1) < 0) && ~all(crossProds(2:3) < 0)
    

    short circuit operators

        2
  •  3
  •   Benoît photo_tom    17 年前

    a1 <= a2 < a1 + 2*pi
    a1 <= a3 < a1 + 2*pi
    

        3
  •  2
  •   Ken Ken    17 年前

        4
  •  0
  •   Ivan Bestvina    11 年前

    :

    R1 -= R3;
    R2 -= R3;
    
    if (R1 < 0) R1 += 2 * PI;
    if (R2 <= 0) R2 += 2 * PI;
    
    return (r1 < r2);
    

    逆时针

        5
  •  -2
  •   Nils Pipenbrinck    17 年前

    要测试三角形的缠绕,只需检查顶点的二维叉积的符号即可。

    int IsBetween (vector v1, vector v2, vector v3)
    {
      float winding1 = (v1.x * v3.y - v1.y * v3.x);
      float winding2 = (v3.x * v2.y - v3.y * v2.x);
    
      // this test could be exactly the wrong way around. This depends
      // on how you define your coordinate system (e.g. is Y going up or down?)
    
      if ((winding1 <0) && (winding2 < 0))
      {
        printf ("V3 is between them\n");
      }
      else
      {
        printf ("it's not\n");
      }
    }