9.2.1. Bézier Curves¶
9.2.1.1. Valentina Requirements¶
- Quadratic Bézier curve
- adjustment of the control points by pulling the curve (passing by a point)
- curve - line intersection
- curve - curve intersection
9.2.1.2. Definitions¶
A Bézier curve is defined by a set of control points \(\mathbf{P}_0\) through \(\mathbf{P}_n\), where \(n\) is called its order (\(n = 1\) for linear, 2 for quadratic, 3 for cubic etc.). The first and last control points are always the end points of the curve;
In the following \(0 \le t \le 1\) and \(u = 1 - t\)
9.2.1.3. Linear Bézier Curves¶
Given distinct points \(\mathbf{P}_0\) and \(\mathbf{P}_1\), a linear Bézier curve is simply a straight line between those two points. The curve is given by
and is equivalent to linear interpolation.
9.2.1.4. Quadratic Bézier Curves¶
A quadratic Bézier curve is the path traced by the function \(\mathbf{B}(t)\), given points \(\mathbf{P}_0\), \(\mathbf{P}_1\), and \(\mathbf{P}_2\),
which can be interpreted as the linear interpolant of corresponding points on the linear Bézier curves from \(\mathbf{P}_0\) to \(\mathbf{P}_1\) and from \(\mathbf{P}_1\) to \(\mathbf{P}_2\) respectively.
Rearranging the preceding equation yields:
This can be written in a way that highlights the symmetry with respect to \(\mathbf{P}_1\):
Which immediately gives the derivative of the Bézier curve with respect to t:
from which it can be concluded that the tangents to the curve at \(\mathbf{P}_0\) and \(\mathbf{P}_2\) intersect at \(\mathbf{P}_1\). As \(t\) increases from 0 to 1, the curve departs from \(\mathbf{P}_0\) in the direction of \(\mathbf{P}_1\), then bends to arrive at \(\mathbf{P}_2\) from the direction of \(\mathbf{P}_1\).
The second derivative of the Bézier curve with respect to \(t\) is
9.2.1.5. Cubic Bézier Curves¶
Four points \(\mathbf{P}_0\), \(\mathbf{P}_1\), \(\mathbf{P}_2\) and \(\mathbf{P}_3\) in the plane or in higher-dimensional space define a cubic Bézier curve. The curve starts at \(\mathbf{P}_0\) going toward \(\mathbf{P}_1\) and arrives at \(\mathbf{P}_3\) coming from the direction of \(\mathbf{P}_2\). Usually, it will not pass through \(\mathbf{P}_1\) or \(\mathbf{P}_2\); these points are only there to provide directional information. The distance between \(\mathbf{P}_1\) and \(\mathbf{P}_2\) determines “how far” and “how fast” the curve moves towards \(\mathbf{P}_1\) before turning towards \(\mathbf{P}_2\).
Writing \(\mathbf{B}_{\mathbf P_i,\mathbf P_j,\mathbf P_k}(t)\) for the quadratic Bézier curve defined by points \(\mathbf{P}_i\), \(\mathbf{P}_j\), and \(\mathbf{P}_k\), the cubic Bézier curve can be defined as an affine combination of two quadratic Bézier curves:
The explicit form of the curve is:
For some choices of \(\mathbf{P}_1\) and \(\mathbf{P}_2\) the curve may intersect itself, or contain a cusp.
The derivative of the cubic Bézier curve with respect to \(t\) is
The second derivative of the Bézier curve with respect to \(t\) is
9.2.1.6. Recursive definition¶
A recursive definition for the Bézier curve of degree \(n\) expresses it as a point-to-point linear combination of a pair of corresponding points in two Bézier curves of degree \(n-1\).
Let \(\mathbf{B}_{\mathbf{P}_0\mathbf{P}_1\ldots\mathbf{P}_n}\) denote the Bézier curve determined by any selection of points \(\mathbf{P}_0\), \(\mathbf{P}_1\), \(\ldots\), \(\mathbf{P}_{n-1}\).
The recursive definition is
The formula can be expressed explicitly as follows:
where \(b_{i,n}(t)\) are the Bernstein basis polynomials of degree \(n\) and \(n \choose i\) are the binomial coefficients.
9.2.1.7. Degree elevation¶
A Bézier curve of degree \(n\) can be converted into a Bézier curve of degree \(n + 1\) with the same shape.
To do degree elevation, we use the equality
Each component \(\mathbf{b}_{i,n}(t) \mathbf{P}_i\) is multiplied by \((1-t)\) and \(t\), thus increasing a degree by one, without changing the value.
For arbitrary \(n\), we have
Therefore the new control points are
It introduces two arbitrary points \(\mathbf{P}_{-1}\) and \(\mathbf{P}_{n+1}\) which are cancelled in \(\mathbf{P'}_i\).
Example for a quadratic Bézier curve:
9.2.1.8. Matrix Forms¶
9.2.1.9. Symbolic Calculation¶
>>> from sympy import *
>>> P0, P1, P2, P3, P, t = symbols('P0 P1 P2 P3 P t')
>>> B2 = (1-t)*((1-t)*P0 + t*P1) + t*((1-t)*P1 + t*P2)
>>> collect(expand(B2), t)
P0 + t**2*(P0 - 2*P1 + P2) + t*(-2*P0 + 2*P1)
>>> B2_012 = (1-t)*((1-t)*P0 + t*P1) + t*((1-t)*P1 + t*P2)
>>> B2_123 = (1-t)*((1-t)*P1 + t*P2) + t*((1-t)*P2 + t*P3)
>>> B3 = (1-t)*B2_012 + t*B2_123
>>> collect(expand(B3), t)
P0 + t**3*(-P0 + 3*P1 - 3*P2 + P3) + t**2*(3*P0 - 6*P1 + 3*P2) + t*(-3*P0 + 3*P1)
# Compute derivative
>>> B2p = collect(simplify(B2.diff(t)), t)
-2*P0 + 2*P1 + t*(2*P0 - 4*P1 + 2*P2)
>>> B3p = collect(simplify(B3.diff(t)), t)
-3*P0 + 3*P1 + t**2*(-3*P0 + 9*P1 - 9*P2 + 3*P3) + t*(6*P0 - 12*P1 + 6*P2)
9.2.1.10. Curve Length¶
Reference
- http://www.gamedev.net/topic/551455-length-of-a-generalized-quadratic-bezier-curve-in-3d
- Dave Eberly Posted October 25, 2009
The quadratic Bézier is
The derivative is
The length of the curve for \(0 <= t <= 1\) is
The integrand is of the form \(\sqrt{c t^2 + b t + a}\)
You have three separate cases: \(c = 0\), \(c > 0\), or \(c < 0\).
The case \(c = 0\) is easy.
For the case \(c > 0\), an antiderivative is
For the case \(c < 0\), an antiderivative is
where \(k = \frac{4c}{q}\) with \(q = 4ac - b^2\).
9.2.1.11. Determine the curve flatness¶
Reference
- Kaspar Fischer and Roger Willcocks http://hcklbrrfnn.files.wordpress.com/2012/08/bez.pdf
- PostScript Language Reference. Addison- Wesley, third edition, 1999
flatness is the maximum error allowed for the straight line to deviate from the curve.
Algorithm
We define the flatness of the curve as the argmax of the distance from the curve to the line passing by the start and stop point.
\(\mathrm{flatness} = argmax(d(t))\) for \(t \in [0, 1]\) where \(d(t) = \vert B(t) - L(t) \vert\)
The line equation is
Let
The distance is
The square of the distance is
From
we can express a bound on the flatness
Thus an upper bound of \(16\,\mathrm{flatness}^2\) is
9.2.1.12. Intersection of Bézier Curve with a Line¶
Algorithm
- Apply a transformation to the curve that maps the line onto the X-axis.
- Then we only need to test the Y-values for a zero.
9.2.1.13. Closest Point¶
Reference
- https://hal.archives-ouvertes.fr/inria-00518379/document Improved Algebraic Algorithm On Point Projection For Bézier Curves Xiao-Diao Chen, Yin Zhou, Zhenyu Shu, Hua Su, Jean-Claude Paul
Let a point \(\mathbf{P}\) and the closest point \(\mathbf{B}(t)\) on the curve, we have this condition:
9.2.1.13.1. Quadratic Bézier Curve¶
Let
We have
The condition can be expressed as
>>> C = collect(expand((P*B2p - B2*B2p)/-2), t)
P*P0 - P*P1 - P0**2 + P0*P1 +
t**3 * (P0**2 - 4*P0*P1 + 2*P0*P2 + 4*P1**2 - 4*P1*P2 + P2**2) +
t**2 * (-3*P0**2 + 9*P0*P1 - 3*P0*P2 - 6*P1**2 + 3*P1*P2) +
t * (-P*P0 + 2*P*P1 - P*P2 + 3*P0**2 - 6*P0*P1 + P0*P2 + 2*P1**2)
>>> A = P1 - P0
B = P2 - P1 - A
M = P0 - P
>>> C2 = B**2 * t**3 + 3*A*B * t**2 + (2*A**2 + M*B) * t + M*A
>>> expand(C - C2)
0
9.2.1.13.2. Cubic Bézier Curve¶
Let
We have
>>> n, r, s, v = symbols('n r s v')
>>> B3 = n*t**3 + r*t**2 + s*t + v
>>> B3p = simplify(B3.diff(t))
>>> C = collect(expand((P*B3p - B3*B3p)), t)
-3*n**2 * t**5 +
-5*n*r * t**4 +
-2*(2*n*s + r**2) * t**3 +
3*(P*n - n*v - r*s) * t**2 +
(2*P*r - 2*r*v - s**2) * t +
P*s - s*v