2015-11-06 114 views
0

我在矩形内画了四分之一圆。客观C - 如何知道点是否在四分之一圆内?

矩形

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; 

Cirlce季:

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]; 

然后我的tarted绘图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]; 
     } 


    } 

我测试,如果点是用这个圆圈内:

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

其中xvalueyvalue是将要创建的点的坐标,并XcenterCircleYcenterCircle是圆心的coordiantes。

我有什么错的,因为它给了我这样的结果(它正确dosent测试,如果该点位于圆圈内或不:圆圈内的点的一部分,被认为是外):

enter image description here

你能告诉我我在做什么错吗?以及我如何才能确切地说明圈内的点?

+1

也许是这样的:http://stackoverflow.com/questions/10831370/how-to-check-if-touch-point-is-on-uibezierpath-ios – Alex

+0

@Alex这是一个很好的解决方案。 – Sulthan

回答

2

*不是电源操作,它是乘法。

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

if (valeurPoint < (radius * radius)) 

应该解决您的问题

或使用pow功能:

float valeurPoint = pow((xvalue - XcenterCircle), 2) + pow((yvalue -YcenterCircle), 2); 

您也可以直接使用hypot功能(虽然性能略差,因为sqrt计算)

float distance = hypotf((xvalue - XcenterCircle), (yvalue -YcenterCircle)); 

if (distance < radius) 

编辑: 感谢@Alex的建议。最好的解决方案是使用本地方法-[UIBerierPath containsPoint:]。那么你根本不需要计算距离。

+0

我要测试这个解决方案,并回复你 – samouray

+0

这对我来说,我有电力操作问题 – samouray

相关问题