我在矩形内画了四分之一圆。客观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;
其中xvalue
和yvalue
是将要创建的点的坐标,并XcenterCircle
和YcenterCircle
是圆心的coordiantes。
我有什么错的,因为它给了我这样的结果(它正确dosent测试,如果该点位于圆圈内或不:圆圈内的点的一部分,被认为是外):
你能告诉我我在做什么错吗?以及我如何才能确切地说明圈内的点?
也许是这样的:http://stackoverflow.com/questions/10831370/how-to-check-if-touch-point-is-on-uibezierpath-ios – Alex
@Alex这是一个很好的解决方案。 – Sulthan