1. 程式人生 > >判斷有序的3個點是順時針還是逆時針 (Orientation of 3 ordered points)

判斷有序的3個點是順時針還是逆時針 (Orientation of 3 ordered points)

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/