title: Objective C - how to know if point is inside a quarter of circle?
body: <p>I drew a quarter of a circle inside a rectangle. </p>
  
  <p><strong>Rectangle</strong>:</p>
  
  <pre><code>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;
  </code></pre>
  
  <p><strong>Quarter of Cirlce</strong>:</p>
  
  <pre><code>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]];
  </code></pre>
  
  <p>Then I added the rectangle in the view and added the circle inside the rectangle:</p>
  
  <pre><code>[self.view addSubview:Rectangle];
  [Rectangle.layer addSublayer:circleLayer];
  </code></pre>
  
  <p>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</p>
  
  <pre><code>int compteurPointsinCercle  = 0 ;
      int compteurPointsOutCercle  = 0 ;
      float       XcenterCircle   =   center.x;
      float       YcenterCircle   =   center.y;
  
      for (int i = 0 ; i &lt; 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 &lt; radius^2
          float valeurPoint = (xvalue - XcenterCircle)*2 + (yvalue -YcenterCircle)*2;
  
          NSLog(@"(Inside for), valeurPoint is : %f",valeurPoint);
  
  
          if ( valeurPoint &lt;  (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 &gt; (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];
          }
  
  
      }
  </code></pre>
  
  <p>I test if the point is inside the circle using this : </p>
  
  <pre><code> float valeurPoint = (xvalue - XcenterCircle)*2 + (yvalue -YcenterCircle)*2;
  </code></pre>
  
  <p>where <code>xvalue</code> and <code>yvalue</code> are the coordinates of the point that will be created, and <code>XcenterCircle</code> and <code>YcenterCircle</code> are the coordiantes of the center of the circle.</p>
  
  <p>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):</p>
  
  <p><a href="https://i.stack.imgur.com/J6vi3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/J6vi3.png" alt="enter image description here"></a></p>
  
  <p>can you tell me what I am doing wrong here ? and how can I exactly the points inside the circle ? </p>