flash on 2014-11-5
♥0 |
Line 169 |
Modified 2014-11-06 03:50:06 |
MIT License
archived:2017-03-30 11:52:58
ActionScript3 source code
/**
* Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/6ceF
*/
package {
import flash.events.Event;
import flash.display.Sprite;
public class FlashTest extends Sprite {
public function FlashTest() {
vecRect = new Vector.<xRect>(0,false);
var i:int; var num:int; var a:xRect;
var kx:Number; var ky:Number;
kx =8; ky= 8;
for (i = 0; i < 64; i++)
{
kx += 32 + Math.random()*32;
if (kx >= 400) {kx=0;ky +=32+Math.random()*32;}
if (Math.random() < 0.5) {continue;}
a = new xRect();
a.cx = kx+Math.random()*32;
a.cy = ky+Math.random()*32;
a.cw=32+Math.random()*64;
a.ch=32+Math.random()*64;
vecRect.push(a);
}//nexti
conRect(); //connect rectangles with eachother
stage.addEventListener(Event.ENTER_FRAME, onEnter);
}//ctor
public var vecRect:Vector.<xRect>;
public var gt:int = 0;
public function onEnter(e:Event):void
{
graphics.clear();
graphics.lineStyle(2,0);
var a:xRect;
var mx:Number; var my:Number;
mx = stage.mouseX;
my = stage.mouseY;
cx = mx - cw*0.5;
cy = my - ch*0.5;
graphics.lineStyle(2,0xFF,0.5);
graphics.drawRect(cx,cy,cw,ch);
graphics.lineStyle(2,0);
graphics.beginFill(0,1);
graphics.drawCircle(mx,my,8);
graphics.endFill();
a = getFirstRectAt(mx,my);
if (a != null)
{
graphics.beginFill(0xFF,0.5);
graphics.drawRect(a.cx,a.cy, a.cw,a.ch);
graphics.endFill();
floodRect(a);
}//endif
var i:int; var num:int; //var a:xRect;
num = vecRect.length;
for (i = 0; i < num; i++)
{
a = vecRect[i];
graphics.drawRect(a.cx,a.cy,a.cw,a.ch);
graphics.drawCircle(a.cx+a.cw*0.5, a.cy+a.ch*0.5,4);
drawNear(a);
if (a.visFrame == gt)
{
graphics.beginFill(0xFF0000,0.25);
graphics.drawRect(a.cx,a.cy, a.cw,a.ch);
graphics.endFill();
}//endif
}//nexti
gt+=1;
}//onenter
public var vecTemp:Vector.<xRect> = new Vector.<xRect>(2048,false);
public function floodRect(a:xRect):void
{
var vec:Vector.<xRect>;
var it:int;
var vr:Vector.<xRect>; var num:int; var i:int;
var b:xRect;
vec = vecTemp;
vec[0] = a;
it = 1;
while (it > 0)
{
it -= 1;
a = vec[it];
vr = a.vecNext;
num = vr.length;
for (i = 0; i < num; i++)
{
b = vr[i];
if (b.visTest == gt) { continue; } //already tested this frame
b.visTest = gt;
if (isVis(b) == false) { continue; } //not visible
vec[it] = b;
b.visFrame = gt;
it += 1;
//for now this is the inf loop protection
//we dont expect more rectangles to be processed than this
if (it >= 2048) {it=2048; return;}
}//nexi
}//wend
}//floodrect
public var cx:Number = 0;
public var cy:Number = 0;
public var cw:Number = 128;
public var ch:Number =128;
public function isVis(a:xRect):Boolean
{
if (a.cx+a.cw < cx) {return false;}
if (a.cy+a.ch < cy) {return false;}
if (cx+cw < a.cx) {return false;}
if (cy+ch < a.cy) {return false;}
return true;
}//isvis
public function getFirstRectAt(ax:Number, ay:Number):xRect
{
var i:int; var num:int; var k:int; var a:xRect;
num = vecRect.length;
for (i = 0; i < num; i++)
{
a = vecRect[i];
if (ax < a.cx) {continue;}
if (ay < a.cy) {continue;}
if (ax > a.cx+a.cw) {continue;}
if (ay > a.cy+a.ch) {continue;}
return a;
}//nexti
return null;
}//getrect
public function drawNear(r:xRect):void
{
var vec:Vector.<xRect>; var a:xRect;
var i:int; var num:int;
vec = r.vecNext;
num = vec.length;
for (i = 0; i < num; i++)
{
a = vec[i];
graphics.moveTo(r.cx+r.cw*0.5,r.cy+r.ch*0.5);
graphics.lineTo(a.cx+a.cw*0.5,a.cy+a.ch*0.5);
}//nexti
}//near
public function conRect():void
{
var i:int; var num:int; var k:int;
var a:xRect; var b:xRect;
num = vecRect.length;
for (i = 0; i < num; i++)
{
a = vecRect[i];
for (k = i; k < num; k++)
{
b = vecRect[k];
if (a == b) { continue; } //test with self
//aabb overlap test
if (a.cx+a.cw < b.cx){continue;}
if (a.cy+a.ch < b.cy){continue;}
if (b.cx+b.cw < a.cx){continue;}
if (b.cy+b.ch < a.cy){continue;}
a.vecNext.push(b);
b.vecNext.push(a);
}//nextk
}//nexti
}//conrect
}//classend
}
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 visFrame:int = -1;
public var visTest:int = -1;
//pointer to neightbours/overlapping
public var vecNext:Vector.<xRect> = new Vector.<xRect>;
}//xrect