Then we find the distance between x coordinates of the points.
Now we find the distance between the y coordinates.
These lengths are two sides of the triangle, infact they are the legs, time to find the hypotenuse which means after doing some math to rearragne c^2 = a^2 + b^2 we get the hypotenuse to equal sqrt((xDist^2) + (yDist^2)). xDist^2 = (xDist * xDist). And likewise: yDist^2 = (yDist * yDist)
You can't really make a CGPoint be the distance, distance doesn't have an x and y component. It is just 1 number.
If you think CGPoint is a unit of measurement (for example feet is a unit of measurement) it is not.
CGPoint p1, p2; // Having two points
CGFloat distance = hypotf((p1.x-p2.x), (p1.y-p2.y));
Longer Explanation
If you have two points p1 and p2 it is obviously easy to find the difference between their height and width (e.g. ABS(p1.x - p2.x)) but to find a true representation of their distance you really want the hypothenuse (H below).
p1
|\
| \
| \ H
| \
| \
|_ _ _\
p2
Thankfully there is a built in macro for this: hypotf (or hypot for doubles):
// Returns the hypothenuse (the distance between p1 & p2)
CGFloat dist = hypotf((p1.x-p2.x), (p1.y-p2.y));
I've had to do this by hand 10,000 times so I wrote a function for it and stuck it in my personal library that I always dump in at the beginning of a new program so I forget it's not cannon.
Set "isItATap" true in touchesBegan, put the above in touchesMoved, then you know the player moved their finger too far for the touch to be a "tap", so you can have it NOT select the object the player touched and instead scroll the object around.
As for scale, that should be based on whether or not you have retina display and what size of a device you're on (divide by 2 for retina display since a physical distance of 5 "points" on a regular screen as the user's finger feels it will come up as 10 "pixels" on a retina display screen, since each point is 4 pixels, so you'll wind up with a situation where the player has a very hard time tapping on retina display (which is a common oversight)
func distanceBetweenRectangles(r1: CGRect, r2: CGRect) -> CGFloat { // returns distance between boundaries of two rectangles or -1 if they intersect
let cornerPointsR1: [CGPoint] = [
CGPoint(x: r1.minX, y: r1.minY),CGPoint(x: r1.minX, y: r1.maxY),CGPoint(x: r1.maxX, y: r1.minY),CGPoint(x: r1.maxX, y: r1.maxY)]
let cornerPointsR2: [CGPoint] = [
CGPoint(x: r2.minX, y: r2.minY),CGPoint(x: r2.minX, y: r2.maxY),CGPoint(x: r2.maxX, y: r2.minY),CGPoint(x: r2.maxX, y: r2.maxY)]
for i in 0..<cornerPointsR1.count {
if (r2.contains(cornerPointsR1[i])) {
return -1
}
}
var distances: [CGFloat] = []
for i in 0..<cornerPointsR1.count {
for j in 0..<cornerPointsR2.count {
distances.append(distanceBetweenPoints(p1: cornerPointsR1[i], p2: cornerPointsR2[j]))
}
}
distances.sort()
return distances[0]
}
I extended above function to count distance between two CGRects. I count it by counting distance between coners of both CGRects and then returning the smallest distance. I copied function counting intersection point between two lines from:
func distanceBetweenRectangles(r1: CGRect, r2: CGRect) -> CGFloat { // returns distance between boundaries of two rectangles or -1 if they intersect
let cornerPointsR1: [CGPoint] = [
CGPoint(x: r1.minX, y: r1.minY),CGPoint(x: r1.minX, y: r1.maxY),CGPoint(x: r1.maxX, y: r1.minY),CGPoint(x: r1.maxX, y: r1.maxY)]
let cornerPointsR2: [CGPoint] = [
CGPoint(x: r2.minX, y: r2.minY),CGPoint(x: r2.minX, y: r2.maxY),CGPoint(x: r2.maxX, y: r2.minY),CGPoint(x: r2.maxX, y: r2.maxY)]
for i in 0..<cornerPointsR1.count {
if (r2.contains(cornerPointsR1[i])) {
return -1
}
}
var distances: [CGFloat] = []
for i in 0..<cornerPointsR1.count {
for j in 0..<cornerPointsR2.count {
distances.append(distanceBetweenPoints(p1: cornerPointsR1[i], p2: cornerPointsR2[j]))
}
}
distances.sort()
return distances[0]
}
发布评论
评论(11)
好吧,你也提到的东西完整的代码在哪里:
距离是可变距离。
这里发生了什么:
你不能真正让 CGPoint 成为距离,距离没有 x 和 y 分量。这只是 1 个数字。
如果您认为 CGPoint 是一种测量单位(例如英尺是一种测量单位),那么事实并非如此。
Well, with stuff your refering too where is the full code:
The distance is the variable distance.
What is going on here:
You can't really make a CGPoint be the distance, distance doesn't have an x and y component. It is just 1 number.
If you think CGPoint is a unit of measurement (for example feet is a unit of measurement) it is not.
简短答案
较长的解释
如果您有两个点
p1
和p2
,显然很容易找到它们的height
和width< 之间的差异/code> (例如
ABS(p1.x - p2.x)
),但要找到它们距离的真实表示,您确实需要假设(下面的H
)。值得庆幸的是,有一个内置宏:
hypotf
(或hypot
fordoubles
):(原始参考)
Short Answer
Longer Explanation
If you have two points
p1
andp2
it is obviously easy to find the difference between theirheight
andwidth
(e.g.ABS(p1.x - p2.x)
) but to find a true representation of their distance you really want the hypothenuse (H
below).Thankfully there is a built in macro for this:
hypotf
(orhypot
fordoubles
):(original reference)
在 Swift 中,您可以向 CGPoint: 添加扩展,
并像这样使用它:
In Swift, you can add an extension to CGPoint:
and use it like this:
我必须手动执行此操作 10,000 次,所以我为它编写了一个函数并将其保存在我的个人库中,我总是在新程序开始时转储该库,所以我忘记了它不是大炮。
所以你可以这样称呼它(假设你想知道你的手指移动了多远):
这对于移动功能以及滚动菜单中的点检查很有用:
在touchesBegan中设置“isItATap”true,将以上内容放入touchesMoved中,那么您就知道玩家将手指移动得太远,触摸无法成为“轻击”,因此您可以让它不选择玩家触摸的对象,而是滚动该对象。
至于比例,这应该基于您是否有视网膜显示屏以及您所使用的设备尺寸(视网膜显示屏除以 2,因为在常规屏幕上,用户手指的物理距离为 5 个“点”)感觉它会在视网膜显示屏上显示为 10 个“像素”,因为每个点都是 4 个像素,所以您最终会遇到玩家很难点击视网膜显示屏的情况(这是一个常见的疏忽) )
I've had to do this by hand 10,000 times so I wrote a function for it and stuck it in my personal library that I always dump in at the beginning of a new program so I forget it's not cannon.
so you call it like this (say you want to know how far you moved your finger):
This is useful in movement functions as well for spot checking in a scrolling menu:
Set "isItATap" true in touchesBegan, put the above in touchesMoved, then you know the player moved their finger too far for the touch to be a "tap", so you can have it NOT select the object the player touched and instead scroll the object around.
As for scale, that should be based on whether or not you have retina display and what size of a device you're on (divide by 2 for retina display since a physical distance of 5 "points" on a regular screen as the user's finger feels it will come up as 10 "pixels" on a retina display screen, since each point is 4 pixels, so you'll wind up with a situation where the player has a very hard time tapping on retina display (which is a common oversight)
听起来您可能想要从 p1 到 p2 的向量(或差异)而不是距离。
Sounds like you probably want the vector from p1 to p2 (or difference) rather than the distance.
在Apple的示例项目中,他们使用hypot。这将返回两点之间的斜边(距离),如本答案中所述。
In Apple's sample projects, they use hypot. This returns hypothenuse (distance) between two points as explained in this answer.
只有这个...
其中 p1 和 p2 是 CGPoint 的对象
only this...
where p1 and p2 are objects of CGPoint
斯威夫特 4、斯威夫特 3 解决方案
Swift 4, Swift 3 solution
使用 Vision api 到另一个点的欧几里德距离。
从iOS 14开始。
Euclidean distance to another point with Vision api.
Starting from iOS 14.
我扩展了上面的函数来计算两个 CGRect 之间的距离。我通过计算两个 CGRect 的角之间的距离来计算它,然后返回最小的距离。我从以下位置复制了计算两条线之间交点的函数:
I extended above function to count distance between two CGRects. I count it by counting distance between coners of both CGRects and then returning the smallest distance. I copied function counting intersection point between two lines from: