I'd like to calculate a point on a quadratic curve. To use it with the canvas element of HTML5.
When I use the quadraticCurveTo()
function in JavaScript, I have a source point, a target point and a control point.
How can I calculate a point on the created quadratic curve at let's say t=0.5
with "only" knowing this three points?
I edited talkhabis answer (cubic curve) so the curve is displayed with the right coordinates. (Couldn't comment) The Y-coordinates needed to be changed (-p[].y+150). (A new variable for that might be a nicer and more efficient solution, but you get the idea)
http://jsfiddle.net/u214gco8/1/
I also created some C-Code to test the results for the cubic curve. Just enter the X and Y coordinates in the main function.
https://ideone.com/glLXcB
I created this demo :
Full example here
may help someone.
Use the quadratic Bézier formula, found, for instance, on the Wikipedia page for Bézier Curves:
In pseudo-code, that's
p[0]
is the start point,p[1]
is the control point, andp[2]
is the end point.t
is the parameter, which goes from 0 to 1.Just a note: If you are using the usual formulas presented here then don't expect t = 0.5 to return the point at half of the curve's length.. In most cases it won't.
More on this here under "§23 — Tracing a curve at fixed distance intervals" and here.
In case somebody needs the cubic form: