Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>You need too keep your points in drawing order (not “x” order)</strong></p> <p>To do that, add a sortBy field to each point object </p> <pre><code>{ x:10, y:10, sortBy:”1” } { x:30, y:10, sortBy:”2” } </code></pre> <p>To add a point between these 2 points, just add a suffix letter to the sortBy field:</p> <pre><code>{ x:10, y:10, sortBy:”1” } { x:20, y:10, sortBy:”1A” } { x:30, y:10, sortBy:”2” } </code></pre> <p>And then sort your objects by the sortBy field using a compare function like this:</p> <pre><code>function myCompare(a,b){ if(a.sortBy&lt;b.sortBy){ return -1; } if(a.sortBy&gt;b.sortBy){ return 1; } return(0); } myPoints.sort(myCompare); </code></pre> <p>You can create an appendPoint function for inserting point objects in sequential order</p> <pre><code>var points=[]; var index=0; appendPoint(100,150); appendPoint(200,150); function appendPoint(x,y){ points.push({x:x,y:y,sortBy:index.toString()}); index++; } </code></pre> <p>And create an insertPoint function that inserts a point object after any sortBy index:</p> <pre><code>insertPoint(150,100,0); function insertPoint(x,y,afterIndex){ points.push({x:x,y:y,sortBy:afterIndex.toString()+"Z"}); points.sort(pointsCompare); } function pointsCompare(a,b){ if(a.sortBy&lt;b.sortBy){ return -1; } if(a.sortBy&gt;b.sortBy){ return 1; } return(0); } </code></pre> <p>Here’s example code:</p> <pre><code>&lt;!doctype html&gt; &lt;html&gt; &lt;head&gt; &lt;link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /&gt; &lt;!-- reset css --&gt; &lt;script type="text/javascript" src="http://code.jquery.com/jquery.min.js"&gt;&lt;/script&gt; &lt;style&gt; body{ background-color: ivory; } .cf:before, .cf:after { content: " "; /* 1 */ display: table; /* 2 */ } .cf:after { clear: both; } &lt;/style&gt; &lt;script&gt; $(function(){ var points=[]; var index=0; appendPoint(100,150); appendPoint(200,150); showPoints(); insertPoint(150,100,0); showPoints(); function appendPoint(x,y){ points.push({x:x,y:y,sortBy:index.toString()}); index++; } function insertPoint(x,y,afterIndex){ points.push({x:x,y:y,sortBy:afterIndex.toString()+"Z"}); points.sort(pointsCompare); } function pointsCompare(a,b){ if(a.sortBy&lt;b.sortBy){ return -1; } if(a.sortBy&gt;b.sortBy){ return 1; } return(0); } function showPoints(){ for(var i=0;i&lt;points.length;i++){ var pt=points[i]; console.log(pt.sortBy+": "+pt.x+"/"+pt.y); } } }); // end $(function(){}); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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