I am searching a way to make c2 continuous CLOSED bezier spline via given points. I found a perfect description for OPENED spline here. Briefly it is: If were describe path needed as $i$ segments, where each segment is cubic Bezier curve, with control points from $P_{0,i}$ to $P_{3,i}$, then C2 continuity conditions are:
$P_{0,i}=P_{3,i-1}$ ; ($C0$ continuity)
$2P_{0,i}=P_{1,i}+P_{2,i-1}$; ($C1$ continuity)
and
$-2P_{1,i}+P_{2,i}=P_{1,i-1}-2P_{2,i-1}$ ; ($C2$ continuity)
Until this moment everything is clear for me (even with my poor math background).
Later they use fact that, the spline becomes linear at the end points. But I need to create a contour:
$P_{0,0}=P_{3,n}$, and this complication burn my brain. I have no ideas how to deal with such equations.
Could you help me to find control point for c2 continuous closed bezier spline?
Here is a simple way for building a $C^2$ closed spline obtained by assembling cubic Bezier curves. See Fig. 1.
1) Begin by considering a "scaffolding" polygon $P_0P_1P_2\cdots P_{n-1}P_n$ that will give the general shape of the curve to come (we take the cyclic convention: $P_n=P_0$ that will as well be used for families of points $A_k$, $B_k$, $C_k$ that are to be defined).
2) On each side $[P_kP_{k+1}]$, consider the $2$ points situated resp. at the third and two-thirds of this side:
$$B_k = \tfrac13(2 P_k + P_{k+1}) \ \ \ \text{and} \ \ \ \ C_k = \tfrac13(P_k + 2 P_{k+1}).$$
3) Then define midpoints $A_{k+1} = \tfrac12(B_k + C_k).$
4) At last, draw the $n$ Bezier curves defined by points $(A_k, B_k, C_k, A_{k+1})$.
At junction points $A_k$, it is almost immediate to verify that the speed vectors of the two joined arcs are the same; it not difficult to prove either that the acceleration vectors of the two arcs are the same too.
There is more to do. The process just described is convenient for curve approximations: for example, a designer who wants to build an egg-shaped curve has a satisfying answer.
But for "technical drawing", where the curve is constrained to pass through pre-determined points $A_k$, we have to build (if possible) points $P_k$ from points $A_k$. This is curve interpolation.
It is always possible. How ? We have just to understand Fig. 2 and the resulting system of equations (3).
Let us start from $6A_k$: this point can be expressed like this:
$$6A_k = 3C_{k-1}+ 3B_k = (P_{k-1} + 2P_k)+(2P_k + P_{k+1}) $$
(using definitions of $B_k$ and $C_k$), i.e.,
$$\tag{2}6A_k = P_{k-1} + 4P_k+ P_{k+1}$$
Gathering equations (2) for all values of $k$, and taking into account the cyclicity property, we get a system that, in the case of a 5 points, is:
$$\tag{3}\begin{cases}&4P_0 &+& P_1&+ &&&&& P_4 &=& 6A_0\\ &P_0 &+& 4P_1&+& P_2 &&&&&=& 6A_1\\ &&&P_1 &+& 4P_2&+& P_3 &&&=& 6A_2\\ &&&&&P_2 &+& 4P_3&+& P_4 &=& 6A_3\\ &P_0 &+&&&&& P_3&+& 4P_4 &=& 6A_4\\ \end{cases}$$
System (3) is invertible (diagonally dominant matrix) ; therefore, being given points $A_k$, we can obtain all points $P_k$.
Fig. 3 shows the versatility of this method : the generated curve can have as well concave parts or even loops.
Here is a Matlab implementation of the method just detailed:
Fig. 1.
Fig. 3.