AI编程助手
AI免费问答

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

PHPz   2024-02-06 08:12   736浏览 转载

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

问题内容

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

<p>在这两种情况下,同时使用坐标作为输入;即使对于相似共线的情况,我最终也会得到该区域的值,例如 4 等。对于明显不共线的情况;我最终得到相同的斜率值。</p>

<p>有没有办法可以使用任何构造来获得共线性的明确通知器?我的做法正确吗?
我传递给该方法的坐标样本值是 Coefficient endPointOne = -26.666666666666686, 32.38095238095238 .... 等等</p>

<p>期待您的意见。</p>

<p>感谢和问候</p><br><h2 class="daan">正确答案</h2><br><p>我不是检查区号,而是检查三点是否共线。那么公式就是:</p>

<p>点 (x1,y1)、(x2,y2)、(x3,y3)。</p>

<p>它应该是共线的,当且仅当,</p>

<pre class="brush:php;toolbar:false;">(y2-y1)      (y3-y2)
 -------  =   -------
 (x2-x1)      (x3-x2)

所以代码应该是,

if(result1==result2){
      isCollenear = true;
  }
声明:本文转载于:stackoverflow,如有侵犯,请联系admin@php.cn删除