gpt4 book ai didi

iphone - 计算两个CGPoint之间的距离

转载 作者:行者123 更新时间:2023-12-03 18:16:30 49 4
gpt4 key购买 nike

我需要计算两个CGPoint之间的距离。我提到了thisthis ,但我不明白。

最佳答案

好吧,你也提到了完整的代码在哪里:

CGPoint p2; //[1]
CGPoint p1;
//Assign the coord of p2 and p1...
//End Assign...
CGFloat xDist = (p2.x - p1.x); //[2]
CGFloat yDist = (p2.y - p1.y); //[3]
CGFloat distance = sqrt((xDist * xDist) + (yDist * yDist)); //[4]

距离是可变距离。

这里发生了什么:

  1. 首先我们提出两点......
  2. 然后我们找到点的 x 坐标之间的距离。
  3. 现在我们找到 y 坐标之间的距离。
  4. 这些长度是三角形的两条边,实际上它们是边,是时候找到斜边了,这意味着在进行一些数学重新排列 c^2 = a^2 + b^2 之后,我们得到斜边等于 sqrt( (xDist^2) + (yDist^2))。 xDist^2 = (xDist * xDist)。同样: yDist^2 = (yDist * yDist)

你不能真正让 CGPoint 成为距离,距离没有 x 和 y 分量。这只是 1 个数字。

如果您认为 CGPoint 是一种测量单位(例如英尺是一种测量单位),那么事实并非如此。

关于iphone - 计算两个CGPoint之间的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6416101/

49 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com