snowflake fractal
/**
* Copyright Highly ( http://wonderfl.net/user/Highly )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/39hK
*/
// forked from Highly's text and timer
package {
import flash.utils.Proxy;
import flash.display.Sprite;
import flash.text.TextField;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.Event;
import flash.geom.Point;
public class FlashTest extends Sprite {
public function FlashTest() {
var ti:Timer = new Timer(500,0);
ti.addEventListener(TimerEvent.TIMER,tmr);
ti.start();
var outMod:int = 5;
var sw:int = stage.stageWidth;
var sh:int = stage.stageHeight;
var out1:TextField = new TextField();
out1.x=outMod;
out1.y=outMod;
out1.selectable = false;
out1.multiline = true;
out1.wordWrap = true;
out1.width = sw-(outMod*2);
out1.height = sh-(outMod*2);
out1.text = "";
addChild(out1);
var gfx:Sprite = new Sprite();
addChild(gfx);
var i:int=0;
var aMod:int= 50;
var res:int = 5;
var arr:Array = [new Point(aMod,sh-aMod),new Point(sw-aMod,aMod)];
var tArr:Array = new Array();
tArr = arr;
drawLines(arr);
var k:int=0;
function tmr(e:TimerEvent):void{
if(k<res){
tArr = fract(tArr);
++k;
}else{
k=0;
tArr=arr;
}
gfx.graphics.clear();
drawLines(tArr);
}
function fract(a:Array):Array{
var fa:Array = new Array();
var ang:Number;
var mp:Point = new Point();
var tp:Point = new Point();
var d:Number;
if(a.length>1){
for(var i:int = 0;i<a.length-1;++i){
var ta:Array = new Array();
mp = new Point();
tp = new Point();
mp.x=((a[i+1].x-a[i].x)/2)+a[i].x;
mp.y=((a[i+1].y-a[i].y)/2)+a[i].y;
ang = Math.atan2(a[i+1].y-a[i].y,a[i+1].x-a[i].x);
ang -= Math.PI/2;
d = Math.sqrt(Math.pow(a[i+1].x-a[i].x,2)+Math.pow(a[i+1].y-a[i].y,2))/4;
tp.x = (d*Math.cos(ang))+mp.x;
tp.y = (d*Math.sin(ang))+mp.y;
ta[0] = a[i];
ta[1] = new Point(((mp.x-a[i].x)/2)+a[i].x,((mp.y-a[i].y)/2)+a[i].y);
ta[2] = tp;
ta[3] = new Point(((a[i+1].x-mp.x)/2)+mp.x,((a[i+1].y-mp.y)/2)+mp.y);
ta[4] = a[i+1];
fa = fA_push(fa,ta);
}
}
return fa;
}
function fA_push(f:Array,a:Array):Array{
var t:Array=new Array();
if(f.length>0){
//copy f to t
for(var i:int=0;i<f.length;++i){
t.push(f[i]);
}
for(i=1;i<a.length;++i){
t.push(a[i]);
}
}else{
for(i=0;i<a.length;++i){
t.push(a[i]);
}
}
return t;
}
function drawLines(a:Array):void{
gfx.graphics.lineStyle(0.5,0x000000);
if(a.length>1){
for(var i:int = 0;i<a.length;++i){
if(i==0){
gfx.graphics.moveTo(a[i].x,a[i].y);
}else{
gfx.graphics.lineTo(a[i].x,a[i].y);
gfx.graphics.moveTo(a[i].x,a[i].y);
}
}
}
};//end drawLines
}
}
}