/**
* Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/uQED
*/
package {
import flash.text.TextField;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.display.Sprite;
public class FlashTest extends Sprite {
public function FlashTest() {
deb = new TextField();
deb.width=320; deb.height=240; deb.mouseEnabled=false;
addChild(deb);
//cx = 64; cy = 64;
var a:xRect;
a = new xRect(); a.cx = 64; a.cy =64; a.cw = 256; a.ch = 256; vecRect.push(a);
/*
var a:xRect; var i:int;
var ww:Number; var wh:Number;
ww = cw*0.5; wh = ch*0.5;
a = new xRect(); a.cx = cx; a.cy = cy; a.cw = ww; a.ch = wh; vecRect.push(a);
a = new xRect(); a.cx = cx; a.cy = cy; a.cw = ww; a.ch = wh; vecRect.push(a); a.cx += ww;
a = new xRect(); a.cx = cx; a.cy = cy; a.cw = ww; a.ch = wh; vecRect.push(a); a.cy += wh;
a = new xRect(); a.cx = cx; a.cy = cy; a.cw = ww; a.ch = wh; vecRect.push(a); a.cx += ww; a.cy+=wh;
*/
stage.addEventListener(MouseEvent.CLICK, onClick);
stage.addEventListener(Event.ENTER_FRAME, onEnter);
}//ctor
public var deb:TextField;
public function onClick(e:MouseEvent):void
{
// if (vecRect.length < 120) { divRect(vecRect[1] ); }
var r:Number; r = 32;
remArea(stage.mouseX-r,stage.mouseY-r,r*2,r*2, r);
deb.text = ""+vecRect.length;
}//onclick
//point distance to rect
public function getMagRect(a:xRect, ax:Number, ay:Number):Number
{
var ix:Number; var iy:Number;
if (ax < a.cx) { ix = a.cx;}
else if (ax > a.cx+a.cw) { ix = a.cx+a.cw; } else { ix = ax; }
if (ay < a.cy) { iy = a.cy;}
else if (ay > a.cy+a.ch) { iy = a.cy+a.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);}
public function remArea(ax:Number,ay:Number,aw:Number,ah:Number, arad:Number):void
{
var i:int; var num:int; var a:xRect;
var kx:Number; var ky:Number;
kx = ax+aw*0.5; ky = ay+ah*0.5;
num = vecRect.length;
var k:int; k = 0;
for (i = 0; i < num; i++)
{
a = vecRect[i];
if (a.cx + a.cw < ax) { continue ; }
if (a.cy + a.ch < ay) { continue ; }
if (a.cx > ax+aw) { continue ; }
if (a.cy > ay+ah) { continue ; }
if (getMagRect(a, kx,ky) > arad) { continue; }
divRect(a);
if (vecRect.length != num) //added new rects? go back 1
{
num = vecRect.length; i -=1;
k+=1; if (k > 1024) { return; } //infiny loop protection (i dont trust myself very much)
// deb.text = ""+k;
}//endif
}//nexti
}//remarea
/*
public var cx:Number = 0;
public var cy:Number = 0;
public var cw:Number = 256;
public var ch:Number = 256;
*/
public var vecRect:Vector.<xRect> = new Vector.<xRect>(0, false);
public function remRect(i:int):void
{
var num:int;
num = vecRect.length - 1;
for (i; i < num; i++)
{ vecRect[i] = vecRect[i+1];}
vecRect.pop();
}//rem
public var minx:Number = 8; // 16;
public function divRect(r:xRect):void
{
var i:int; i = vecRect.indexOf(r); if (i < 0) { return; } remRect(i);
//dont make new ones -- it was too small
if (r.cw <= minx || r.ch <= minx) { return; }
var ww:Number; var wh:Number; ww = r.cw*0.5; wh = r.ch*0.5;
var a:xRect;
a = new xRect(); a.cx = r.cx; a.cy = r.cy; a.cw = ww; a.ch = wh; vecRect.push(a);
a = new xRect(); a.cx = r.cx; a.cy = r.cy; a.cw = ww; a.ch = wh; vecRect.push(a); a.cx += ww;
a = new xRect(); a.cx = r.cx; a.cy = r.cy; a.cw = ww; a.ch = wh; vecRect.push(a); a.cy += wh;
a = new xRect(); a.cx = r.cx; a.cy = r.cy; a.cw = ww; a.ch = wh; vecRect.push(a); a.cx += ww; a.cy+=wh;
}//breakrect
public function onEnter(e:Event):void
{
var mx:Number; var my:Number; var mrad:Number;
mx = stage.mouseX; my = stage.mouseY; mrad = 32;
graphics.clear();
graphics.lineStyle(2, 0);
var i:int; var num:int; var a:xRect;
num = vecRect.length;
for (i = 0; i < num; i++)
{
a = vecRect[i];
graphics.beginFill(0, 0.5);
graphics.drawRect(a.cx,a.cy,a.cw,a.ch);
graphics.endFill();
}//nexti
graphics.drawCircle(mx, my, mrad);
}//onenter
}//classend
}
internal class xRect
{
public var cx:Number = 0;
public var cy:Number = 0;
public var cw:Number = 0;
public var ch:Number = 0;
}//xrect