Prime Numbers on Sunflower Seed Spiral
/**
* Copyright Quasimondo ( http://wonderfl.net/user/Quasimondo )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/fesy
*/
// forked from Quasimondo's Sunflower Seed Spiral
package {
import flash.display.Graphics;
import flash.display.Sprite;
public class SunflowerSpiral extends Sprite {
private var firstPrime:PrimeNumber = new PrimeNumber(2);
private var lastPrime:PrimeNumber = firstPrime.setNext(3).setNext(5).setNext(7);
public function SunflowerSpiral() {
renderSpiral( 5000, 1.75, 0.7, 0);
//renderSpiral( 120, 10, 5, 1);
}
private function renderSpiral( count:int, radius:Number, padding:Number, magicAngleIndex:uint = 0 ):void
{
var g:Graphics = graphics;
var x:Number = 200;
var y:Number = 200;
var divergence:Number = [137.50776,99.50078,222.492][magicAngleIndex % 3] / 180 * Math.PI;
for ( var i:int = 1; i <= count; i++ )
{
var r:Number = (radius + padding) * Math.sqrt( i ) - radius * 0.3;
var a:Number = divergence * i;
g.beginFill( isPrime( i ) ? 0xff8000 : 0xb0a8a0 );
g.drawCircle( x + Math.cos( a ) * r, y + Math.sin( a ) * r, radius);
g.endFill();
}
}
private function isPrime( n:Number ):Boolean
{
if ( ( n & 1 ) == 0 || ( n > 5 && n % 5 == 0) ) return false;
var max:Number = Math.sqrt( n );
if ( max == Math.floor(max) ) return false;
var p:PrimeNumber = firstPrime;
var pn:Number;
while ( p != null )
{
if ( (pn = p.n) > max ) return true;
if ( n % pn == 0 ) return false;
p = p.p;
}
var divisor:Number = lastPrime.n;
while ( ( divisor += 2 ) <= max )
{
if ( !isPrime(divisor) ) continue;
lastPrime = lastPrime.setNext( divisor );
if ( divisor > max ) return true;
if ( n % divisor == 0 ) return false;
}
return true;
}
}
}
class PrimeNumber
{
public var p:PrimeNumber;
public var n:Number;
public function PrimeNumber( n:Number )
{
this.n = n;
}
public function setNext( n:Number ):PrimeNumber
{
return p = new PrimeNumber( n );
}
}