2008/09/01

判斷三點間順時針或逆時針的關係


若有三點如上圖所示

要判斷 P2 在 P1 的哪個方向

可以先利用 V1.V2 = |V1| |V2| cosθ 和 arccos(cosθ) 求得角度

要判斷順時針與逆時針則先求得 vector_P0P1 與 vector_P0P2

Let

vector_P0P1 = (vx1, vy1) = (P1x - P0x, P1y - P0y)
vector_P0P2 = (vx2, vy2) = (P2x - P0x, P2y - P0y)



vector_P0P1 X vector_P0P2 > 0 為圖左方,逆時針(counter-clockwise)
vector_P0P1 X vector_P0P2 < 0 為圖右方,順時針(clockwise)

vector_P0P1 X vector_P0P2 = (vx1, vy1) X (vx2, vy2) =

| vx1 vx2 |
| vy1 vy2 | = vx1*vy2 - vx2*vy1 = |P1P0| |P2P0| sinθ

所以

vx1*vy2 - vx2*vy1 > 0 為圖左方,逆時針(counter-clockwise)
vx1*vy2 - vx2*vy1 < 0 為圖右方,順時針(clockwise)

參考資料:http://www.csie.nctu.edu.tw/~sctsai/adprog/notes/CompGeo.ppt

No comments:

Post a Comment