圆内的点

3

给定圆的中心点和半径,如何知道某个点(x,y)是否在圆内?有人知道吗?谢谢。


4
一般性问题与您的标签无关,它很容易被搜索到。 - Mitch Wheat
不是特定于Objective-C,但这应该有所帮助。您应该能够相当容易地转换这些函数:https://dev59.com/pXRB5IYBdhLWcg3w4bAo - Max MacLeod
2
简而言之:确定从中心到点的距离,然后将其与半径进行比较。 - Markus Hedlund
1个回答

8

最初您要求的是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
    }
}

这段代码假设你正在处理一个子类化视图内部的圆形。

谢谢,非常感谢你的回答。 - james

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接