title: Where are the standard C implementations of sutherland-hodgman polygon clipping algorithm?
body: <p>I have implemented my version of sutherland-hodgman polygon clipping algorithm, but I believe my implementation could be better. So I am wondering whether there is a standard implementation.</p>
  
  <p>Here is my implementation </p>
  
  <pre><code>bool shclip(stPt** verts, int *n, float left, float right, float bottom, float top)
  {
   if (leftclip(verts, n, left) &amp;&amp;
    rightclip(verts, n, right) &amp;&amp;
    bottomclip(verts, n, bottom) &amp;&amp;
    topclip(verts, n, top))
    return true;
   else
    return false;
  }
  
  bool leftclip(stPt** verts, int *n, float left)
  {
   int v1, v2;
   float x1, x2, y1, y2;
   float relx, rely;
  
   v1 = v2 = 0;
   while (v1 &lt; *n) {
    x1 = ((*verts)[v1]).x; 
    x2 = ((*verts)[(v1 + 1) % *n]).x;
    if (x1 &lt; left) {
     if (x2 &gt; left) {
      y1 = ((*verts)[v1]).y; y2 = ((*verts)[(v1 + 1) % *n]).y;
      relx = x2 - x1; rely = y2 - y1;
      nverts1[v2].y = (left - x1) * rely / relx + y1;
      nverts1[v2].x = left;
      nverts1[v2+1].y = ((*verts)[(v1 + 1) % *n]).y; 
      nverts1[v2+1].x = ((*verts)[(v1 + 1) % *n]).x;
      v2 += 2; 
     }
  
    } else {
     if (x2 &gt; left) {
      nverts1[v2].x = ((*verts)[(v1 + 1) % *n]).x; nverts1[v2].y = ((*verts)[(v1 + 1) % *n]).y;
      v2++; 
     } else {
      y1 = ((*verts)[v1]).y; y2 = ((*verts)[(v1 + 1) % *n]).y;
      relx = x2 - x1; rely = y2 - y1;
      nverts1[v2].y = (left - x1) * rely / relx + y1;
      nverts1[v2].x = left;
      v2++; 
     }
    }
    v1++;
   }
  
   if (v2 != 0) {
    *n = v2;
    (*verts) = nverts1;
    return true;
   } else
    return false; 
  }
  </code></pre>
  
  <p>Thanks.</p>
  
  <p><strong>EDIT</strong><br>
  1 It seems that I do not understand the algorithm cause my textbook does not explain it clearly. I looked at the orignal thesis, but could not manage to figure it out. </p>
  
  <p>2 The code I written is <strong>not</strong> retreent.</p>
  
  <p>3 I pass the vertexs between two clipper by copy from one vertex array to another vertex array which is <strong>not</strong> efficient. I thought I could use linklist or something that is much better data structure for the algorithm.</p>
  
  <p>4 What I mean with 'standard implementation' better to be the code implementated by the algorithm designer like nicholl-lee-nicholl or implementations in widly used standard library / graphic library.</p>