How do I measure the distance between two CGPoint objects?

Is there a convenient way in iOS to measure the distance between two CGPoint objects on a view and test whether the distance comes withing a certain range?

@brower Thanks very much for your question!

A nice solution would be the following:

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)))

}

I found the above answer here :slight_smile:

I hope this helps!

All the best!

This topic was automatically closed after 166 days. New replies are no longer allowed.