判断有序的3个点是顺时针还是逆时针 (Orientation of 3 ordered points)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zjucor/article/details/83010289

1. 从叉乘的含义,设3点为ABC,ABxAC:代表平行四边形的面积,为0说明3点共线,大于0说明顺时针,小于0说明逆时针

如果展开到3位,其实就是在看z的正负

ref:https://zhidao.baidu.com/question/102397466.html

2. 从2条线段的斜率着手

The idea is to use slope.  



Slope of line segment (p1, p2): σ = (y2 - y1)/(x2 - x1)
Slope of line segment (p2, p3): τ = (y3 - y2)/(x3 - x2)

If  σ < τ, the orientation is counterclockwise (left turn)
If  σ = τ, the orientation is collinear
If  σ > τ, the orientation is clockwise (right turn)

Using above values of σ and τ, we can conclude that, 
the orientation depends on sign of  below expression: 
(y2 - y1)*(x3 - x2) - (y3 - y2)*(x2 - x1)

Above expression is negative when σ < τ, i.e., counterclockwise
Above expression is 0 when σ = τ, i.e., collinear
Above expression is positive when σ > τ, i.e., clockwise

其结果与第一种方法计算方式相同

ref:https://www.geeksforgeeks.org/orientation-3-ordered-points/

猜你喜欢

转载自blog.csdn.net/zjucor/article/details/83010289