Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Instead of putting the occurance inside a circle as an item in an array, count the number of occurances in an array with the same length as the number of circles. I.e. instead of <code>[g,g,g,g,b,b,r,r,r,r,r,r,r]</code>, use <code>[4,2,7]</code>. That way you don't have to keep the array sorted when adding items, you just increment the correct item.</p> <p>That also makes it easy to sort the result on relevance. You can create a numeric value for the array by looking at it as a number with the base being the same as the number of data coordinates. I.e.:</p> <pre><code>var n = arr[0] * data.length * data.length + arr[1] * data.length + arr[2]; </code></pre> <p>With 10 data coordinates, the array <code>[4,2,7]</code> would get the value 427, making it easy to compare it to the array <code>[3,5,0]</code> that would get the value 350.</p> <p>You can calculate the arrays as:</p> <pre><code>for(var i=0; i &lt; coords.length; i++) { var arr = [0, 0, 0]; for(var j=0; j &lt; data.length; j++) { var xs = coords[i][1] - data[j][0]; var ys = coords[i][2] - data[j][1]; distance = Math.sqrt(xs * xs + ys * ys); if (distance &lt;= 25) { arr[0]++; } else if (distance &lt;= 60) { arr[1]++; } else if (distance &lt;= 150) { arr[2]++; } } coords[i].arr = arr; coords[i].value = arr[0] * data.length * data.length + arr[1] * data.length + arr[2]; } </code></pre> <p>Now you can sort the coordinates on the values:</p> <pre><code>coords.sort(function(a, b){ return a.value - b.value; }); </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