flash on 2013-5-28
/**
* Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/q6Wk
*/
package {
import flash.events.Event;
import flash.display.Sprite;
public class FlashTest extends Sprite {
public function FlashTest() {
// write as3 code here..
xrad = 64;
dispRect = new Sprite();
dispRect.graphics.lineStyle(2,0);
dispRect.graphics.drawCircle(0,0,4);
dispRect.graphics.drawRect(-xrad,-yrad,xrad+xrad,yrad+yrad);
dispRect.x = 200;
dispRect.y = 200;
addChild(dispRect);
stage.addEventListener(Event.ENTER_FRAME, onEnter);
}//ctor
public var xrad:Number = 32;
public var yrad:Number = 32;
public var dispRect:Sprite;
public var move:Number = 0;
public var mang:Number = 0;
public function onEnter(e:Event):void
{
move += 0.05;
mang += 0.01;
dispRect.x = 200+(Math.cos(move)*Math.sin(move))*92;
dispRect.rotationZ = mang* (180/3.1415);
graphics.clear();
graphics.lineStyle(2, 0);
var ax:Number;
var ay:Number;
var bx:Number;
var by:Number;
ax = 200;
ay = 50;
bx = 200;
by = 350;
graphics.moveTo(ax, ay);
graphics.lineTo(bx, by);
graphics.drawCircle(ax,ay,4);
graphics.lineStyle(2, 0xFF0000, 0.25);
var u:Number;
u = lineLineInter(ax,ay,bx,by, dispRect.x-xrad,dispRect.y-yrad,
dispRect.x+xrad, dispRect.y-yrad);
var kx:Number;
var ky:Number;
if (u != 999)
{
kx = ax + (bx - ax) * u;
ky = ay + (by - ay) * u;
graphics.drawCircle(kx,ky,3);
}//endif
var cx:Number;
var cy:Number;
var dx:Number;
var dy:Number;
var rx:Number;
var ry:Number;
var wang:Number;
wang = -mang;
cx = ax - dispRect.x;
cy = ay - dispRect.y;
dx = bx - dispRect.x;
dy = by - dispRect.y;
rx = Math.cos(wang)*cx - Math.sin(wang)*cy;
ry = Math.sin(wang)*cx + Math.cos(wang)*cy;
cx = rx; cy = ry;
rx = Math.cos(wang)*dx - Math.sin(wang)*dy;
ry = Math.sin(wang)*dx + Math.cos(wang)*dy;
dx = rx; dy = ry;
graphics.moveTo(200+cx, 200+cy);
graphics.lineTo(200+dx, 200+dy);
graphics.drawCircle(200+cx, 200+cy, 4);
graphics.drawRect(200-xrad,200-yrad,xrad+xrad,yrad+yrad);
var t:Number;
var t0:Number;
var t1:Number;
var t2:Number;
var t3:Number;
t0 = lineLineInter(cx,cy,dx,dy,-xrad,-yrad,xrad,-yrad);
t1 = lineLineInter(cx,cy,dx,dy,-xrad,yrad,xrad,yrad);
t2 = lineLineInter(cx,cy,dx,dy,xrad,-yrad,xrad,yrad);
t3 = lineLineInter(cx,cy,dx,dy,-xrad,-yrad,-xrad,yrad);
t = Math.min(t0, t1);
t0 = Math.min(t2, t3);
t = Math.min(t, t0);
var ix:Number;
var iy:Number;
if (t != 999)
{
kx = cx + (dx - cx) * t;
ky = cy + (dy - cy) * t;
graphics.drawCircle(200+kx, 200+ky, 8);
rx = Math.cos(mang)*kx - Math.sin(mang)*ky;
ry = Math.sin(mang)*kx + Math.cos(mang)*ky;
ix = rx + dispRect.x;
iy = ry + dispRect.y;
graphics.lineStyle(2,0);
graphics.beginFill(0);
graphics.drawCircle(ix, iy, 4);
graphics.endFill();
}//endif
}//onenter
public function lineLineInter(
v0x:Number, v0y:Number,
v1x:Number, v1y:Number,
v2x:Number, v2y:Number,
v3x:Number, v3y:Number ):Number
{
var ua:Number;
var ub:Number;
var bot:Number;
bot = ((v3y-v2y)*(v1x-v0x))-((v3x-v2x)*(v1y-v0y));
if (bot == 0) { return 999;}
ua = (((v3x-v2x)*(v0y-v2y))-((v3y-v2y)*(v0x-v2x))) / bot;
ub = (((v1x-v0x)*(v0y-v2y))-((v1y-v0y)*(v0x-v2x))) / bot;
if (ua > 1 || ua < 0 || ub > 1 || ub < 0) { return 999; }
return ua;
}//lineline
}//classend
}