title: Polygon Filling Algorithm for Polygons with crossing Lines in C
body: <p>I have this implementation of my polygon filling algorithm:</p>
  
  <pre><code>//point in polygon algorithm for filling polyons
  void fill_polygon(Array *vx,Array *vy){
      int  nodes,  pixelX, pixelY, i, j, swap,polyCorners ;
      double top,bot,right,left,nodeX[1000];
      polyCorners = vx-&gt;used;
      if(polyCorners&lt;=2){
          return;
      }
                  /*printf("Soltado VX: Used(%d) \n",vx-&gt;used);
                  for(i=0;i&lt;vx-&gt;used;i++){
                      printf("|%d|", vx-&gt;array[i]); 
  
                  }
                  printf("\n VY: Used(%d) \n",vy-&gt;used);
                  for(i=0;i&lt;vy-&gt;used;i++){
                      printf("|%d|", vy-&gt;array[i]); 
  
                  }       
                  printf("\n");
                  wait(35);   */
      bot = maximum(vy);
      top = minimum(vy);
      right = maximum(vx);
      left = minimum(vx);
      /*printf("Top %d  - Bot %d - Left %d - Right %d \n",top,bot,left,right);
      wait(40);*/
      for (pixelY=top; pixelY&lt;bot; pixelY++) {
  
          //  Construyendo lista de Nodos de la figura
          nodes=0; j=polyCorners-1;
          for (i=0; i&lt;polyCorners; i++) {
              if (vy-&gt;array[i]&lt; (double)pixelY &amp;&amp; vy-&gt;array[j]&gt;=(double)pixelY
                  ||  vy-&gt;array[j]&lt;(double) pixelY &amp;&amp; vy-&gt;array[i]&gt;=(double)pixelY) {
  
                  nodeX[nodes++]=(vx-&gt;array[i]+(pixelY-vy-&gt;array[i])/(vy-&gt;array[j]-vy-&gt;array[i])*(vx-&gt;array[j]-vx-&gt;array[i])); 
  
              }
              j=i; 
          }   
          //  Ordenamos nodos con bubble sort
          i=0;
          while (i&lt;nodes-1) {
              if (nodeX[i]&gt;nodeX[i+1]) {
                swap=nodeX[i]; nodeX[i]=nodeX[i+1]; nodeX[i+1]=swap; if (i) i--; 
              }
  
              else {
                i++; 
              }
          }
       // Rellenamos pixeles entre los nodos
          for (i=0; i&lt;nodes; i+=2) {
              if   (nodeX[i]&gt;=right) break;
  
              if   (nodeX[i+1]&gt; left ) {
                  if (nodeX[i  ]&lt; left ) nodeX[i  ]=left ;
                  if (nodeX[i+1]&gt; right) nodeX[i+1]=right;
                  for (pixelX= (int)nodeX[i]; pixelX&lt;nodeX[i+1]; pixelX++){
  
                      plot_pixel(pixelX,pixelY,current_color);
                  }  
              }
          }       
  
      }
  }
  </code></pre>
  
  <p>It is working fine for normal polygons but it is not filling the polygons as it should when I have a crossing polygon like the one below. (The rightis the un filled polygon, the leftis the result of my algorithm.</p>
  
  <p><a href="https://i.stack.imgur.com/xsfkL.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xsfkL.jpg" alt="enter image description here"></a></p>
  
  <p>When the result should be something like this:</p>
  
  <p><a href="https://i.stack.imgur.com/go28d.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/go28d.jpg" alt="enter image description here"></a></p>