Reputation: 1895
I need to calculate the distance between two CGPoint
s. I saw this, but I don't get it.
Upvotes: 35
Views: 34139
Reputation: 15566
CGPoint p1, p2; // Having two points
CGFloat distance = hypotf((p1.x-p2.x), (p1.y-p2.y));
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));
(original reference)
Upvotes: 27
Reputation: 11143
extension CGPoint {
func magnitude() -> CGFloat {
return sqrt(x * x + y * y)
}
func distance(to: CGPoint) -> CGFloat {
return CGPoint(x: to.x - x, y: to.y - y).magnitude()
}
}
Upvotes: 2
Reputation: 393
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]
}
Upvotes: -1
Reputation: 580
Euclidean distance to another point with Vision api.
Starting from iOS 14.
import Vision
extension CGPoint {
public func distance(to point: CGPoint) -> Double {
VNPoint(location: self).distance(VNPoint(location: point))
}
}
print(CGPoint(x: 1, y: 1).distance(to: .zero)) // 1.4142135623730951
Upvotes: 1
Reputation: 1463
In Apple's sample projects, they use hypot. This returns hypothenuse (distance) between two points as explained in this answer.
extension CGPoint {
func distance(from point: CGPoint) -> CGFloat {
return hypot(point.x - x, point.y - y)
}
}
Upvotes: 8
Reputation: 13364
In Swift, you can add an extension to CGPoint:
extension CGPoint {
func distance(to point: CGPoint) -> CGFloat {
return sqrt(pow((point.x - x), 2) + pow((point.y - y), 2))
}
}
and use it like this:
let distance = p1.distance(to: p2)
Upvotes: 25
Reputation: 27221
Swift 4, Swift 3 solution
extension CGPoint {
static func distanceBetween(point p1: CGPoint,
andPoint p2: CGPoint) -> CGFloat {
return sqrt(pow((p2.x - p1.x), 2) + pow((p2.y - p1.y), 2))
}
}
Upvotes: 1
Reputation: 1167
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.
- (float)distanceBetween:(CGPoint)p1 and:(CGPoint)p2
{
return sqrt(pow(p2.x-p1.x,2)+pow(p2.y-p1.y,2));
}
so you call it like this (say you want to know how far you moved your finger):
float moveDistance = [self distanceBetween:touchStart and:touchEnd];
This is useful in movement functions as well for spot checking in a scrolling menu:
if([self distanceBetween:touchStart and:touchAt] > 20*scalePoints)
isItATap = FALSE;
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)
Upvotes: 21
Reputation: 166
only this...
float distance = ccpLength(ccpSub(p1,p2));
where p1 and p2 are objects of CGPoint
Upvotes: 2
Reputation: 5421
Sounds like you probably want the vector from p1 to p2 (or difference) rather than the distance.
const CGPoint p1 = {10, 10};
const CGPoint p2 = {510, 310};
const CGPoint diff = {p2.x - p1.x, p2.y - p1.y} // == (CGPoint){500, 300}
Upvotes: 20
Reputation: 16250
Well, with stuff your refering too where is the full code:
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]
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.
Upvotes: 55