bezier_interpolation
is a Python library for generating smooth curves between given points using cubic Bezier or quadratic interpolation.
It solves a specific problem: Calculate the optimal control points between two data points to have smooth bezier curves.
It provides a simple and efficient way to calculate the control points and interpolate the data, making it ideal for applications in computer graphics, data visualization, and other fields where smooth curves are required.
pip install bezier_interpolation
from bezier_interpolation import cubic_interpolation, quadratic_interpolation
c_data = {"x": [1, 2, 3], "y": [-1, -5, 3]}
c_data = list(zip(c_data["x"], c_data["y"]))
c_interpolated_data = cubic_interpolation(c_data)
print(c_interpolated_data)
# Returns: [[1, -1], [1.3, -3.3], [1.6 -5.6], [2, -5], [2.3, -4.3], [2.6, -0.6], [3, 3]]
q_data = [(1, 1), (2, 4), (3, 9)]
q_interpolated_data = quadratic_interpolation(q_data)
print(q_interpolated_data)
# Returns: [[1, 1], [1.5, 2.5], [2, 4], [2.5, 5.5], [3, 9]]
Let's say we have this 3 green points
We need to find where to set the control points
Here the first index represent the point and the second represent the segment.
In general we have n points, n - 1 segments, and we need to find
For that we need to set two conditions:
- The last point in one segment
$P_{3,i}$ is equal to the start point in the next segment$P_{0, i + 1}$ . - To get a smooth curve we need to make sure the slope of the line connecting
$P_{2,0}$ and$P_{3,0}$ is the same as$P_{3,0}$ and$P_{0,1}$ .
We can write the first condition as
For the second condition we are goint to use derivatives. The first derivative of any polynomial curve is the slope, so let's derivate the equation for a cubic Bezier curve:
Ant its first derivative:
By definition, if we replace
If we replace the values in the equation, we get:
And using the eq (1):
We will now move to the second derivative. The second derivative tells us about the continuity of the curve. The second derivative of the bezier curve is:
The second derivative talks about the continuity and concavity of the plot.
So, we can write:
If we substitute again we get:
Those equations only apply in the internal knots, when the segments come together. So, let's get the equations for the boundaries, this is,
and:
now we have:
With those equations we can set a matrix system with the form Ax -b = 0, when A is a tri-diagonal matrix with this form:
x is this vector:
and b this one:
and if we solve this system, we can get
We could follow a process similar to the one we did above, but this one would be straightforward and it will have no dependency with t. We want to control the start angle, so let's try a geomterical approach.
Let's suppose we now want just one control point between two data points, so we have something like this
Did you notice that now we need points
In general, we need
We are going to set the first control point in the middle of
If we want to add the control parameter
Now that we have the first control point, we need to compute the line between
We call the line between
So, in general, for the next
and
wich are the X,Y components of the desired