指向圈内

时间:2011-09-13 08:30:50

标签: objective-c cocoa-touch geometry computational-geometry

给定圆的中心点和半径,我如何知道某个点(x,y)是否在圆中?谁知道呢?感谢。

1 个答案:

答案 0 :(得分: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
    }
}

此代码假定您处理子类视图中的圆圈。