-
Notifications
You must be signed in to change notification settings - Fork 0
Bezier Curve SDF Derivation
Hello friends, It's been almost a month since my last article. The main reason is that the derivation for this one really exceedeed my capabilities and there a a lack of relveant materials. making it ver challenging to digest. The highlights of this article include
- Friendly to beginners: It's been over a decade since I last used math. and my math level is basically thata of a middle school student. In hte process of understaning, I encountered many caued-and-effect explanations that were completely unclear to me. The reason for this that I had forgetten many formulas and theorems, or simply never know then. Through continuously consulting materials. I finnaly managed to write out the entire derivation prcoess. This article supplements a lot of this foundational knowledge. and I believe it will also be suitable for beginners, as I was one myself
- Consistent marker, symbols and variables: At first glance of sdf shader code, I couldn't understand how the code was working. Later when I found some meterials with the entire proof process, I still didn't understand it. A big part of the reason was the symbols in the code and in the mathematical formulas were completely different. In this article, the symbols for legends, proofs, and code are all the same.
- Proved: Using
De Moivre's theorem
andFermat's lemma
with diagrams, that the third root cannot be the minimum value.
The chain rule is a fundamental rule in calculus for differentiating composite functions.
Polynomial differentiation is a basic concept in calculus,
Where
- Power rule: if
$f(x) = x^n$ , where$n$ is any real number, then$f'(x) = n \cdot x^{n-1}$ . - Constant rule: If
$C$ is a constant, then the derivative of$f(x) = C$ is 0. That is$C' = 0$
Cubic unit roots, also known as the cube roots of unity, are a special type of number in complex numbers that are solutions to the equation
The triple angle formula is an important formula in trigonometry that relates the trigonometric values of a triple angle to the trigonometric values of a single angle.
The most beautiful formula in the world, it connects exponential operations of complex numbers with trigonometric functions
When we seek the
An equation of the form $$ x^3 + px + q = 0 $$ which lacks a quadratic term, is known as the “Depressed form” or “Cardano's form.” Its roots are given by the following formula: $$ x = \sqrt[3]{-\frac{q}{2} + \sqrt{\left(\frac{q}{2}\right)^2 + \left(\frac{p}{3}\right)^3}} + \sqrt[3]{-\frac{q}{2} - \sqrt{\left(\frac{q}{2}\right)^2 + \left(\frac{p}{3}\right)^3}} $$
To use Cardano's equation, any cubic equation of the form
When using Cardano's method
, we often encounter a quanity known as the Cardano's discriminant
This quantity tells us about the nature of the equation's roots:
- If
$\Delta > 0$ , then the equation has one real root and two complex roots. - If
$\Delta = 0$ , then all roots are real, and at least two roots are equal. - If
$\Delta < 0$ , then the equation has three distinct real roots.
Viète's formulas, are a set of theorems about the relationship between the roots of a polynomial equation and its coefficients. These theorems hold for any polynomial equation with real or complex coefficients.
$$ a_nx^n + a_{n-1}x^{n-1} + \dots + a_1x + a_0 = 0
\implies
\begin{cases} x_1 + x_2 + \dots + x_n = -\frac{a_{n-1}}{a_n} \ x_1x_2 + x_1x_3 + \dots + x_{n-1}x_n = \frac{a_{n-2}}{a_n} \ x_1x_2 \dots x_n = (-1)^n \frac{a_0}{a_n} \end{cases} $$
Fermat's Lemma is a fundamental result in calculus which states that:
If a function
Bézier curves result from linear interpolation. If we know the distance between two points and want to find a new point 20% of the distance away from the first point (that is, 80% of the distance away from the second point), we can calculate it as follows:
In a 2D coordinate system with points A, B, C, and variable t, where t's value ranges from
vec2 Bezier(vec2 a, vec2 b, vec2 c, float t) {
return mix(mix(a, b, t), mix(b,c,t), t);
}
Also $$ mix(a, b, t) = a\cdot(1-t) + b\cdot t $$
In mathmatical formulas $$ \begin{aligned}
P(t) &= (1 - t)^2 \cdot A + 2 \cdot (1 - t) \cdot t \cdot B + t^2 \cdot C \ &=(1-2t+t^2) \cdot A + (2t - 2t^2) \cdot B + t^2 \cdot C \
let f(t) = P(t) - p
\implies f(t)
&= (A-2B+C) \cdot t^2 + 2 \cdot (B-A) \cdot t + A-p \
\
let
\begin{cases}
a=B-A \
b=A-2B+C \
c=2(B-A) = 2a \
d=A-p
\end{cases}
\implies f(t)
&= b \cdot t^2 + c \cdot t + d
\end{aligned} $$
Thus, the problem of the distance field for the Bézier curve becomes $$ what \ value \ t, the \ minimum \ value \ of \ |f(t)| $$
This problem is equivalent to
Thus, it becomes a problem of finding the extremum, where the extremum must be point where derivative equals 0. Using the chain rule of differnetiation, it can be calculated as follows
Applying basic polynomial differentiation
Finally, the problem becomes one of solving a cubic polynomial.
To find the turning points. take the second derivative and shift to zero location(change of variables) to eliminate the quadratic term.
$$ \begin{aligned} f(x)=x^3 + a_2x^2 + a_1x + a_0 &\implies f''(x) = 6x + 2a_2 =0 \ x\to x - \frac{a_2}{3} &\implies x^3 + (a_1-\frac{a_2^2}{3})x + (a_0 + \frac{a_2*(2a_2^2 - 9a_1)}{27}))= 0
\end{aligned}
$$
Using Cardano's formula variables
$$
\begin{aligned}
let \ x\to s+t &\implies x^3 = 3st(s+t) + s^3 + t^3 \
&\implies x^3 = 3st\cdot x + s^3 + t^3
\end{aligned}
$$
By comparing coefficients from Equations 1 and 2 as above,
$$
\begin{cases}
s^3 \cdot t^3 = m^3 \
s^3 + t^3 = 2n
\end{cases}
$$
According to Vieta's formulas,
unfolding the equation gives us $$ x = s+t = \sqrt[3]{ n + \sqrt{n^2-m^3}} + \sqrt[3]{n - \sqrt{n^2-m^3}} $$ Applying the cubic roots of unity allows for full expansion
$$ \implies x= \begin{cases} \sqrt[3]{ n + \sqrt{n^2-m^3}} + \sqrt[3]{n - \sqrt{n^2-m^3}} \ \omega \sqrt[3]{ n + \sqrt{n^2-m^3}} + \omega^2\sqrt[3]{n - \sqrt{n^2-m^3}} \ \omega^2\sqrt[3]{ n + \sqrt{n^2-m^3}} + \omega \sqrt[3]{n - \sqrt{n^2-m^3}} \
\end{cases} \ \omega = e^{\frac{2\pi}{3}i} = -\frac12 + \frac{\sqrt{3}}{2}i $$
The issue with this solution is that it only becomes clear which roots are real after computing each formula in sequence.
This method avoids the issues with square roots and uncertainty of real roots encountered in the previous method.
Using the triple angle formula, we have $$ \cos(3\theta) = 4\cos^3 \theta - 3\cos \theta $$ Similarly, this is a cubic equation, so if we have the following cubic equation, it can be inferred that
$$ 4x^3 - 3x - cos3\theta = 0 \implies x = cos\theta $$ Recalling our equation from the previous section
$$
x^3 = 3px + 2q
$$
If we make a substitution such that equation satisfies the triple angle formula, we can directly obtain the solution
$$ \begin{aligned} x = 2\sqrt{p} \ C &\implies 4C^3 - 3C = \frac{q}{p\sqrt{p}} = cos3\theta \ &\implies x = 2\sqrt{p} cos(\frac{\phi +2k\pi}{3}), \phi = arccos(\frac{q}{p\sqrt{p}}) , k = 0, 1, 2 \end{aligned} $$ This solution is the method used in code.
Returning to our equations
$$
\begin{cases}
s^3 = q + \sqrt{q^2-p^3} \
t^3 = q - \sqrt{q^2-p^3}
\end{cases}
$$
Let's consider the situation with three real roots when
The magnitude
Placing
Taking the cube root of
For the root 0 $$ x = s_0 + t_0 = 2RealPart(s_0) = 2\sqrt{p} \ \ cos(\phi/3) $$
By adding
Let's return to expressing the final roots in terms of
$$ \phi \in [0, \pi] \implies \frac{\phi}{3} = \theta \in [0, \frac{\pi}3] $$ geogebra: https://www.geogebra.org/classic/u8uuxw9u
For any
Returning to the function of the quadratic bezier curve, we have
$$ \begin{aligned} f(t) &= b \cdot t^2 + c \cdot t + d \ \frac{d}{dt}[f(t)]^2 = g(t) &= 2b^2 \cdot t^3 + 3bc \cdot t^2 + (c^2 + 2bd) \cdot t + dc \ g'(t) &= 6b^2t^2 + 6bct + c^2 +2bd \end{aligned} $$
The extremal points of
geogebra: https://www.geogebra.org/classic/qdrdxycn
- There are local maxima at
$t_0$ and$t_1$ , while$t_2$ is a local minimum. - There are local minima at
$t_0$ and$t_1$ , while$t_2$ is a local maximum.
The graphs of the second derivative
Given that $g'(t) = 6b^2t^2 + 6bct + c^2 +2bd$ Because
$$ g'(t_2) < max(g'(t_0), g'(t_1)) < 0 \tag{4} $$ Inequalities (3) and (4) contradict each other, thus situation 1 does not exist.
Therefore, the closest distance from the Bézier curve must be at the root either