CircleFitting
/**
* Copyright shapevent ( http://wonderfl.net/user/shapevent )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/hZCr
*/
package {
import flash.display.*;
import flash.events.*;
public class CircleFitting extends MovieClip {
private var circs:Array;
private var circNum:int;
public function CircleFitting(){
// init
circs = []
circNum = 600;
addEventListener(Event.ENTER_FRAME, onAdd);
}
// private methods
private function onAdd(evt:Event):void {
if (circs.length < circNum){
makeGrowable();
}
}
private function makeGrowable():void{
var s:MovieClip = MovieClip(addChild(new MovieClip()));
s.x = Math.random() * stage.stageWidth;
s.y = Math.random() * stage.stageHeight;
with(s.graphics){
lineStyle(0,0);
drawCircle(0,0,10);
}
s.scaleX = s.scaleY = 0;
circs.push(s);
s.addEventListener(Event.ENTER_FRAME, onScaleUp);
}
private function onScaleUp(evt:Event):void {
var c:MovieClip = MovieClip(evt.currentTarget);
c.scaleX = c.scaleY += 0.05;
for (var i:int = 0; i<circs.length; i++){
var circ:MovieClip = circs[i];
if (circ != c){
var amt:Number = circ.width/2 + c.width/2;
var dx:Number = circ.x - c.x;
var dy:Number = circ.y - c.y;
var dist:Number = Math.sqrt(dx * dx + dy * dy);
if (amt > dist){
c.removeEventListener(Event.ENTER_FRAME, onScaleUp);
if (c.scaleX < 0.1){
if (contains(c)){
removeChild(c);
}
}
}
}
}
}
}
}