/**
* Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/hPEM
*/
package {
import flash.events.Event;
import flash.display.Sprite;
public class FlashTest extends Sprite {
public function FlashTest() {
myRect = new xRect();
myRect.cx=16; myRect.cy=16;
myRect.cw=384; myRect.ch=384;
stage.addEventListener(Event.ENTER_FRAME, onEnter);
}//ctor
public var myRect:xRect;
public var gt:int = 0;
public function onEnter(e:Event):void
{
graphics.clear();
graphics.lineStyle(2, 0);
var ax:Number; var ay:Number;
var aw:Number; var ah:Number;
aw = 64; ah = 64;
ax = Math.random()*384; ay = Math.random()*384;
if ((gt & 32) > 0)
{ myRect.remove(ax,ay,aw,ah, aw*0.5); }
else {
myRect.remove(ax,ay,aw,ah, aw*0.5);
myRect.add(ax,ay,aw,ah, aw*0.5); }
myRect.render(graphics);
gt += 1;
}//onenter
}//classend
}
import flash.display.Graphics;
internal class xRect
{
public var cx:Number = 0; public var cy:Number = 0;
public var cw:Number = 0; public var ch:Number = 0;
public var depth:int = 0;
public var parent:xRect = null;
public var child:Vector.<xRect> = null;
public var bSolid:Boolean = true;
public function genChild():void
{
bSolid = false;
if (depth >= 6) { return; }
if (child != null) { return; }
var i:int; var a:xRect; var sw:Number; var sh:Number;
child = new Vector.<xRect>(4, false);
sw=cw*0.5; sh=ch*0.5;
for (i = 0; i < 4; i++)
{
a = new xRect(); child[i] = a;
a.cx = cx; a.cy = cy; a.cw = sw; a.ch = sh;
a.depth = depth + 1; a.parent = this; a.bSolid = true;
}//nexti
child[1].cx += sw; child[2].cy += sh;
child[3].cx += sw; child[3].cy += sh;
}//genchild
public function drawRect(g:Graphics):void
{
g.beginFill(0, 0.2+0.1*depth);
g.drawRect(cx,cy, cw,ch);
g.endFill();
}//drawrect
public function render(g:Graphics):void
{
//drawRect(g);
if (bSolid) { drawRect(g); return; }
if (child == null) { return; }
var i:int; for (i = 0; i < 4; i+=1) { child[i].render(g); }
}//render
public function remove(ax:Number,ay:Number,aw:Number,ah:Number, r:Number):void
{
if (cx+cw < ax) { return; } if (cy+ch < ay) { return; }
if (ax+aw < cx) { return; } if (ay+ah < cy) { return; }
if (getMagRect(ax+aw*0.5,ay+ah*0.5) > r) { return; }
bSolid = false;
if (child == null) { genChild(); }
if (child == null) { return; }
var i:int; var a:xRect;
for (i = 0; i < 4; i++) { a = child[i]; a.remove(ax,ay,aw,ah, r); }
}//explode
//not works too well yet
public function add(ax:Number,ay:Number,aw:Number,ah:Number, r:Number):void
{
if (child != null)
{
var i:int; var a:xRect; var k:int; k = 0;
for (i = 0; i < 4; i++) { a = child[i]; a.add(ax,ay,aw,ah, r); if (a.bSolid) {k+=1;} }
if (k >= 4) { bSolid = true; }
return;
}
if (bSolid) { return; }
if (cx+cw < ax) { return; } if (cy+ch < ay) { return; }
if (ax+aw < cx) { return; } if (ay+ah < cy) { return; }
if (getMagRect(ax+aw*0.5,ay+ah*0.5) > r) { return; }
bSolid = true;
}//add
public function getMagRect(ax:Number, ay:Number):Number
{
var ix:Number; var iy:Number;
if (ax < cx) { ix = cx;} else if (ax > cx+cw) { ix = cx+cw; } else { ix = ax; }
if (ay < cy) { iy = cy;} else if (ay > cy+ch) { iy = cy+ch; } else { iy = ay; }
return getMag(ix-ax, iy-ay);
}//getmagrect
public function getMag(ax:Number, ay:Number):Number
{ return Math.sqrt(ax*ax+ay*ay); }
}//xrect