package{
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.ui.Mouse;
import flash.text.TextField;
import flash.utils.getTimer;
public class FlashTest extends Sprite{
public var circles:Number = 10;
public var maxx:Number = 100;
public var maxy:Number = 100;
public var maxsize:Number = 50;
public var minsize:Number = 30;
public var maxspeed:Number = 1;
public static const DEG_RAD:Number = Math.PI/180;
public var timer:Number = 0;
public var steps:Number = 360;
public var cloudcolor:uint = 0xcccccc;
public var linewidth:Number = 3;
public var easydraw:Boolean = false;
public var cloud:Sprite = new Sprite();
public var Circles:Vector.<Vector.<Number>> = new Vector.<Vector.<Number>>;
public var fps:TextField = new TextField();
public function FlashTest(){
addChild(cloud);
addChild(fps);
fps.x = -stage.stageWidth/2;
fps.y = -stage.stageHeight/2;
fps.height = 15;
fps.width = 50;
steps = 360/steps;
stage.addEventListener(Event.ENTER_FRAME,main);
stage.addEventListener(MouseEvent.MOUSE_DOWN,mousedown);
x = stage.stageWidth/2;
y = stage.stageHeight/2;
init();
}
public function init():void{
for(var i:int=0;i<circles;i++){
var W:Number = range(maxx,-maxx);
var H:Number = range(maxy,-maxy);
var S:Number = range(maxsize,minsize);
var VX:Number = range(maxspeed,-maxspeed);
var VY:Number = range(maxspeed,-maxspeed);
var v:Vector.<Number> = new Vector.<Number>;
v.push(W,H,S,Math.pow(S,2),VX,VY);
Circles.push(v);
}
}
public function main(e:Event):void{
var d:Number = getTimer();
fps.text = Math.round(1000/(d-timer)).toString()+" fps";
timer = d;
cloud.graphics.clear();
for(var i:int=0;i<circles;i++){
var a:Vector.<Number> = Circles[i];
a[0] += a[4];
a[1] += a[5];
if(a[0]+a[2] > stage.stageWidth/2){
a[0] = stage.stageWidth/2-a[2];
a[4] = -a[4];
}else if(a[0]-a[2] < -stage.stageWidth/2){
a[0] = -stage.stageWidth/2+a[2];
a[4] = -a[4];
}
if(a[1]+a[2] > stage.stageHeight/2){
a[1] = stage.stageHeight/2-a[2];
a[5] = -a[5];
}else if(a[1]-a[2] < -stage.stageHeight/2){
a[1] = -stage.stageHeight/2+a[2];
a[5] = -a[5];
}
}
render();
}
public function render():void{
for(var i:int=0;i<circles;i++){
with(cloud.graphics){
lineStyle(0,0x000000,0);
beginFill(cloudcolor);
drawCircle(Circles[i][0],Circles[i][1],Circles[i][2]);
endFill();
lineStyle(linewidth,0x000000,1);
}
var dx:Number=0;
var dy:Number=0;
var mark:Boolean = false;
for(var j:Number=0;j<=360;j+=steps){
var X:Number = Circles[i][0]+Math.cos(j*DEG_RAD)*Circles[i][2];
var Y:Number = Circles[i][1]+Math.sin(j*DEG_RAD)*Circles[i][2];
var h:Boolean = false;
for(var k:int=0;k<circles;k++){
if(i != k){
var d:Number = Math.pow(X-Circles[k][0],2)+Math.pow(Y-Circles[k][1],2);
if(d < Circles[k][3]){
k = circles;
h = true;
}
}
}
if(!h){
if(easydraw){
X = Circles[i][0]+Math.cos(j*DEG_RAD)*Circles[i][2];
Y = Circles[i][1]+Math.sin(j*DEG_RAD)*Circles[i][2];
with(cloud.graphics){
drawCircle(X,Y,1);
}
}else{
if(mark){
cloud.graphics.moveTo(dx,dy);
dx = X;
dy = Y;
cloud.graphics.lineTo(dx,dy);
}else{
dx = X;
dy = Y;
mark = true;
}
}
}else{
mark = false;
}
}
}
}
public function mousedown(e:MouseEvent):void{
reset();
render();
}
public function reset():void{
cloud.graphics.clear();
Circles = new Vector.<Vector.<Number>>();
init();
}
public function range(M:Number,m:Number=0):Number{
return Math.random()*(M-m)+m;
}
}
}