Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 目标C-如何知道点是否在圆的四分之一以内?_Ios_Objective C_Geometry_Uibezierpath - Fatal编程技术网

Ios 目标C-如何知道点是否在圆的四分之一以内?

Ios 目标C-如何知道点是否在圆的四分之一以内?,ios,objective-c,geometry,uibezierpath,Ios,Objective C,Geometry,Uibezierpath,我在一个矩形内画了四分之一个圆 矩形: UIView *Rectangle = [[UIView alloc] initWithFrame:CGRectMake(0,0,[[UIScreen mainScreen] bounds].size.width,[[UIScreen mainScreen] bounds].size.height-292)]; Rectangle.backgroundColor = [UIColor lightGrayColor]; Rectangle.

我在一个矩形内画了四分之一个圆

矩形

UIView *Rectangle  = [[UIView alloc] initWithFrame:CGRectMake(0,0,[[UIScreen mainScreen] bounds].size.width,[[UIScreen mainScreen] bounds].size.height-292)];
    Rectangle.backgroundColor = [UIColor lightGrayColor];
    Rectangle.layer.zPosition = -5;
CGPoint center;
center.x = 0;
center.y = 0;
float radius = [[UIScreen mainScreen] bounds].size.width;

UIBezierPath *circle = [UIBezierPath bezierPathWithArcCenter:center
                                                      radius:radius
                                                  startAngle:0
                                                    endAngle:M_PI
                                                   clockwise:YES];
CAShapeLayer *circleLayer = [CAShapeLayer layer];
[circleLayer setPath:[circle CGPath]];
Cirlce的四分之一

UIView *Rectangle  = [[UIView alloc] initWithFrame:CGRectMake(0,0,[[UIScreen mainScreen] bounds].size.width,[[UIScreen mainScreen] bounds].size.height-292)];
    Rectangle.backgroundColor = [UIColor lightGrayColor];
    Rectangle.layer.zPosition = -5;
CGPoint center;
center.x = 0;
center.y = 0;
float radius = [[UIScreen mainScreen] bounds].size.width;

UIBezierPath *circle = [UIBezierPath bezierPathWithArcCenter:center
                                                      radius:radius
                                                  startAngle:0
                                                    endAngle:M_PI
                                                   clockwise:YES];
CAShapeLayer *circleLayer = [CAShapeLayer layer];
[circleLayer setPath:[circle CGPath]];
然后我在视图中添加了矩形,并在矩形内添加了圆形:

[self.view addSubview:Rectangle];
[Rectangle.layer addSublayer:circleLayer];

然后,我开始画1个宽度和1个高度的小矩形,我把它们看作是点,然后用一个for循环将它们随机地添加到视图中,用绿色表示圆圈内的点,并用红色

圆圈外的点。
int compteurPointsinCercle  = 0 ;
    int compteurPointsOutCercle  = 0 ;
    float       XcenterCircle   =   center.x;
    float       YcenterCircle   =   center.y;

    for (int i = 0 ; i < 50000 ; i++ )
    {

        float xvalue = arc4random_uniform([[UIScreen mainScreen] bounds].size.width);
        float yvalue = arc4random_uniform([[UIScreen mainScreen] bounds].size.height-292);

              // (x - center_x)^2 + (y - center_y)^2 < radius^2
        float valeurPoint = (xvalue - XcenterCircle)*2 + (yvalue -YcenterCircle)*2;

        NSLog(@"(Inside for), valeurPoint is : %f",valeurPoint);


        if ( valeurPoint <  (radius*2) )
        {
            // Point is inside of circle (green color)
            compteurPointsinCercle++;
            UIView *Rectangle2  = [[UIView alloc] initWithFrame:CGRectMake(xvalue,yvalue,1,1)];
            Rectangle2.backgroundColor = [UIColor greenColor];
            [self.view addSubview:Rectangle2];
        }
        else if ( valeurPoint > (radius*2) )
        {
            // Point is outside of circle (red color)
            compteurPointsOutCercle++;
            UIView *Rectangle2  = [[UIView alloc] initWithFrame:CGRectMake(xvalue,yvalue,1,1)];
            Rectangle2.backgroundColor = [UIColor redColor];
            [self.view addSubview:Rectangle2];
        }


    }
其中,
xvalue
yvalue
是将要创建的点的坐标,
XcenterCircle
YcenterCircle
是圆心的坐标

我有点不对劲,因为它给出了这个结果(如果该点在圆内或不在圆内,则测试不正确:圆内的一部分点被视为在圆外):


你能告诉我我做错了什么吗?我怎样才能精确地计算出圆内的点呢

*
不是幂运算,而是乘法

float valeurPoint = (xvalue - XcenterCircle) * (xvalue - XcenterCircle) + (yvalue -YcenterCircle)*(yvalue -YcenterCircle);

if ( valeurPoint <  (radius * radius) )
您也可以直接使用
hypot
函数(尽管由于
sqrt
计算,性能可能稍差)

float distance=hypotf((xvalue-XcenterCircle),(yvalue-YcenterCircle));
if(距离<半径)
编辑:
谢谢@Alex的建议。最好的解决方案是使用本机方法
-[uiberpath containsPoint:][/code>。那你就不必计算距离了。

可能是这样的:@Alex这是一个很好的解决方案。我将测试这个解决方案,然后回复你。我是为我做的,我在电源操作方面遇到了问题
float distance = hypotf((xvalue - XcenterCircle), (yvalue -YcenterCircle));

if (distance < radius)