How to determine whether V3 is between V1 and V2 when we go from V1 to V2 counterclockwise?

后端 未结 5 2117
鱼传尺愫
鱼传尺愫 2021-01-12 15:37

I have three vectors V1, V2, and V3. Their origin points are on the axes\' origin. How could I determine whether V3 is between V1 and V2 when I move around counterclockwise

5条回答
  •  忘掉有多难
    2021-01-12 16:18

    To test this condition you have to calculate the winding of two triangles:

    1. The triangle formed by V1, the origin and V3. This triangle must be counter-clockwise.

    2. The triangle formed by V3, the origin and V2. This triangle must be counter-clockwise as well.

    To test the winding of a triangle it's enough to check the sign of the 2D cross-product of the vertices.

    The test looks like this (sorry - C-code):

    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");
      }
    }
    

提交回复
热议问题