> Jason Merrill wrote: 
> I am drawing a bezier curve using:
> 
> mySprite.graphics.curveTo(X1, Y1, X2, Y2); 
> 
> Works, and so I know where the control point is, and the desintation 
> point, but is there a way to calculate a point midway along the curve? 
> I'd like to position a sprite there. 
 
Hi Jason, 
 
The findPointOnCurve function (shown below) will give you exactly what you
need. Just plug in your coordinates. Use a value of .5 for t to return the
curve's midpoint. 
 
The findPointOnCurve function is used by Zeh Fernando's mc_tween2.as for the
MovieClip.bezierSlideTo(...). I have also been successful adapting it for
use in conjunction with Jack Doyle's TweenLite.as (http://www.greensock.com)
for tweening an arc. 
 
 
private function findPointOnCurve(p1x:Number, p1y:Number, cx:Number,
cy:Number, p2x:Number, p2y:Number, t:Number):Object { 
// From Zeh Fernando's mc_tween2.as (http://hosted.zeh.com.br/mctween/) 
// Returns the points on a bezier curve for a given time (t is 0-1); 
// This is based on Robert Penner's Math.pointOnCurve() function 
return {x:p1x + t*(2*(1-t)*(cx-p1x) + t*(p2x - p1x)), 
        y:p1y + t*(2*(1-t)*(cy-p1y) + t*(p2y - p1y))}; 
} 
 
// Usage: 
mySprite.graphics.clear(); 
mySprite.graphics.lineStyle(3, 0x00FF00, 100); 
mySprite.graphics.moveTo(start.x, start.y); 
mySprite.graphics.curveTo(control.x, control.y, finish.x, finish.y); 
var p:Object = findPointOnCurve(start.x, start.y, control.x, control.y,
finish.x, finish.y, .5); 
mySprite.graphics.lineStyle(10, 0xFF0000, 100); 
mySprite.graphics.moveTo(p.x, p.y); 
mySprite.graphics.lineTo(p.x + 1, p.y - 1); 
 
HTH 
  
Regards, 

-Keith 
http://keithreinfeld.home.comcast.net
 



_______________________________________________
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to