You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何计算两点(x1,y1)与(x2,y2)的距离并赋值给pointsDistance?

两点间距离计算实现
  • 需求:计算点(x1, y1)与点(x2, y2)之间的距离,将结果赋值给变量pointsDistance
  • 示例:若两点为(1.0, 2.0)和(1.0, 5.0),则pointsDistance的值为3.0

修正错误公式

原提供的计算公式存在逻辑错误:

pointsDistance = sqrt ((x2-x1) * 2.0 +(y2-y1) * 2.0);

错误点:两点距离的正确计算应为坐标差值的平方和开根号,而非坐标差值乘以2。

正确实现代码

方式一(使用幂函数):

pointsDistance = sqrt(pow(x2 - x1, 2.0) + pow(y2 - y1, 2.0));

方式二(直接计算平方):

pointsDistance = sqrt((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1));

内容的提问来源于stack exchange,提问作者Cruz Carnero

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 15:29:53