Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

macfanboy

macrumors 6502a
Original poster
Jun 5, 2007
916
163
maybe this is a noob question (sorry!)
but im using CGRect to create multiple rectangles. How could i detect if they got within 5 pixels of each other?

sorry again if this is a noob question
 

xsmasher

macrumors regular
Jul 18, 2008
140
0
maybe this is a noob question (sorry!)
but im using CGRect to create multiple rectangles. How could i detect if they got within 5 pixels of each other?

sorry again if this is a noob question

As beer implied, there's nothing built-in for this, but you can use trigonometry to figure it out. Specifically the Pythagorean theorem - think of your two points as being on the hypotenuse of a triangle. In code:

dx = a.x - b.x;
dy = a.y - b.y;
distance = sqrt(dx*dx + dy*dy);

That will give you the distance between the centers (not the edges) of the objects, which may be good enough for you.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.