Pages

Showing posts with label geometry. Show all posts
Showing posts with label geometry. Show all posts

September 1, 2013

Test if a point is inside a circle

Oh, geometry! This Objective-c method cover a simple question: is a CGPoint inside a circle?
You need:
- http://objective-c-functions.blogspot.com/2013/06/compute-distance-between-two-points.html


/** Check whether a point lays inside a circle.

 @param p Point to test.
 @param circleCenter Center of the circle.
 @param circleRadius Circle radius.
 @return Returns true if point is within circle, false otherwise.
 @see pointInCircleWithOrigin:ray:atAngle:

 */
+ (BOOL)isPoint:(CGPoint)p insideCircleWithCenter:(CGPoint)circleCenter radius:(CGFloat)circleRadius {
    return ( [LMFunctions distanceIn2dFromPoint1:p toPoint2:circleCenter] <= circleRadius );
}



Download the ready-for-use source file (.m) of Test if a point is inside a circle

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