flash on 2014-10-12
/**
* Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/6tcb
*/
package {
import flash.events.MouseEvent;
import flash.events.Event;
import flash.display.Sprite;
public class FlashTest extends Sprite {
public function FlashTest() {
tempRect.cx = 64; tempRect.cy = 64;
tempRect.cw = 128; tempRect.ch = 128;
tempRect2.cw = 48; tempRect2.ch = 48;
stage.addEventListener(MouseEvent.CLICK, onClick);
stage.addEventListener(Event.ENTER_FRAME, onEnter);
}//ctor
public function onClick(e:MouseEvent):void
{
tempRect.cx = 32+Math.random()*200;
tempRect.cy = 32+Math.random()*200;
tempRect.cw = 16 + Math.random()*160;
tempRect.ch = 16 + Math.random()*160;
tempRect2.cw = 8 + Math.random()*160;
tempRect2.ch = 8 + Math.random()*160;
}//onclick
public var tempRect:xRect = new xRect();
public var tempRect2:xRect = new xRect();
public function onEnter(e:Event):void
{
var mx:Number; var my:Number;
mx = stage.mouseX;
my = stage.mouseY;
var a:xRect; var b:xRect;
a = tempRect;
b = tempRect2;
//a.cx = 64; a.cy = 64;
//a.cw = 128; a.ch = 128;
b.cx = mx; b.cy = my;
//b.cw = 48; b.ch = 48;
//b.cw = 160;
//b.ch = 160;
graphics.clear();
graphics.lineStyle(2, 0);
graphics.drawRect(a.cx,a.cy,a.cw,a.ch);
graphics.beginFill(0,0.5);
graphics.drawRect(b.cx,b.cy,b.cw,b.ch);
graphics.endFill();
cutRect(a, b);
}//onenter
//actually just draw the results for now
//a rect to cut from
//b rect used to cut
public function cutRect(a:xRect, b:xRect):void
{
var ax:Number; var ay:Number;
var aw:Number; var ah:Number;
//check overlap
if (a.cx + a.cw < b.cx ) { return; }
if (a.cy + a.ch < b.cy ) { return; }
if (b.cx + b.cw < a.cx ) { return; }
if (b.cy + b.ch < a.cy ) { return; }
//check if a inside b
if (a.cx > b.cx)
if (a.cx +a.cw < b.cx+b.cw)
if (a.cy > b.cy)
if (a.cy +a.ch < b.cy+b.ch)
{
graphics.lineStyle(4, 0xFF0000);
graphics.drawRect(a.cx,a.cy,a.cw,a.ch);
return;
}//endif
//top
if (b.cy > a.cy)
{
ax = a.cx; ay = a.cy;
aw = a.cw; ah = b.cy - ay;
graphics.lineStyle(2, 0xFF0000);
graphics.beginFill(0xFF0000,0.5); graphics.drawRect(ax, ay, aw, ah); graphics.endFill();
}//endif
//bottom
if ((b.cy+b.ch) < (a.cy + a.ch))
{
ax = a.cx; ay = b.cy+b.ch;
aw = a.cw; ah = ((a.cy+a.ch) - ay);
graphics.lineStyle(2, 0xFF);
graphics.beginFill(0xFF,0.5); graphics.drawRect(ax, ay, aw, ah); graphics.endFill();
}//endif
//left
if (b.cx > a.cx)
{
ax = a.cx; ay = b.cy;
aw = b.cx - a.cx; ah = b.ch;
if (ay < a.cy) { ah = b.ch - (a.cy - ay); ay = a.cy; }
if ((ay + ah) > (a.cy + a.ch)) { ah = (a.cy + a.ch)-ay; }
graphics.lineStyle(2, 0x0FFF0F);
graphics.beginFill(0x0FFF0F,0.5); graphics.drawRect(ax, ay, aw, ah); graphics.endFill();
}//endif
//right
if (b.cx+b.cw < (a.cx + a.cw) )
{
ax = b.cx+b.cw; ay = b.cy;
aw = (a.cx+a.cw) - (b.cx+b.cw); ah = b.ch;
if (ay < a.cy) { ah = b.ch - (a.cy - ay); ay = a.cy; }
if ((ay + ah) > (a.cy + a.ch)) { ah = (a.cy + a.ch)-ay; }
graphics.lineStyle(2, 0xFF00FF);
graphics.beginFill(0xFF00FF,0.5); graphics.drawRect(ax, ay, aw, ah); graphics.endFill();
}//endif
//intersection
ax = Math.max(a.cx,b.cx);
ay = Math.max(a.cy,b.cy);
aw = Math.min(a.cx+a.cw,b.cx+b.cw) - ax;
ah = Math.min(a.cy+a.ch, b.cy+b.ch) - ay;
graphics.lineStyle(2, 0x0);
graphics.beginFill(0xFFFaaF,0.5); graphics.drawRect(ax, ay, aw, ah); graphics.endFill();
}//cutrect
}//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