/**
* Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/n5Ja
*/
package {
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.events.Event;
import flash.display.Sprite;
public class FlashTest extends Sprite {
public function FlashTest() {
// write as3 code here..
//debugshape
vecLine = new Vector.<xLine>(0,false);
vecLine.push(new xLine(160, 160, 240, 160));
vecLine.push(new xLine(240, 160, 240, 200));
vecLine.push(new xLine(240, 200, 240, 240));
vecLine.push(new xLine(240, 240, 160, 240));
vecLine.push(new xLine(160, 240, 160, 160));
genShape();
deb = new TextField();
deb.mouseEnabled = false;
deb.width = 320;
deb.height = 240;
addChild(deb);
stage.addEventListener(MouseEvent.MOUSE_DOWN, mdown);
stage.addEventListener(Event.ENTER_FRAME, onEnter);
}//ctor
public function mdown(e:MouseEvent):void
{
genShape();
}//mdown
public var vecLine:Vector.<xLine>;
public var deb:TextField;
public function genShape():void
{
vecLine = new Vector.<xLine>(0,false);
graphics.lineStyle(2,0);
var ang:Number;
var kx:Number;
var ky:Number;
var px:Number;
var py:Number;
var d:Number;
var nx:Number;
var ny:Number;
var mag:Number;
px = 160;
py = 40;
for (ang = 0; ang < 6.28; ang+=0.5)
{
d = (Math.random()*60 + 30);
kx = 160 + Math.cos(ang) * d;
ky = 160 + Math.sin(ang) * d;
if (ang == 0) { px =kx; py= ky; continue; }
//graphics.drawCircle(kx,ky,8);
graphics.moveTo(px,py);
graphics.lineTo(kx,ky);
vecLine.push(new xLine(px,py,kx,ky) );
nx = -(py - ky);
ny = px - kx;
mag = Math.sqrt(nx*nx+ny*ny);
if (mag == 0) { mag =0.00001;}
nx /= mag;
ny /= mag;
graphics.moveTo(px,py);
graphics.lineTo(px+nx*8,py+ny*8);
px = kx;
py = ky;
}//nextang
// vecLine.push(new xLine(vecLine[0].x0,vecLine[0].y0, kx,ky));
vecLine.push(new xLine(kx,ky,vecLine[0].x0,vecLine[0].y0));
graphics.moveTo(vecLine[0].x0,vecLine[0].y0);
graphics.lineTo(kx,ky);
}//genshape
public function onEnter(e:Event):void
{
graphics.clear();
var mx:Number;
var my:Number;
mx = stage.mouseX;
my = stage.mouseY;
// my = 200; //debug
graphics.lineStyle(1,0);
graphics.drawCircle(mx,my,4);
graphics.moveTo(mx-500, my);
graphics.lineTo(mx+500, my);
var a:xLine;
var i:int;
var num:int;
var w:int;
w = 0;
num = vecLine.length;
deb.text = "";
for (i = 0; i < num; i++)
{
a = vecLine[i];
graphics.lineStyle(2, 0);
graphics.moveTo(a.x0, a.y0);
graphics.lineTo(a.x1, a.y1);
graphics.moveTo(a.cx,a.cy);
graphics.lineTo(a.cx+a.nx*8,a.cy+a.ny*8);
if (a.y0 <= my && a.y1 <= my) { continue; }
if (a.y0 > my && a.y1 > my) { continue; }
//if (a.x0 > mx && a.x1 > mx) { continue; }
var wz:Number;
var ax:Number;
var ay:Number;
var bx:Number;
var by:Number;
ax = mx - a.x0;
ay = my - a.y0;
bx = a.x1 - a.x0;
by = a.y1 - a.y0;
wz = (ax*by)-(ay*bx);
//deb.appendText(" wz "+ wz);
var c:uint;
if (wz > 0) { c = 0x0000FF; w+= 1;}
else if (wz < 0) { c = 0xFF0000; w -= 1;}
graphics.lineStyle(4, c);
graphics.moveTo(a.x0, a.y0);
graphics.lineTo(a.x1, a.y1);
}//nexti
deb.text = " "+w;
//note --
//still has false detection
//when the line goes through a point
//tried to fix it by checking for a sign differently
//im not 100% sure it fixed it for all cases
if (w != 0)
{
graphics.drawCircle(mx,my,8);
}//endif
if (w == 0) { c = 0;}
else {c =0xFF0000;}
//if (w == 0)
{
graphics.beginFill(c,0.5);
graphics.lineStyle();
for (i = 0; i < num; i++)
{
a = vecLine[i];
if (i == 0)
{ graphics.moveTo(a.x0, a.y0);
graphics.lineTo(a.x1, a.y1);
}
else
{ graphics.lineTo(a.x1, a.y1);}
}//nexti
a = vecLine[0];
graphics.lineTo(a.x0, a.y0);
graphics.endFill();
}//endif
}//onenter
}//classend
}
internal class xLine
{
public var x0:Number = 0;
public var y0:Number = 0;
public var x1:Number = 0;
public var y1:Number = 0;
public function xLine(ax:Number,ay:Number, bx:Number, by:Number):void
{
x0 = ax; y0 = ay; x1 = bx; y1 = by;
//calc normal (and midpoint) only needed for debugging
var mag:Number;
nx = -(y0 - y1);
ny = (x0 - x1);
mag = Math.sqrt(nx*nx+ny*ny);
if (mag == 0) { mag = 0.00001;}
nx /= mag;
ny /= mag;
cx = x0 +(x1-x0)*0.5;
cy = y0 +(y1-y0)*0.5;
}//xline
public var cx:Number = 0;
public var cy:Number = 0;
public var nx:Number = 0;
public var ny:Number = 0;
}//xline