ETOOBUSY 🚀 minimal blogging for the impatient
Extremes for Bézier curves
TL;DR
Using Derivatives for Bézier curves to find the extreme candidates useful for the bounding box.
In our quest to find the bounding box for Bézier curves (i.e. the minimum area rectangle that has sides parallel to the coordinate axes and that fully contains the curve) we did a step ahead in Derivatives for Bézier curves, because we found a way to easily find out the parameters of the derived polynomial.
Why the derivation? From calculus, we know that all values of t where the derivative of a function is zero are candidate positions for either a minimum or a maximum. Hence, it suffices to find where the derivative components are equal to zero and we will have candidate values of the parameter t to find the extreme points for either axes. We will of course also make sure that these values of t are admissible (i.e. fall in the regular [0,1] interval for it) and also check the values at the two sides of the interval for t itself.
We left Derivatives for Bézier curves with this formula to calculate the coefficients of the polynomials for the derivative in the two axes:
MD,n⋅ˆPAs anticipated, these matrices can be pre-calculated and used when necessary upon the specific set of control points ˆP, which of course depends on the specific Bézier curve we are considering.
The generic solution would involve finding the zeros of a polynomial of any degree. For quadratic and cubic Béziers, instead, we are lucky because it’s very easy to find the roots.
Quadratic Bézier curves
For quadratic Bézier curves this means using the following matrix:
MD,2=M1⋅D2=[10−11]⋅2⋅[−1100−11]MD,2=2⋅[−1101−21]Multiplying this matrix by the matrix ˆP obtained putting the three control points as row vectors we end up with the following:
[qm]=[qxqymxmy]=2⋅[−P1+P2P1−2⋅P2+P3]The two row vectors q and m represent the intercept and the angular coefficient of the derivatives for the two components over the X and Y axes. Hence, it is trivial to find the two candidates for the bounding box:
tx=−qxmxty=−qymyCubic Bézier curves
For cubic Bézier curves we have instead:
MD,3=M2⋅D3=[100−2201−21]⋅3⋅[−11000−11000−11]MD,3=3⋅[−11002−420−13−31]Again, we can multiply by the matrix of the control points and obtain:
[cba]=[cxcybxbyaxay]=3⋅[−P1+P22⋅(P1−2⋅P2+1⋅P3)−P1+3⋅P2−3⋅P3+P4]Here, the three row vectors a, b, and c hold the parameters for the second-degree equations that allow us to find the zeros in the two dimensions X and Y. Note that b can be easily divided by 2, so we can find the roots as follows:
tx,12=−bx2±√(bx2)2−ax⋅cxaxty,12=−by2±√(by2)2−ay⋅cyay