How to find the distance between two CG points?

IphoneCocoa Touch

Iphone Problem Overview


When we do multitouch with two fingers in a UIScrollView, we get two CG points. I want to find the distance between them. Then when again we do the pinch(inside or outside), Then we will again get two points. Then after finding the distance again between these two points , I want to decide whether I pinched in or out. If i have pinched in, surely the new distance will be lesser and vice versa.

But don't know how to find an accurate measurement for the distance between 2 points for doing comparison ? Is anyone having idea about this ?

Iphone Solutions


Solution 1 - Iphone

You can use the hypot() or hypotf() function to calculate the hypotenuse. Given two points p1 and p2:

CGFloat distance = hypotf(p1.x - p2.x, p1.y - p2.y);

And that's it.

Solution 2 - Iphone

Distance between p1 and p2:

CGFloat xDist = (p2.x - p1.x);
CGFloat yDist = (p2.y - p1.y);
CGFloat distance = sqrt(xDist * xDist + yDist * yDist);

Put in a function:

func distance(_ a: CGPoint, _ b: CGPoint) -> CGFloat {
    let xDist = a.x - b.x
    let yDist = a.y - b.y
    return CGFloat(sqrt(xDist * xDist + yDist * yDist))
}

Background: Pythagorean theorem

If you only need to calculate if the distance between the points increases or decreases, you can omit the sqrt() which will make it a little faster.

Solution 3 - Iphone

For swift users

extension CGPoint {
    func distance(to point: CGPoint) -> CGFloat {
        return sqrt(pow(x - point.x, 2) + pow(y - point.y, 2))
    }
}

Solution 4 - Iphone

With Swift 4, you may choose one of the 5 following Playground codes in order to get the distance between two CGPoint instances.


1. Using Darwin sqrt(_:) function

import CoreGraphics

func distance(from lhs: CGPoint, to rhs: CGPoint) -> CGFloat {
    let xDistance = lhs.x - rhs.x
    let yDistance = lhs.y - rhs.y
    return sqrt(xDistance * xDistance + yDistance * yDistance)
}

let point1 = CGPoint(x: -10, y: -100)
let point2 = CGPoint(x: 30, y: 600)

distance(from: point1, to: point2) // 701.141925718324

2. Using CGFloat squareRoot() method

import CoreGraphics

func distance(from lhs: CGPoint, to rhs: CGPoint) -> CGFloat {
    let xDistance = lhs.x - rhs.x
    let yDistance = lhs.y - rhs.y
    return (xDistance * xDistance + yDistance * yDistance).squareRoot()
}

let point1 = CGPoint(x: -10, y: -100)
let point2 = CGPoint(x: 30, y: 600)

distance(from: point1, to: point2) // 701.141925718324

3. Using CGFloat squareRoot() method and Core Graphics pow(_:_:) function

import CoreGraphics

func distance(from lhs: CGPoint, to rhs: CGPoint) -> CGFloat {
    return (pow(lhs.x - rhs.x, 2) + pow(lhs.y - rhs.y, 2)).squareRoot()
}

let point1 = CGPoint(x: -10, y: -100)
let point2 = CGPoint(x: 30, y: 600)

distance(from: point1, to: point2) // 701.141925718324

4. Using Core Graphics hypot(_:_:) function

import CoreGraphics

func distance(from lhs: CGPoint, to rhs: CGPoint) -> CGFloat {
    return hypot(lhs.x - rhs.x, lhs.y - rhs.y)
}

let point1 = CGPoint(x: -10, y: -100)
let point2 = CGPoint(x: 30, y: 600)

distance(from: point1, to: point2) // 701.141925718324

5. Using Core Graphics hypot(_:_:) function and CGFloat distance(to:) method

import CoreGraphics

func distance(from lhs: CGPoint, to rhs: CGPoint) -> CGFloat {
    return hypot(lhs.x.distance(to: rhs.x), lhs.y.distance(to: rhs.y))
}

let point1 = CGPoint(x: -10, y: -100)
let point2 = CGPoint(x: 30, y: 600)

distance(from: point1, to: point2) // 701.141925718324

Solution 5 - Iphone

-(float)distanceFrom:(CGPoint)point1 to:(CGPoint)point2
{
CGFloat xDist = (point2.x - point1.x);
CGFloat yDist = (point2.y - point1.y);
return sqrt((xDist * xDist) + (yDist * yDist));
}

If you are using cocos2d

float distance = ccpDistance(point1, point2);

Solution 6 - Iphone

I wrote this, I use it a lot:

- (float) distanceBetween : (CGPoint) p1 and: (CGPoint) p2
{
    return sqrt(pow(p2.x-p1.x,2)+pow(p2.y-p1.y,2));
}

Call like this:

float distanceMoved = [self distanceBetween touchStart and: touchEnd];

I normally use cocos2d, but I still use my own function for some things because when I was learning I wrote a bunch of my own functions for simple stuff rather than searching for the "official" higher order functions, and additionally I'm not a big fan of functions(vars, vars), I prefer [self functions vars and: vars]

Solution 7 - Iphone

If you want to find the absolute distance value between two points then you can use (for Cocos2d):

float distance = abs(ccpDistance(point1, point2));

Solution 8 - Iphone

#define rw_pointOffset(point1, point2) CGPointMake(point2.x - point1.x, point2.y - point1.y)
#define rw_pointDistance(point1, point2) sqrtf( powf(point2.x - point1.x, 2.0f) + powf(point2.y - point1.y, 2.0f))

And that´s how you use it:

CGPoint offset = rw_pointOffset(view1.center, view2.center);
float distance = rw_pointDistance(view1.center, view2.center);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionwolverineView Question on Stackoverflow
Solution 1 - IphoneluciusView Answer on Stackoverflow
Solution 2 - IphoneOle BegemannView Answer on Stackoverflow
Solution 3 - IphoneDaniel KromView Answer on Stackoverflow
Solution 4 - IphoneImanou PetitView Answer on Stackoverflow
Solution 5 - IphonehaawaView Answer on Stackoverflow
Solution 6 - IphonereduxView Answer on Stackoverflow
Solution 7 - Iphoneuser2425159View Answer on Stackoverflow
Solution 8 - IphonecocoView Answer on Stackoverflow