In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

forked from: forked from: CircleFitting

Get Adobe Flash player
by Thy 03 Sep 2010
/**
 * Copyright Thy ( http://wonderfl.net/user/Thy )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/ezsYg
 */

// forked from Thy's forked from: CircleFitting
// forked from shapevent's CircleFitting
package 
{
       import flash.display.Sprite;
       import flash.events.Event;
       import flash.display.Bitmap;
       import flash.display.BitmapData;
       import flash.display.Shape;
       
       import flash.geom.Point;
       import flash.filters.BlurFilter;
       import flash.geom.ColorTransform;
       public class CircleFitting extends Sprite 
       {
            private var circs:Vector.<Circ>;
            private var circ:Circ; // temp circ
            private var num:int;
            //
            private var B:Bitmap;
            private var D:BitmapData;
            private var shape:Shape
            // temp vars
            private var i:int;
            private var X:Number;
            private var Y:Number;
            //
            private var w:Number = stage.stageWidth;
            private var h:Number = stage.stageHeight;

             public function CircleFitting()
             {
                 // init
                 Wonderfl.capture_delay( 15 )
                 stage.frameRate = 600
                 k = -1 // number of created circles
                 num = 600;
                 shape = new Shape()
                 D = new BitmapData(w,h,false,0xFFFFFF)
                 B = new Bitmap(D, "auto", true)
                 stage.addChild(B)
                 // make circles
                 circs = new Vector.<Circ>(num);
                 // listener
                 stage.addEventListener(Event.ENTER_FRAME, ef)
               }
               
               private function different(X:Number, Y:Number):Boolean
               {
                       var i:int = -1;
                       while(++i < k)
                       {
                           var c:Circ = circs[i];
                           var amt:Number = c.r;
                           var dx:Number = c.x - X;
                           var dy:Number = c.y - Y;
                           if( amt * amt > dx*dx + dy*dy)
                           {
                               return false;
                               break;
                           }
                           
                       }
                       return true;
               }
               
               //
               private var k:int
               private function ef(e:Event=null):void
               {
                       // crate a circle
                       ++k;
                       if(k < num)
                       {
                         while(!different(X = Math.random()*w,Y = Math.random()*h))
                         {
                         }
                         circ = new Circ(X,Y,1, Math.random()*2+1);
                         circs[k] = circ;
                       } else 
                       {
                           k = num
                       }
                       
                       // make circles grow
                       grow();
                       // draw stage
                       draw()
                       // stop listener
                       i=-1
                       while(++i < num)
                       {
                           if (circs[i].v != 0)
                           {
                               break;
                           }
                       }
                       if(i == num)
                       {
                           stage.removeEventListener(Event.ENTER_FRAME, ef)
                           stage.frameRate = 0
                           //
                           D.applyFilter(D,D.rect,new Point(0,0),new BlurFilter(16,16,3))
                           D.colorTransform(D.rect,new ColorTransform(Math.random()+1,Math.random()+1,Math.random()+1))
                           //
                           shape.graphics.clear()
                           shape.graphics.lineStyle(1,0, .1)
                           i=-1
                           while(++i < k)
                           {
                               circ = circs[i]
                               shape.graphics.drawCircle(circ.x,circ.y,circ.r)
                           }
                           D.draw(shape)
                           shape = null
                       }
               }
               
               
               private function grow():void
               {
                       // temp var j
                       var j:int;
                       // loop
                       i=-1;
                       while(++i < k)
                       {
                           circ = circs[i];
                           if(circ.v != 0 || circs[k-1].v != 0)
                           {
                               circ.r += circ.v;
                               //
                               j = -1
                               while(++j < k)
                               {
                                   if(j==i) ++j
                                   
                                   var c:Circ = circs[j];
                                   var amt:Number = c.r + circ.r;
                                   var dx:Number = c.x - circ.x;
                                   var dy:Number = c.y - circ.y;
                                   if( amt * amt > dx*dx + dy*dy)
                                   {
                                       c.v = 0
                                       circ.v = 0
                                   }
                               
                               }
                           }
                       }
               }
               
               private function draw():void
               {
                       shape.graphics.clear()
                       shape.graphics.lineStyle(1,0)
                       i=-1
                       while(++i < k)
                       {
                           circ = circs[i]
                           if(!circ.stop)
                           {
                               shape.graphics.drawCircle(circ.x,circ.y,circ.r)
                               if(circ.v == 0)circ.stop = true
                           }
                           
                       }
                       //
                       D.draw(shape)        
               }
       }
}

class Circ
{
    
    public var x:Number = 0, y:Number = 0, r:Number = 0
    public var v:Number = 0
    public var stop:Boolean
    public function Circ(x:Number = 0,y:Number = 0,r:Number = 0,v:Number = 0)
    {
        this.x = x
        this.y = y
        this.r = r
        this.v = v
    }
}