卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章34086本站已运行391

如何判断三个坐标是否共线

如何判断三个坐标是否共线

问题内容

我确信我的问题是一个非常通用的问题,可能是一个纯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.666666666666686, 32.38095238095238 .... 等等

期待您的意见。

感谢和问候


正确答案


我不是检查区号,而是检查三点是否共线。那么公式就是:

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

它应该是共线的,当且仅当,

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

所以代码应该是,

if(result1==result2){
      isCollenear = true;
  }
卓越飞翔博客
上一篇: Go 的“类型断言”方式背后的原因是什么?
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏