寫代碼經常遇到判斷向量和點的位置關系,做一個簡單的記錄,方便后面使用。原理是向量的叉乘。
兩個同起點的向量A(xA ,yA)和B(xB ,yB)的叉乘公式為: crossV = xA *yB - yA*xB。
展開后即可得到代碼中公式,進而可以判斷點和向量的位置關系。
一、定義結構體
二、定義判斷函數
def judgeDirection(startPoint, endPoint, P): tmp = (startPoint.y - endPoint.y)*P.x + (endPoint.x - startPoint.x)*P.y + startPoint.x*endPoint.y - endPoint.x*startPoint.y if tmp < 0: print("the point at the right of vector!") else: print("the point at the left of vertor")
三、測試
-
情形一
-
情形二