Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// Move point on quad Bezier
// (De Casteljau's Algorithm)
import flash.geom.Point;
import flash.display.Shape;
import flash.events.Event;
var tangent:Shape = new Shape();
addChild(tangent);
var dot:Shape = new Shape();
dot.graphics.clear();
dot.graphics.lineStyle(1, 0xff0000);
dot.graphics.beginFill(0xffffff);
dot.graphics.drawCircle(0, 0, 4);
dot.graphics.endFill();
addChild(dot);
// Bezier control points
var p0:Point = new Point(50, 350); // start
var p1:Point = new Point(250, 50); // control
var p2:Point = new Point(500, 200); // end
graphics.lineStyle(1, 0xcccccc);
graphics.moveTo(p0.x, p0.y);
graphics.lineTo(p1.x, p1.y);
graphics.lineTo(p2.x, p2.y);
graphics.lineStyle(1, 0);
graphics.moveTo(p0.x, p0.y);
graphics.curveTo(p1.x, p1.y, p2.x, p2.y);
var c:Number = 0;
addEventListener(Event.ENTER_FRAME, onEnterFrame);
function onEnterFrame(event:Event):void {
var u:Number = c / 5000;
var x1:Number = p0.x + u * (p1.x - p0.x);
var y1:Number = p0.y + u * (p1.y - p0.y);
var x2:Number = p1.x + u * (p2.x - p1.x);
var y2:Number = p1.y + u * (p2.y - p1.y);
dot.x = x1 + u * (x2 - x1);
dot.y = y1 + u * (y2 - y1);
tangent.graphics.clear();
tangent.graphics.lineStyle(1, 0xff0000, 0.4);
tangent.graphics.moveTo(x1, y1);
tangent.graphics.lineTo(x2, y2);
if((c += 20) > 5000) c = 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment