Posted By

espinallab on 05/25/11


distance cocos2d CGPoint

Versions (?)

Who likes this?

1 person have marked this snippet as a favorite


How to find the distance between two CG points?

 / Published in: Objective C


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. CGFloat xDist = (p2.x - p1.x);
  2. CGFloat yDist = (p2.y - p1.y);
  3. CGFloat distance = sqrt((xDist * xDist) + (yDist * yDist));

Report this snippet  

You need to login to post a comment.