Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A somewhat less "<a href="http://en.wikipedia.org/wiki/Na%C3%AFve_algorithm" rel="nofollow noreferrer">naïve</a>" approach would be to use a <a href="http://alienryderflex.com/polygon_fill/" rel="nofollow noreferrer">polygon fill algorithm</a>, and then select points from the fill lines randomly.</p> <h2>C Code Sample</h2> <pre><code>// public-domain code by Darel Rex Finley, 2007 int nodes, nodeX[MAX_POLY_CORNERS], pixelX, pixelY, i, j, swap ; // Loop through the rows of the image. for (pixelY=IMAGE_TOP; pixelY&lt;IMAGE_BOT; pixelY++) { // Build a list of nodes. nodes=0; j=polyCorners-1; for (i=0; i&lt;polyCorners; i++) { if (polyY[i]&lt;(double) pixelY &amp;&amp; polyY[j]&gt;=(double) pixelY || polyY[j]&lt;(double) pixelY &amp;&amp; polyY[i]&gt;=(double) pixelY) { nodeX[nodes++]=(int) (polyX[i]+(pixelY-polyY[i])/(polyY[j]-polyY[i]) *(polyX[j]-polyX[i])); } j=i; } // Sort the nodes, via a simple “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++; }} // Fill the pixels between node pairs. // Code modified by SoloBold 27 Oct 2008 // The flagPixel method below will flag a pixel as a possible choice. for (i=0; i&lt;nodes; i+=2) { if (nodeX[i ]&gt;=IMAGE_RIGHT) break; if (nodeX[i+1]&gt; IMAGE_LEFT ) { if (nodeX[i ]&lt; IMAGE_LEFT ) nodeX[i ]=IMAGE_LEFT ; if (nodeX[i+1]&gt; IMAGE_RIGHT) nodeX[i+1]=IMAGE_RIGHT; for (j=nodeX[i]; j&lt;nodeX[i+1]; j++) flagPixel(j,pixelY); }}} // TODO pick a flagged pixel randomly and fill it, then remove it from the list. // Repeat until no flagged pixels remain. </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload