tree
frocessingの練習サンプル。
↓にアニメーションをつけてみた。
http://formandcode.com/code-examples/recursive-tree
/**
* Copyright nipx ( http://wonderfl.net/user/nipx )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/4t2u
*/
package{
import frocessing.display.F5MovieClip2DBmp;
import frocessing.math.FMath;
[SWF(backgroundColor="#FFFFFF", frameRate="30", width="465", height="465")]
public class FlashTest extends F5MovieClip2DBmp{
private var dotSize:Number = 9;
private var angleOffsetA:Number;
private var angleOffsetB:Number;
private var funcs:Vector.<SeedCommand>;
public function setup():void{
size( 465, 465 );
noStroke();
fill( 0 );
smooth();
angleOffsetA = FMath.radians( 1.5 );
angleOffsetB = FMath.radians( 50 );
funcs = new <SeedCommand>[];
}
public function draw():void{
translate( width/2, height );
if( funcs.length == 0 ){
background( 255 );
funcs = seed1( dotSize, FMath.radians( 270 ), 0, 0 );
}
else{
var temp:Vector.<SeedCommand> = new <SeedCommand>[];
for each( var i:SeedCommand in funcs ){
temp = temp.concat( i.execute() );
}
funcs = temp;
}
}
private function seed1( dotSize:Number, angle:Number, x:Number, y:Number ):Vector.<SeedCommand>{
var vector:Vector.<SeedCommand> = new <SeedCommand>[];
if( dotSize > 1 ){
circle( x, y, dotSize/2 );
var newx:Number;
var newy:Number;
if( random( 1 ) > 0.1 ){
newx = x + Math.cos( angle ) * dotSize;
newy = y + Math.sin( angle ) * dotSize;
vector.push( new SeedCommand( seed1, new <Number>[ dotSize * 0.99, angle - angleOffsetA, newx, newy ] ) );
}
else{
newx = x + Math.cos( angle );
newy = y + Math.sin( angle );
vector.push( new SeedCommand( seed2, new <Number>[ dotSize * 0.99, angle + angleOffsetA, newx, newy ] ) );
vector.push( new SeedCommand( seed1, new <Number>[ dotSize * 0.60, angle + angleOffsetB, newx, newy ] ) );
vector.push( new SeedCommand( seed2, new <Number>[ dotSize * 0.50, angle - angleOffsetB, newx, newy ] ) );
}
}
return vector;
}
private function seed2( dotSize:Number, angle:Number, x:Number, y:Number ):Vector.<SeedCommand>{
var vector:Vector.<SeedCommand> = new <SeedCommand>[];
if ( dotSize > 1 ){
circle( x, y, dotSize/2 );
var newx:Number;
var newy:Number;
if( random( 1 ) > 0.1 ){
newx = x + Math.cos( angle ) * dotSize;
newy = y + Math.sin( angle ) * dotSize;
vector.push( new SeedCommand( seed2, new <Number>[ dotSize * 0.99, angle + angleOffsetA, newx, newy ] ) );
}
else{
newx = x + Math.cos( angle );
newy = y + Math.sin( angle );
vector.push( new SeedCommand( seed1, new <Number>[ dotSize * 0.99, angle + angleOffsetA, newx, newy ] ) );
vector.push( new SeedCommand( seed2, new <Number>[ dotSize * 0.60, angle + angleOffsetB, newx, newy ] ) );
vector.push( new SeedCommand( seed1, new <Number>[ dotSize * 0.50, angle - angleOffsetB, newx, newy ] ) );
}
}
return vector;
}
}
}
class SeedCommand{
public function SeedCommand( func:Function, params:Vector.<Number> ){
this.func = func;
this.params = params;
}
private var func:Function;
private var params:Vector.<Number>;
public function execute():Vector.<SeedCommand>{
return func( params[0], params[1], params[2], params[3] );
}
}