/**
* Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/p8uq
*/
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() {
deb = new TextField();
deb.mouseEnabled = false;
deb.width = 320;
deb.height = 240;
addChild(deb);
vecPnt = new Vector.<xPnt>(0,false);
var i:int;
var a:xPnt;
for (i = 0; i < 32; i++)
{
a = new xPnt();
vecPnt[i] = a;
a.cx = Math.random()*400 +40;
a.cy = Math.random()*400+40;
}//nexti
stage.addEventListener(MouseEvent.CLICK, onClick);
stage.addEventListener(Event.ENTER_FRAME, onEnter);
}//ctor
public var deb:TextField;
public var vecPnt:Vector.<xPnt>;
public var cx:Number = 230;
public var cy:Number = 230;
public function onClick(e:MouseEvent):void
{
cx = stage.mouseX; cy= stage.mouseY;
}//onclick
public function onEnter(e:Event):void
{
graphics.clear();
graphics.lineStyle(2, 0);
var mx:Number; var my:Number;
var ax:Number; var ay:Number;
var ang:Number;
mx = stage.mouseX; my = stage.mouseY;
// ax = 230; ay = 230;
ax =cx; ay =cy;
ang = Math.atan2(my-ay,mx-ax);
graphics.drawCircle(ax,ay,16);
graphics.moveTo(ax,ay);
graphics.lineTo(ax+Math.cos(ang)*16,ay+Math.sin(ang)*16);
/*
var bx:Number; var by:Number;
bx = 100; by = 100;
graphics.drawCircle(bx,by,4);
*/
var bm:Number; var bm2:Number;
var nm:Number;
var nx:Number; var ny:Number;
var nx2:Number; var ny2:Number;
nm = 0.2;
nx = Math.cos(ang+nm); ny = Math.sin(ang+nm);
graphics.moveTo(ax,ay);
graphics.lineTo(ax+nx*512,ay+ny*512);
graphics.moveTo(ax+nx*128,ay+ny*128);
graphics.lineTo(ax+nx*128+ny*16,ay+ny*128+nx*-16);
nx2 = Math.cos(ang-nm); ny2 = Math.sin(ang-nm);
graphics.moveTo(ax,ay);
graphics.lineTo(ax+nx2*512,ay+ny2*512);
graphics.moveTo(ax+nx2*128,ay+ny2*128);
graphics.lineTo(ax+nx2*128+ny2*-16,ay+ny2*128+nx2*16);
/*
bm = (bx-ax)*ny + (by-ay)*-nx;
bm2 = (bx-ax)*-ny2 + (by-ay)*nx2;
deb.text = ""+bm+"\n"+bm2;
if (bm > 0 && bm2 > 0)
{
graphics.drawCircle(bx,by, 8);
}//endif
*/
var d:Number;
var dist:Number;
var sel:xPnt;
dist = 999999;
sel = null;
var i:int; var num:int; var a:xPnt;
num = vecPnt.length;
for (i = 0; i <num; i++)
{
a = vecPnt[i];
graphics.drawCircle(a.cx,a.cy,6);
bm = (a.cx-ax)*ny + (a.cy-ay)*-nx;
bm2 = (a.cx-ax)*-ny2 + (a.cy-ay)*nx2;
if (bm < 0 || bm2 < 0) { continue; }
graphics.drawCircle(a.cx,a.cy,10);
d = getDist(a.cx-ax,a.cy-ay);
if (d > dist) { continue; }
dist = d;
sel = a;
}//nexti
if (sel != null)
{
graphics.moveTo(ax,ay);
graphics.lineTo(sel.cx,sel.cy);
graphics.beginFill(0,1);
graphics.drawCircle(sel.cx,sel.cy,7);
graphics.endFill();
}//endif
}//onenter
public function getDist(ax:Number,ay:Number):Number
{ return Math.sqrt(ax*ax+ay*ay); }
}//classend
}
internal class xPnt
{
public var cx:Number = 0;
public var cy:Number = 0;
}//classend