flash on 2012-11-9
/**
* Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/2oTp
*/
package {
import flash.text.TextField;
import flash.events.Event;
import flash.display.Sprite;
public class FlashTest extends Sprite {
public var deb:TextField;
public function FlashTest() {
deb = new TextField();
addChild(deb);
stage.addEventListener(Event.ENTER_FRAME, onEnter);
}
//based on
//http://clb.demon.fi/MathGeoLib/docs/Line.cpp_code.html
public var dt:Number = 0;
public function cpLineLine( ax:Number, ay:Number,
bx:Number, by:Number,
cx:Number, cy:Number,
dx:Number, dy:Number):Number
{
//ax 0
//ay 0
//bx 0
//by 0
var d0232:Number;
var d3210:Number;
var d3232:Number;
var mu:Number;
d0232 = (ax - cx) * (dx-cx) + (ay - cy) * (dy - cy);
d3210 = (dx - cx) * (bx - ax) + (dy - cy) * (by - ay);
d3232 = (dx - cx) * (dx - cx) + (dy - cy) * (dy - cy);
var d0210:Number;
var d1010:Number;
d0210 = (ax-cx) * (bx-ax) + (ay-cy) * (by - ay);
d1010 = (bx-ax)*(bx-ax) + (by-ay) * (by-ay);
mu = (d0232 * d3210 - d0210*d3232) / (d1010 * d3232 - d3210*d3210);
dt = (d0232 + mu * d3210) / d3232;
/*
float mu = (d0232 * d3210 -
Dmnop(v,0,2,1,0)*d3232) /
(Dmnop(v,1,0,1,0)*Dmnop(v,3,2,3,2) -
Dmnop(v,3,2,1,0)*Dmnop(v,3,2,1,0));
*/
// float d0232 = Dmnop(v,0,2,3,2);
// float d3210 = Dmnop(v,3,2,1,0);
// float d3232 = Dmnop(v,3,2,3,2);
return mu;
}//cplineline
public function onEnter(e:Event):void
{
var ax:Number = 50;
var ay:Number = 90;
var bx:Number = 120;
var by:Number = 300;
var cx:Number = 300;
var cy:Number = 30;
var dx:Number = 50;
var dy:Number = 350;
var t:Number;
dx = mouseX;
dy = mouseY;
t = cpLineLine(ax,ay,bx,by,cx,cy,dx,dy);
graphics.clear();
graphics.lineStyle(2,0);
graphics.moveTo(ax,ay);
graphics.lineTo(bx,by);
graphics.moveTo(cx,cy);
graphics.lineTo(dx,dy);
var kx:Number;
var ky:Number;
if (t > 1) { t = 1;}
if (t < 0) { t = 0;}
kx = ax + (bx - ax) * t;
ky = ay + (by - ay) * t;
graphics.drawCircle(kx, ky, 16);
var ux:Number;
var uy:Number;
var u:Number;
u = lineCirc3(cx,cy,dx,dy,kx,ky,16);
ux = cx + (dx - cx) * u;
uy = cy + (dy - cy) * u;
deb.text = "t " + t + " \n dt " + dt + " \n u " + u;
graphics.drawCircle(ux, uy, 4);
graphics.lineStyle(1,0);
graphics.moveTo(kx,ky);
graphics.lineTo(ux,uy);
}//onenter
//based on
//http://www.geometrictools.com/SamplePhysics/CollisionsMovingSphereTriangle/RTSphereTriangle.cpp
public function lineCirc3(
ax:Number, ay:Number, bx:Number, by:Number,
cx:Number, cy:Number, rad:Number):Number
{
var rsq:Number
var vx:Number;
var vy:Number;
var mx:Number;
var my:Number;
var q2:Number;
var q1:Number;
var q0:Number;
var disc:Number;
rsq = rad * rad;
mx = cx - ax;
my = cy - ay;
vx = ax - bx;
vy = ay - by;
q2 = (vx*vx +vy*vy);
q1 = (vx*mx + vy*my);
q0 = (mx*mx + my*my) - rsq;
disc = q1*q1 - q0 * q2;
if (disc < 0) { return 999; }
var invq2:Number;
var rootdisc:Number;
var root0:Number;
var root1:Number;
invq2 = 1 / q2;
rootdisc = Math.sqrt(disc);
root0 = (-q1 - rootdisc) * invq2;
root1 = (-q1 + rootdisc) * invq2;
return root0;
}//linecirc3
}//classend
}