views:

60

answers:

2

What's the best way to draw cubic bezier curves programmatically in AS3? The Graphics class only seems to support quadratic curves. :( I want to be able to do something like:

var startPoint:Point = new Point(0, 0);
var endPoint:Point = new Point(5, 5);
var control1:Point = new Point(5, 0);
var control2:Point = new Point(0, 5);

var myBezier:Sprite = getBezier(startPoint, control1, control2, endPoint);

For a performance target, I'm planning on having ~50 of these on the stage at once.

+1  A: 

If quadratic are built into that API call, you'll have to understand Bezier well enough to write your own cubic implementation.

Like this.

duffymo
+2  A: 

Just last week I wrote a class to draw Bezier curves of arbitrary order.

The code is not optimized but works fine in my tests. Performance is acceptable event for doing animations (although I don't think it's a good idea to abuse it, since as I said it's not optimized; it doesn't make sense to use these for quadratic curves, of course, since the player can do that natively).

The code is here if you want to use it or take a look:

The BezierCurve class

Sample code

I think that with the sample code you will be able to figure out how to use it without trouble (it's quite straight forward and somewhat commented); but if you run into problems, ask away!

Feel free to use it as you see fit.

Juan Pablo Califano