forked from: forked from: まるをしきつめる
/**
* Copyright Thy ( http://wonderfl.net/user/Thy )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/zgaG
*/
// forked from Thy's forked from: まるをしきつめる
// forked from nemu90kWw's まるをしきつめる
package {
import flash.display.*;
import flash.events.*;
import flash.geom.Point;
import net.hires.debug.Stats;
public class FlashTest extends Sprite
{
private var
num:int = 150, // how many circles
gridSize:Number = 50, // the circle max size
circles:Vector.<Circle> = new Vector.<Circle>(num, true),
// an grid
grid:Vector.<Vector.<Vector.<Circle>>>;
public function FlashTest() {
graphics.beginFill(0xD0D0FF);
graphics.drawRect(0, 0, 465, 465);
graphics.endFill();
stage.frameRate = 120;
var i:int = -1;
while(++i < num)
{
circles[i] = new Circle(465/2+Math.random()*100-50,
465/2+Math.random()*100-50,
Math.random()*Math.random()*50);
this.addChild(circles[i]);
}
stage.addEventListener(MouseEvent.CLICK, onClick);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
addChild(new Stats());
}
private function onClick(e:MouseEvent):void
{
var // create circles
i:int = -1,
circle:Circle;
while(++i < num)
{
circle = circles[i];
circle.x = mouseX + Math.random()*100 - 50;
circle.y = mouseY + Math.random()*100 - 50;
}
}
private function onEnterFrame(e:Event):void
{
//gride();
var // variables
i:int = -1, k:int, l:int = num -1,
c1:Circle, c2:Circle,
dx:Number, dy:Number, d:Number,
angle:Number, multiplier:Number, cos:Number, sin:Number;
// Separate circles in grid
while(++i < l)
{
k = i;
while(++k < num)
{
c1 = circles[i];
c2 = circles[k];
dx = c1.x - c2.x;
dy = c1.y - c2.y;
d = Math.sqrt((dx*dx) + (dy*dy));
if(d < c1.size+c2.size)
{
cos = dx / d;
sin = dy / d;
multiplier = (c1.size + c2.size - d) * .5;
c1.x += cos * multiplier;
c1.y += sin * multiplier;
c2.x -= cos * multiplier;
c2.y -= sin * multiplier;
}
}
}
}
// the grid. inside the drig, we find the circles
private function gride():void
{
var tx:int, ty:int, minX:int, minY:int;
var maxX:int, maxY:int;
var c:Circle = circles[0];
minX = maxX = tx = c.tx = c.x/50;
minY = maxY = ty = c.ty = c.y/50;
// see the circle position (in grids)
var i:int=0;
while(++i<num)
{
c = circles[i];
c.tx = tx = c.x / 50;
c.ty = ty = c.y / 50;
if(tx < minX) minX = tx;
if(tx > maxX) maxX = tx;
if(ty < minY) minY = ty;
if(ty > maxY) maxY = ty;
}
// make the grid
var j:int = maxY - minY;
grid = new Vector.<Vector.<Vector.<Circle>>>(j);
i = -1;
while(++i < j)
{
grid.push(new Vector.<Vector.<Circle>>(maxX - minX));
}
// adjust circle position (in grids)
i = -1;
while(++i<num)
{
c = circles[i];
grid[c.ty - minY][c.tx - minX].push(c);
}
}
}
}
import flash.display.*;
class Circle extends Shape
{
public var
size:Number, tx:int, ty:int;
function Circle(x:Number, y:Number, size:Number)
{
this.x = x;
this.y = y;
this.size = size;
graphics.beginFill(0xFFA000);
graphics.lineStyle(2, 0x402000);
graphics.drawCircle(0, 0, size);
graphics.endFill();
this.cacheAsBitmap = true;
}
}