2017-03-17 102 views
0

我一直在試圖弄清楚如何檢查一個點是否位於同一條直線上以及兩個其他點之間。它似乎工作,如果線是對角線,但如果它的垂直或水平直線失敗。確定一個點是否在一條線上的兩個其他點之間

這是我的方法:適用於所有情況

public bool isBetween(Vector3 C, Vector3 A, Vector3 B) 
{ 
    Vector3 sum = Vector3.Cross(A,B) + Vector3.Cross(A,C) + Vector3.Cross(B,C); 

    if (sum.x == 0 && sum.z == 0 && sum.y == 0) 
    { 
     Vector3 min = Vector3.Min(A, B); 
     Vector3 max = Vector3.Max(A, B); 

     // only checking 2 dimensions 
     if (C.x > min.x && C.x < max.x && C.z > min.z && C.z < max.z) 
     { 
      return true; 
     } 
    } 
    return false; 
} 

它適用於某些情況下,但不是100%。不知道如何解決它,讓它工作。

+0

它適用於哪些情況?什麼情況下不起作用? – gdc

回答

2

假設point1和point2不同,首先檢查點是否在線上。爲此,您只需要向量point1 - > currPoint和point1 - > point2的「交叉乘積」。

dxc = currPoint.x - point1.x; 
dyc = currPoint.y - point1.y; 

dxl = point2.x - point1.x; 
dyl = point2.y - point1.y; 

cross = dxc * dyl - dyc * dxl; 

你的觀點在於當且僅當交叉等於零時。

if (cross != 0) 
return false; 

現在,你知道點也騙就行了,現在是時候來檢查它是否位於原始點之間。這可以通過比較在x座標來容易地完成,如果線是「比垂直更水平的」,或以其他方式y座標

if (abs(dxl) >= abs(dyl)) 
return dxl > 0 ? 
    point1.x <= currPoint.x && currPoint.x <= point2.x : 
    point2.x <= currPoint.x && currPoint.x <= point1.x; 
else 
return dyl > 0 ? 
    point1.y <= currPoint.y && currPoint.y <= point2.y : 
    point2.y <= currPoint.y && currPoint.y <= point1.y; 

注意,上述算法,如果完全積分如果輸入數據是一體的,即它整數輸入不需要浮點計算。計算交叉時,請注意潛在的溢出。

P.S.這個算法絕對精確,這意味着它會拒絕非常接近線但不在線上的點。有時候這不是我們所需要的。但這是一個不同的故事。

+0

我會試一試你的例子。我最初在這裏嘗試了類似的東西:http://pastebin.com/xhCPaeSG但它失敗了。 – Sir

+0

注意:您可能想將答案移至http://stackoverflow.com/questions/7050186/find-if-point-lays-on-line-segment。 –

+0

這些答案中有一半不是C#或計算距離 - 我甚至使用該答案頁來闡述我在pastebin中寫的內容,但它甚至沒有工作。 – Sir

相關問題