Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quadratic Bézier Curve: Calculate Points

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?

like image 643
Christian Engel Avatar asked Apr 12 '11 11:04

Christian Engel


People also ask

How do you get points on a Bezier curve?

Following the construction of a Bézier curve, the next important task is to find the point C(u) on the curve for a particular u. A simple way is to plug u into every basis function, compute the product of each basis function and its corresponding control point, and finally add them together.

How do you find the Bezier curve of a quadratic equation?

For given three points P0, P1 and P2, a quadratic bezier curve is a linear interpolation of two points, got from Linear Bezier curve of P0 and P1 and Linear Bezier Curve of P1 and P2. So, Quadratic bezier curve is given by, B(t) = (1-t) BP0,P1(t) + t BP1,P2(t), 0 < t < 1.

What are control points in Bezier curve?

A Bézier curve is defined by a set of control points P0 through Pn, where n is called the order of the curve (n = 1 for linear, 2 for quadratic, etc.). The first and last control points are always the endpoints of the curve; however, the intermediate control points (if any) generally do not lie on the curve.


2 Answers

Use the quadratic Bézier formula, found, for instance, on the Wikipedia page for Bézier Curves:

quadratic Bezier formula

In pseudo-code, that's

t = 0.5; // given example value x = (1 - t) * (1 - t) * p[0].x + 2 * (1 - t) * t * p[1].x + t * t * p[2].x; y = (1 - t) * (1 - t) * p[0].y + 2 * (1 - t) * t * p[1].y + t * t * p[2].y; 

p[0] is the start point, p[1] is the control point, and p[2] is the end point. t is the parameter, which goes from 0 to 1.

like image 94
xan Avatar answered Oct 06 '22 01:10

xan


In case somebody needs the cubic form:

        //B(t) = (1-t)**3 p0 + 3(1 - t)**2 t P1 + 3(1-t)t**2 P2 + t**3 P3          x = (1-t)*(1-t)*(1-t)*p0x + 3*(1-t)*(1-t)*t*p1x + 3*(1-t)*t*t*p2x + t*t*t*p3x;         y = (1-t)*(1-t)*(1-t)*p0y + 3*(1-t)*(1-t)*t*p1y + 3*(1-t)*t*t*p2y + t*t*t*p3y; 
like image 37
Tatarize Avatar answered Oct 05 '22 23:10

Tatarize