开发者

Point within circle

开发者 https://www.devze.com 2023-04-04 23:58 出处:网络
Given the c开发者_运维百科enter point and radius of a circle, how do I know if a certain point (x,y) is in the circle? Anyone knows it? Thanks.Originally you asked for Objective-C.

Given the c开发者_运维百科enter point and radius of a circle, how do I know if a certain point (x,y) is in the circle? Anyone knows it? Thanks.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消