Bézier Curve through 3 points

Some days ago I had to do a Flash application where the user was able to select an image area by dragging some points around. The points should be anchors to create curves that pass through those points.

My first thought was that I wouldn’t have problems by using the curveTo method of the MovieClip class as long this method uses 3 points to create a curve, what was exactly what I needed.

The problem is that this method uses the Bézier formula to create the curve, and the Bézier curve does not pass through the second point, as you can see above:


I strong believe that I wasn’t the first one to have this frustration trying to create a curve on Flash, because that I decided to post the solution.

The formula above is the Bézier formula. Using my second point as B(t), 0.5 as t (the middle of the curve) and isolating the P2 I came to this code:

p3.x = (0.25 * p0.x + 0.25 * p2.x - p1.x) / -0.5;
p3.y = (0.25 * p0.y + 0.25 * p2.y - p1.y) / -0.5;
moveTo(p0.x, p0.y);
curveTo(p3.x, p3.y, p2.x, p2.y);

P0, P1, P2 are the 3 points used to create the curve, P3 is the generated point to replace the P2 on the curveTo method.

Here is the result:

I hope it helps someone! :)

7 comments ↓

#1 Anonymous on 09.25.09 at 4:49 pm

Hi, nice article.

BTW: the beziere formula ist supported natively by the player (from 9 up) in the fl.motion package. Search livedocs for "BezierSegment". Helps to speed up your app and keep code short ;)

#2 Anonymous on 10.04.09 at 7:51 pm

Thanks so much, this is just what I needed!

#3 Henrique Vilela on 10.07.09 at 12:38 am

You're welcome :)

#4 Anonymous on 12.22.09 at 2:33 pm

Thank you – been looking for days for something like this.

BTW, your swf's dont show up anymore.

#5 Henrique Vilela on 12.22.09 at 4:23 pm

Sorry, I had a problem with my old server so I lost that files and I don't have the source anymore. But I'm glad that it helped you.

#6 Anonymous on 01.04.10 at 11:02 am

Awesome! thank you so much!

#7 spoommaPamp on 07.29.10 at 12:47 am

Howdy every one, nice website I have found It very accessible and it has helped me a lot
I hope to be able to give something back and guide other users like this forum has helped me

_________________
unlock iphone 4.0

Leave a Comment