forked from: CircleFitting
/**
* Copyright Thy ( http://wonderfl.net/user/Thy )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/csxE
*/
// 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
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);
i = -1;
while(++i<num)
{
while(!different(X = Math.random()*w,Y = Math.random()*h))
{
}
circ = new Circ(X,Y,1, Math.random()*.25+1);
circs[i] = circ;
}
// listener
stage.addEventListener(Event.ENTER_FRAME, ef)
}
private function different(X:Number, Y:Number):Boolean
{
var i:int = -1;
while(++i < this.i)
{
if(X == circs[i].x || Y == circs[i].y)
{
return false;
break;
}
}
return true;
}
//
private function ef(e:Event=null):void
{
// make circles grow
grow();
// draw stage
draw()
i=-1
while(++i < num)
{
if (circs[i].v != 0)
{
break;
}
}
if(i == num)
{
stage.removeEventListener(Event.ENTER_FRAME, ef)
//
D.applyFilter(D,D.rect,new Point(0,0),new BlurFilter(8,8,8))
D.colorTransform(D.rect,new ColorTransform(Math.random(),1,1))
D.draw(shape)
shape = null
}
}
private function grow():void
{
// temp var j
var j:int;
// loop
i=-1;
while(++i < num)
{
circ = circs[i];
if(circ.v != 0 || circs[num-1].v != 0)
{
circ.r += circ.v;
//
j = -1
while(++j < num)
{
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 < num)
{
circ = circs[i]
shape.graphics.drawCircle(circ.x,circ.y,circ.r)
}
//
D.draw(shape)
}
/*
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);
}
}
}
}
}
}
*/
}
}
class Circ
{
public var x:Number, y:Number, r:Number
public var v:Number
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
}
}