title: C++ / OpenGL - 2D - How to clip a circle in a rectangle boundary box
body: <p>I was just wondering how would I go about clipping a circle in a rectangular boundary box? I am currently using the Cohen–Sutherland algorithm for line clipping in my program and so far I've managed to get rectangles and polygons to clip. However, for circle clipping, I have no idea how I would accomplish this. I'm using the following to construct my circle:</p>
  
  <pre><code>glBegin(GL_POLYGON);
  double radius = 50;  
  for(int angle = 0; angle &lt;= 360; angle++ ){
      float const curve = 2 * PI * (float)angle / (float)360;
      glVertex2f(point.x + sin(curve) * radius, point.y + cos(curve) * radius);
  }
  glEnd();
  </code></pre>
  
  <p>My clipping algorithm is the same as the one here: <a href="http://en.wikipedia.org/wiki/Cohen%E2%80%93Sutherland_algorithm" rel="nofollow">http://en.wikipedia.org/wiki/Cohen%E2%80%93Sutherland_algorithm</a>. However, it returns 2 points representing a new line to later be used to draw the clipped shape. So basically I've tried to do this:</p>
  
  <pre><code>line Lines[360] // an array with size 360 with data type line, which is a struct holding two points (x1, y1, x2, y2) of the new line returned by my clipping function.
  
  double radius = 50;
  for(int angle = 0; angle &lt; 360; angle++){
      float const currentCurve = 2 * PI * (float)angle / (float)360;
      float const nextCurve = 2 * PI * (float)(angle+1) / (float)360;
      int x1 = (int)(point[i].x + sin(currentCurve) * radius); // point is another struct holding only a single point. 
      y1 = (int)(point[i].y + cos(currentCurve) * radius);
      x2 = (int)(point[i+1].x+ sin(nextCurve) * radius);
      y2 = (int)(point[i+1].y + cos(nextCurve) * radius);=
      // Clip the points with the clipping algorithm:
      Lines[i] = Clipper(x1, y1, x2, y2);
  }
  
  // Once all lines have been clipped or not, draw:
  
  glBegin(GL_POLYGON);
  for(int i = 0; i &lt; 360; i++){
      glVertex2f(Lines[i].x1, Lines[i].y1);
      glVertex2f(Lines[i].x2, Lines[i].y2);
  }
  glEnd();
  </code></pre>
  
  <p>Note that, I've drawn a circle on the screen with a mouse and and stored each 360 points into a struct array called point, which is apart of a linked list. So I have like 1 node representing one circle on the screen.</p>
  
  <p>Anyway, with the above, my circle is not drawing clipped (or drawing at all for that matter) and my application crashes after a few mouse clicks.</p>