# Find control point on piecewise quadratic Bézier curve

I need to write an OpenGL program to generate and display a piecewise quadratic Bézier curve that interpolates each set of data points:

$$(0.1, 0), (0, 0), (0, 5), (0.25, 5), (0.25, 0), (5, 0), (5, 5), (10, 5), (10, 0), (9.5, 0)$$

The curve should have continuous tangent directions, the tangent direction at each data point being a convex combination of the two adjacent chord directions.

I am not good at math, can anyone give me some suggestions about what formula I can use to calculate control point for Bézier curve if I have a starting point and an ending point.

• Does "continuous tangent directions" mean G1 continuity (the tangents on both sides of the interpolated control points are colinear) or C1 continuity (the derivative is continuous)? And what other constraints do you have? E.g. are you trying to minimise some property? – Peter Taylor May 31 '11 at 21:41
• Hi Peter, these are all constraints I have. I asked my teacher about tangent early this morning, and he replied "you can assume that the tangent is the same as that of a circle that interpolates the first (last) three points". I really have no idea about G1 and C1. But the curve should like i.stack.imgur.com/ckA9M.png , where black points are my data points and quadratic control points (red) and the curve (blue).Thanks for replying – Bezier Guy May 31 '11 at 22:11
• This was cross-posted at gamedev.stackexchange.com/questions/12972/…. – joriki Nov 13 '11 at 19:23

You can see that it will be difficult to solve this satisfactorily by considering the case where the points to be interpolated are at the extrema of a sinusoidal curve. Any reasonable solution should have horizontal tangents at the points, but this is not possible with quadratic curves.

Peter has described how to achieve continuity of the tangents with many arbitrary choices. You can reduce those choices to a single choice by requiring continuity in the derivatives, not just their directions (which determine the tangents). This looks nice formally, but it can lead to rather wild curves, since a single choice of control point at one end then determines all the control points (since you now have to take equal steps on both sides of the points in Peter's method), and these may end up quite far away from the original points – again, take the case of the extrema of a sinusoidal; this will cause the control points to oscillate more and more as you propagate them.

What I would try in order to get around these problems, if you really have to use quadratic Bézier curves, is to use some good interpolation method, e.g. cubic splines, and calculate intermediate points between the given points, along with tangent directions at the given points and the intermediate points. Then you can draw quadratic Bézier curves through all the points, given and intermediate, and determine control points by intersecting the tangents. This wouldn't work without the intermediate points, because the tangents might not intersect at reasonable points – again, think of the extrema of a sinuisoidal, where the desired tangents are in fact parallel – but I think it should work with the intermediate points – for instance, in the sinusoidal example, the intermediate points would be at the inflection points of the sinusoidal, and the tangents would intersect at suitable control points.

• > you now have to take equal steps on both sides of the points -- the steps have equal length only if the two adjacent segments have equal length parameter intervals. – bubba May 3 '13 at 5:11

Here is one solution:

It's not a very good looking curve, but, given the strange shape of your point set, I'm not sure there are solutions that are much better.

The black dots are the original data points, and the alternating red and green pieces are the quadratic segments.

As you can see, the "break points" or "knot points" where segments join are not at the original data points. This is fairly typical of what you see when interpolating using splines with even degree.

The curve was calculated using pretty standard spline interpolation methods -- essentially you just write down a set of linear equations that express the interpolation conditions, and solve them. The details are left to the student :-).

The curve is C1.

My guess is that this is not what your instructor was expecting you to do. He was expecting you to somehow make up some tangent vectors at the data points, and use the points and tangents to construct the spline. If you do this, you will run into trouble wherever there's an inflexion. Maybe that was the point of the exercise.

If you want to do it this way, I would recommend that you proceed as follows:

(1) Make up the tangent vectors. There are many ways to do this.

(2) Take each pair of points in turn.

(a) If its possible to build a quadratic segment from the two points and two tangents, then insert a quadratic.

(b) If a quadratic is not possible (because of an inflexion, or a 180 degree turn), build a Bezier cubic from the two points and two tangents, instead. Then split this cubic into two, and then replace each of the two halves with a quadratic. You should split at the inflexion point, if there is one.

Here's an example of what you can get by this method. The pink points are places where I joined together two quadratics to replace a cubic, as mentioned in 2(b) above:

I know this is all a bit vague and sketchy, but it should give you some hints, at least.

Given the almost complete lack of constraints, you have a lot of degrees of freedom. Let's call your points $P_0$, $P_1$, ..., $P_n$, and the control points you need to add $Q_0$ to $Q_{n-1}$.

You can pick any point you want for $Q_0$. Then draw a line from $Q_0$ through $P_1$ and pick any point on the other side of $P_1$ for $Q_1$. Draw a line from $Q_1$ through $P_2$ and pick any point on the other side for $Q_2$. Etc.

I agree with joriki. Forcing continuity of the derivatives, you will get a very wild quadratic spline. Forcing only continuity of the directions, you gain liberty, but it will still be hard. I never saw the idea of construct the quadratic spline using the cubic one before, but I liked! (although it will be laborious...)

Complementing the things said, I found this reference:

http://pomax.github.io/bezierinfo/

The text is huge, but look at the figures 36 and 38. They show the two ideas discussed here! (and the source code is available for consult! ;) )

Good luck!

• > Forcing continuity of the derivatives, you will get a very wild quadratic spline -- I don't think this is true. If you have any G1 spline (of any degree), then you can make it C1 (without changing its shape) just by adjusting the knot values. So, C1 curves are no more "wild" than G1 curves. – bubba May 3 '13 at 4:56