Skip to content

Instantly share code, notes, and snippets.

@vonWolfehaus
Created March 5, 2013 05:06
Show Gist options
  • Save vonWolfehaus/5088154 to your computer and use it in GitHub Desktop.
Save vonWolfehaus/5088154 to your computer and use it in GitHub Desktop.
Discrete spiral formula
// http://www.actionscript.org/forums/showpost.php3?s=c68c3e3a7b9bfed76d971fe7658dd3b5&p=915318&postcount=7
function pointOnCounterClockwiseDiscreteSpiral( n:int, dir:String="N" ):Point {
var n_rt:Number = Math.sqrt(n);
var m:int = Math.floor( Math.sqrt(n_rt) );
var negCheck:int = (dir == "S" || dir == "E" ) ? Math.pow(-1, m + 1) : Math.pow(-1, m);
var odd:int = (Math.floor(2 * n_rt) % 2);
var part1:Number = (n - m * (m + 1));
var part2:Number = (dir="N" || dir="S") ? Math.ceil( m / 2 ) : Math.floor( m / 2 );
var pnt:Point = new Point();
pnt.x = negCheck * ( part1 * !odd + part2 );
pnt.y = negCheck * ( part1 * odd + part2 );
return pnt;
}
// i don't actually know if this is how it's supposed to work; NOT TESTED
var p;
for (var i:int = 0; i < 10; i++) {
p = pointOnCounterClockwiseDiscreteSpiral(i);
drawCellAt(p.x, p.y);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment