首页 > 后端开发 > Golang > 正文

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

PHPz
发布: 2024-02-06 08:12:03
转载
843人浏览过

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

问题内容

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

期待您的意见。

感谢和问候


正确答案


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

标小兔AI写标书
标小兔AI写标书

一款专业的标书AI代写平台,提供专业AI标书代写服务,安全、稳定、速度快,可满足各类招投标需求,标小兔,写标书,快如兔。

标小兔AI写标书 40
查看详情 标小兔AI写标书

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

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

(y2-y1)      (y3-y2)
 -------  =   -------
 (x2-x1)      (x3-x2)
登录后复制

所以代码应该是,

if(result1==result2){
      isCollenear = true;
  }
登录后复制

以上就是如何判断三个坐标是否共线的详细内容,更多请关注php中文网其它相关文章!

相关标签:
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:stackoverflow网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门推荐
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号