Return to Snippet

Revision: 46792
at May 25, 2011 08:18 by espinallab


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

Initial URL
http://stackoverflow.com/questions/1906511/how-to-find-the-distance-between-two-cg-points

Initial Description
Distance between p1 and p2:

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

Background: Pythagorean theorem

Edit: 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.

Initial Title
How to find the distance between two CG points?

Initial Tags


Initial Language
Objective C