Pages

June 13, 2013

Compute the distance between two points

Simple but helpful math function: calculate the distance bewteen two CGPoint in Objective-C.


/** Compute the distance between two points in 2D space.

 @param p1 First point.
 @param p2 Second point.
 @return Returns the distance between p1 and p2.

 */
+ (CGFloat)distanceIn2dFromPoint1:(CGPoint)p1 toPoint2:(CGPoint)p2 {
    return sqrt( (p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y) );
}



Download the ready-for-use source file (.m) of Compute the distance between two points

No comments:

Post a Comment