Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's the "walking" method. Less efficient, but it works.</p> <pre><code>var arr = new Array(); for(var n=0; n&lt;25; n++) arr.push(n); var coords = new Array(); var x = 0; var y = 0; for(var i=0; i&lt;arr.length; i++) { if( x &gt; 4 ) { x = 0; y++; } coords[i] = {'x': x, 'y': y}; x++; } // okay, coords contain the coordinates of each item in arr // need to move along the perimeter until a collision, then turn. // start at 0,0 and move east. var dir = 0; // 0=east, 1=south, 2=west, 3=north. var curPos = {'x': 0, 'y': 0}; var resultList = new Array(); for(var x=0; x&lt;arr.length; x++) { // record the current position in results var resultIndex = indexOfCoords(curPos, coords); if(resultIndex &gt; -1) { resultList[x] = arr[resultIndex]; } else { resultList[x] = null; } // move the cursor to a valid position var tempCurPos = movePos(curPos, dir); var outOfBounds = isOutOfBounds(tempCurPos, coords); var itemAtTempPos = arr[indexOfCoords(tempCurPos, coords)]; var posInList = resultList.indexOf( itemAtTempPos ); if(outOfBounds || posInList &gt; -1) { dir++; if(dir &gt; 3) dir=0; curPos = movePos(curPos, dir); } else { curPos = tempCurPos; } } /* int indexOfCoords * * Searches coordList for a match to myCoords. If none is found, returns -1; */ function indexOfCoords(myCoords, coordsList) { for(var i=0; i&lt;coordsList.length; i++) { if(myCoords.x == coordsList[i].x &amp;&amp; myCoords.y == coordsList[i].y) return i; } return -1; } /* obj movePos * * Alters currentPosition by incrementing it 1 in the direction provided. * Valid directions are 0=east, 1=south, 2=west, 3=north * Returns the resulting coords as an object with x, y. */ function movePos(currentPosition, direction) { var newPosition = {'x':currentPosition.x, 'y':currentPosition.y}; if(direction == 0) { newPosition.x++; } else if(direction == 1) { newPosition.y++; } else if(direction == 2) { newPosition.x--; } else if(direction == 3) { newPosition.y--; } return newPosition; } /* bool isOutOfBounds * * Compares the x and y coords of a given position to the min/max coords in coordList. * Returns true if the provided position is outside the boundaries of coordsList. * * NOTE: This is just one, lazy way of doing this. There are others. */ function isOutOfBounds(position, coordsList) { // get min/max var minx=0, miny=0, maxx=0, maxy=0; for(var i=0; i&lt;coordsList.length; i++) { if(coordsList[i].x &gt; maxx) maxx = coordsList[i].x; else if(coordsList[i].x &lt; minx) minx = coordsList[i].x; if(coordsList[i].y &gt; maxy) maxy = coordsList[i].y; else if(coordsList[i].y &lt; miny) miny = coordsList[i].y; } if(position.x &lt; minx || position.x &gt; maxx || position.y &lt; miny || position.y &gt; maxy) return true; else return false; } </code></pre> <p>This will move through the grid in a direction until it hits the bounds or an item already in the results array, then turn clockwise. It's pretty rudimentary, but I think it would do the job. You could reverse it pretty simply.</p> <p>Here's a working example: <a href="http://www.imakewidgets.com/test/walkmatrix.html" rel="nofollow">http://www.imakewidgets.com/test/walkmatrix.html</a></p>
    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