Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Point within circle

Given the center point and radius of a circle, how do I know if a certain point (x,y) is in the circle? Anyone knows it? Thanks.

like image 448
james Avatar asked Mar 22 '26 21:03

james


1 Answers

Originally you asked for Objective-C.

CGFloat DistanceBetweenTwoPoints(CGPoint point1,CGPoint point2)
{
    CGFloat dx = point2.x - point1.x;
    CGFloat dy = point2.y - point1.y;
    return sqrt(dx*dx + dy*dy );
};

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint point = [[touches anyObject] locationInView:self];
    CGFloat distance = DistanceBetweenTwoPoints(self.circleCenter, point);
    if(distance < self.radius){
        //inside the circle
    }
}

This code assumes, that you dealing with the circle inside a subclassed View.

like image 193
vikingosegundo Avatar answered Mar 25 '26 10:03

vikingosegundo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!