点在圆内 [英] Point within circle

查看:152
本文介绍了点在圆内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

最初你问Objective-C。

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.

这篇关于点在圆内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆