首頁  >  文章  >  後端開發  >  如何判斷三個座標是否共線

如何判斷三個座標是否共線

PHPz
PHPz轉載
2024-02-06 08:12:03396瀏覽

如何判斷三個座標是否共線

問題內容

我確信我的問題是一個非常通用的問題,可能是一個純java問題。然而,我一直在嘗試找到一種方法來識別三個座標是否共線,使用相同的邏輯發現它似乎不適用於以「點」作為輸入的範例。 可以採用兩種方法 1. 求構成三個座標/點的三角形的面積。如果它們在同一條線上;面積值必須為零。 2. 將連接這些座標的線分成兩部分,並找出各自的斜率。如果它們在同一條線上,則斜率將相同。

以下是我正在嘗試的方法。

private
boolean collinearCheck( Coordinate endPointOne , Coordinate intersection,Coordinate  endPointTwo ){ 
boolean isCollenear = false; 

//Area of triangle approach
double area = (Math.round(endPointOne.x )* (Math.round(intersection.y) - Math.round  (endPointTwo.y)) + Math.round(intersection.x )* (Math.round(endPointTwo.y) - Math.round (endPointOne.y)) + 
Math.round(endPointTwo.x) * (Math.round(endPointOne.y) - Math.round(intersection.y)));
if((endPointOne.x * (intersection.y - endPointTwo.y) + intersection.x *  (endPointTwo.y - endPointOne.y) + 
endPointTwo.x * (endPointOne.y - intersection.y))<= 0) if(Math.round(area) <= 0)
{
isCollenear = true;
} 

 // Slope Approach
  double numeratorOne = Math.round(intersection.y) - Math.round(endPointOne.y);
  double denominatorOne = Math.round(intersection.x) - Math.round(endPointOne.x);
  double numeratorTwo = Math.round(endPointTwo.y) - Math.round(intersection.y);
  double denominatorTwo = Math.round(endPointTwo.x) - Math.round(intersection.x);
  double result1 = Math.round(numeratorOne/denominatorOne);
  double result2 = Math.round(numeratorTwo/denominatorTwo);
  if(result1== 0 && result2==0){
   isCollenear = true;
  }
 return isCollenear; 
  }

在這兩種情況下,同時使用座標作為輸入;即使對於相似共線的情況,我最終也會得到該區域的值,例如 4 等。對於明顯不共線的情況;我最終得到相同的斜率值。

有沒有辦法可以使用任何構造來獲得共線性的明確通知器?我的做法正確嗎? 我傳遞給該方法的座標樣本值是 Coefficient endPointOne = -26.6666666666666686, 32.38095238095238 .... 等等

期待您的意見。

感謝和問候


正確答案


我不是檢查區號,而是檢查三點是否共線。那麼公式就是:

點 (x1,y1)、(x2,y2)、(x3,y3)。

它應該是共線的,當且僅當,

(y2-y1)      (y3-y2)
 -------  =   -------
 (x2-x1)      (x3-x2)

所以程式碼應該是,

if(result1==result2){
      isCollenear = true;
  }

以上是如何判斷三個座標是否共線的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:stackoverflow.com。如有侵權,請聯絡admin@php.cn刪除