Point within circle

做~自己de王妃 提交于 2019-12-21 20:25:10

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/7398901/point-within-circle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!