Reputation: 578
I drew a quarter of a circle inside a rectangle.
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;
Quarter of 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]];
Then I added the rectangle in the view and added the circle inside the rectangle:
[self.view addSubview:Rectangle];
[Rectangle.layer addSublayer:circleLayer];
Then I started drawing little rectangles of 1 width and 1 height that I consider as points, and randomly added them into the view with a for loop, coloring the points inside the circle with green and points outside of the circle with red
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];
}
}
I test if the point is inside the circle using this :
float valeurPoint = (xvalue - XcenterCircle)*2 + (yvalue -YcenterCircle)*2;
where xvalue
and yvalue
are the coordinates of the point that will be created, and XcenterCircle
and YcenterCircle
are the coordiantes of the center of the circle.
I have something wrong because it gives me this result (it dosent test correctly if the point is inside the circle or not: a part of the points inside of the circle are considered outside):
can you tell me what I am doing wrong here ? and how can I exactly the points inside the circle ?
Upvotes: 0
Views: 868
Reputation: 130102
*
is not power operation, it is multiplication.
float valeurPoint = (xvalue - XcenterCircle) * (xvalue - XcenterCircle) + (yvalue -YcenterCircle)*(yvalue -YcenterCircle);
if ( valeurPoint < (radius * radius) )
Should fix your problem
or using the pow
function:
float valeurPoint = pow((xvalue - XcenterCircle), 2) + pow((yvalue -YcenterCircle), 2);
You can also use hypot
function directly (although the performance can be slightly worse because of sqrt
calculation)
float distance = hypotf((xvalue - XcenterCircle), (yvalue -YcenterCircle));
if (distance < radius)
EDIT:
Thanks @Alex for suggestion. The best solution is to use the native method -[UIBerierPath containsPoint:]
. Then you won't have to calculate the distance at all.
Upvotes: 2