Flex – 沿曲线走 – 二次贝塞尔曲线算法,微积分没白学终于派上用场

Flex – 沿曲线走 – 二次贝塞尔曲线算法,微积分没白学终于派上用场
/**
* 二次贝塞尔曲线 by Sam.Sha
* Q0=P0+(P1-P0)*t
* Q1=P1+(P2-P1)*t
* Px=Q0+(Q1-Q0)*t
* –>
* Px = (P0 + P2 – 2 * P1) * t * t + (2 * P1 – 2 * P0) * t + P0
* –> 求微
* dPt = 2(P0 + P2 – 2* P1) * t +2 * P1 – 2 * P0;
* return [point, angle, distance]
* */
private static function calculateCurveInfo(x0:Number, x1:Number, x2:Number, y0:Number, y1:Number, y2:Number, t:Number, dt:Number = 0.05):Array{
if(t<0){
return null;
}
var subX:Number = 0;
var angle:Number;
var pathNumber:int = t / dt;
if(pathNumber <= 0){
pathNumber = 1;
}
dt = t / pathNumber;
var dx:Number, dy:Number;
var currentT:Number = 0;
for(var i:int = 0;i<pathNumber;i++){
currentT = dt*i;
dx = 2*(x0 + x2 – 2*x1)*currentT + 2*x1 – 2*x0;
dy = 2*(y0 + y2 – 2*y1)*currentT + 2*y1 – 2*y0;
dx *= dt;
dy *= dt;
subX += Math.sqrt(dx*dx + dy*dy);
}
angle = Math.atan2(dy, dx);
var x:Number = (x0 + x2 – 2*x1)*t*t + (2*x1 – 2*x0)*t + x0;
var y:Number = (y0 + y2 – 2*y1)*t*t + (2*y1 – 2*y0)*t + y0;
return [new Point(x, y), angle, subX];
}


五 − 1 =