(Objective C) How to find the distance between two CG points

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.

  1.  
  • Posted by on January 1st, 1970
This entry was posted in Objective-C and tagged , , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Why ask?