88

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 ?

Chetan Purohit
  • 364
  • 1
  • 3
  • 16
wolverine
  • 2,967
  • 5
  • 31
  • 35
  • See [my answer for Swift 4](https://stackoverflow.com/a/46036132/1966109) that shows up to 5 different ways to find the distance between two `CGPoint`s. – Imanou Petit Nov 18 '18 at 10:47

8 Answers8

195

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.

lucius
  • 8,665
  • 3
  • 34
  • 41
131

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.

Ole Begemann
  • 135,006
  • 31
  • 278
  • 256
32

For swift users

extension CGPoint {
    func distance(to point: CGPoint) -> CGFloat {
        return sqrt(pow(x - point.x, 2) + pow(y - point.y, 2))
    }
}
Leo
  • 3,003
  • 5
  • 38
  • 61
Daniel Krom
  • 9,751
  • 3
  • 43
  • 44
19

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
Imanou Petit
  • 89,880
  • 29
  • 256
  • 218
12
-(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);
haawa
  • 3,078
  • 1
  • 26
  • 35
  • 2
    It appears ccpDistance is part of cocos2d, not part of core. Including it to avoid the pythagorean theorem is probably overkill. – Vox Feb 21 '13 at 20:34
3

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]

Oded Ben Dov
  • 9,936
  • 6
  • 38
  • 53
redux
  • 1,157
  • 10
  • 21
0

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

float distance = abs(ccpDistance(point1, point2));
Chris
  • 5,882
  • 2
  • 32
  • 57
0
#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);
coco
  • 4,912
  • 2
  • 25
  • 33