Find bezier control-points for curve passing through N points - bezier

Considering the following nice solution for finding cubic Bézier control points for a curve passing through 4 points:
How to find control points for a BezierSegment given Start, End, and 2 Intersection Pts in C# - AKA Cubic Bezier 4-point Interpolation
I wonder, if there is a straightforward extension to this for making the Bézier curve pass through N points, for N > 2 and maybe N ≤ 20?

This is a really old question, but I'm leaving this here for people who have the same question in the future.
#divanov has mentioned that there's no Bezier curve passing through N arbitrary points for N >4.
I think the OP was asking how to compute the control points to join multiple bezier curves to produce a single curve that looks smooth.
This pdf will show you how to compute the control points: http://www.math.ucla.edu/~baker/149.1.02w/handouts/dd_splines.pdf
which I found on this writeup https://developer.squareup.com/blog/smoother-signatures/ from Square about how they render a smooth curve that passes through all the sampled points of a mouse drawn signature.

In general, there is no Bezier curve passing through N arbitrary points, where N > 4. One should consider curve fitting to minimize least square error between computed Bezier curve and given N data points. Which is discussed, for example, here.

Related

Using least square to fit a parametric curve

I have a curve represented as a parametric function, z(t)=(x(t), y(t)), x=f(t), y=g(t). If I wanna find a approximate curve using least square(using polynomial funtions), am I supposed to get one for x and one for y or just one for z?
So the problem is like this: given a parametric curve r(t) = (x(t), y(t)), find a approximate cubic polynomial curve. I collect multiple points in x(t) and y(t) and then use least square to find a corresponding curve for x(t), y(t). The final approximate curve is like r'(t) = (x'(t), y'(t))

Splitted Bezier curve

Suppose I splitted a cubic bezier curve at the point corresponding to the parameter t=0.5. Call the splitted parts C and D. Then I connect ends of C by a straight line segment. Can we say that this straight line segment never touches C apart from the endpoints?
No. This is because the split portion C and D are also a cubic Bezier curve, which can be of any shape (that a cubic polynomial curve can be). A straight line connecting two ends of a cubic Bezier curve could have intersected the curve at the interior if the cubic Bezier curve is not convex.
If you want to produce an example, you will have to make the control points going up and down drastically as shown in the following example (where the green dots are control points and the yellow dot is where t=0.5).
A close-up on where t=0.5 shows
No, because as I understand it, a straight line is a degenerate case of a Bezier curve.
So you would need to ensure, for example, that the control points are not on the line.
If the also happens to be that both points are on the same side of the line (e.g. either both above, both below, both to the left or both to the right), then that would do it.
If you don't have the control points, but have two points near the endpoints of the line, you can do the same test.
Or in other words the case where you might need to worry about is when either the control points or points near the endpoints are on in different side of the line between the endpoints.

Bezier Curve parameter and the distance taken

For a cubic bezier curve, if its parameter t is 0.5 then does this split the curve equal pieces lengthwise? Reasoning is appreciated.
In general, no, unless in some special cases, for example all control points of the cubic Bezier curve are collinear and evenly-spaced. The reason is simple: a Bezier curve is in general not arc-length parameterized.
Simple example: take three 2D control points
(1,0), (2,0) and (4,0)
The point t=0.5 is (2,0). But this point does not have equal distances to the first and last point.
Well, and this should not depend on the cubic or linear case.

ccBezierConfig as a quadratic, not cubic.

So I'm trying to animate a sprite, and I just found the CCBezierBy/CCBezierTo methods, which are saving me a lot of trouble, but they only work as cubic functions.
HOw can I make them quadratics? (I need s-curves)
Having only 3 controlpoints limits severely what you can do.
Thank you.
I don't think you understand Bezier curves here: S curves cannot be modeled by quadratic Beziers, they can only do curves with a single inflection point. Cubic curves can have two inflection points, so quadratic curves can only model curves that look like | or C, and cubic curves can model anything that looks like |, C or S
Cubic functions have a starting on-curve point, two control points, and an ending on-curve point, for a total of four control points; Quadratic functions have a starting on-curve point, one control point, and an ending on-curve point, for a total of three control points.
So what you want is a cubic curve, and that's exactly what the CCBezierBy/CCBezierTo methods offer. They let you specify a bezier curve as the two control points and end point, because you'll already have the starting point.

Smooth Bezier Curve

I want to smooth some hand draw lines in iphone.
I have used the following code in
http://webdocs.cs.ualberta.ca/~graphics/books/GraphicsGems/gems/FitCurves.c
However, I found that some bezier curved was wrong, the second control point and end point is invalid.
Did anybody have the same problem before?
Thanks.
Bezier Curves are not designed to go through the provided vertices!
They are designed to shape a smooth curve influenced by the control points.
First you must decide if you want to interpolate between missing points,
or if you want to filter non smooth data:
Filtering
You should look at "sliding average" with a small averaging window. (try 5 - 10 pixel).
This works as follows: (look for wiki for a detailed description)
I use here an average window of 10 points:
start by calculation of the average of points 0 - 9, and output the result as result point 0
then calculate the average of point 1 - 10 and output, result 1
And so on.
Interpolation
If you want to interpolate between (missing) points using a smooth curve, you could use piece - wise cubic splines:
You calculate the coefficients of a cubic polygon through 3 vertices.
You start with calculating the cubic polygon through:
Point[0] - Point[2], but you draw your output only from Point[0] to Point[1] .
Then you move on one step: and calculate through
Point[1] - Point[3], but you draw only from p1 to p2.
And so on.
You need to search on wiki for cubic interpolation for a detailed explanation how to caluclate a cubic polygon (sometimes called cubic spline).