I have two quadratic bezier curves and I want to find whether they have a common tangent, and if so where it is.
If the first bezier is defined by points $(x_1, y_1)$, $(x_2, y_2)$, $(x_3, y_3)$, then the points on the curve have coordinates:
$x = (x_1 - 2x_2 + x_3)t^2 + (2x_2 - 2x_1)t + x_1 \\ y = (y_1 - 2y_2 + y_3)t^2 + (2y_2 - 2y_1)t + y_1$
Or to simplify things:
$x = A_xt^2 + B_xt + C_x \\ y = A_yt^2 + B_yt + C_y$
Likewise, I can get an equation for the second bezier:
$x = D_xs^2 + E_xs + F_x \\ y = D_ys^2 + E_ys + F_y$
The gradient of the tangent, $m$, to the first curve at $t$ is $\dfrac{2A_yt + B_y}{2A_xt + B_x}$,
which must be equal to the tangent to the second curve at $s$, $\dfrac{2D_ys + E_y}{2D_xs + E_x}$.
So $m = \dfrac{2A_yt + B_y}{2A_xt + B_x} = \dfrac{2D_ys + E_y}{2D_xs + E_x}$
Which I rearranged to get:
$t = \dfrac{2(B_yD_x - D_yB_x)s + B_yE_x - B_xE_y}{4(A_xD_y - A_yD_x)s + 2(A_xE_y - A_yE_x)}$
The gradient of the tangent is also equal to the difference in the y-coordinates over the difference in x-coordinates:
$m = \dfrac{D_ys^2 + E_ys + F_y - (A_yt^2 + B_yt + C_y)}{D_xs^2 + E_xs + F_x - (A_xt^2 + B_xt + C_x)}$
But that's as far as I got. The algebra looks too messy from here. Can anyone solve this, or is there a better approach?
A single, closed-form solution to this is going to be unavoidably messy. If you take it in steps, however, it’s manageable.
Let $P_1$, $P_2$, $P_3$ be the control points of the first quadratic Bézier and $Q_1$, $Q_2$, $Q_3$ be the control points of the second, so that we have the parameterizations $B_1:t\mapsto(1-t)^2P_1+2t(1-t)P_2+t^2P_3$ and $B_2:s\mapsto(1-s)^2Q_1+2s(1-s)Q_2+s^2Q_3$, respectively. Quadratic Bézier curves have a nice property: the tangent line at $B_1(t)$ passes through the points $(1-t)P_1+tP_2$ and $(1-t)P_2+tP_3$. So, if there is a common tangent, then there is some value for $s$ and $t$ such that these two points and the points $(1-s)Q_1+sQ_2$ and $(1-s)Q_2+sQ_3$ are colinear. A direct approach from here will give you a quadratic equation in $s$ and $t$ that you can try to solve. Another possible approach is to compute the intersections $R_1$ and $R_2$ of this tangent line to $B_1$ with the lines $\overline{Q_1Q_2}$ and $\overline{Q_2Q_3}$, respectively, and then see if ${\|P_1-Q_1\|\over\|Q_2-Q_1\|}={\|P_2-Q_2\|\over\|Q_3-Q_2\|}$, using signed distances, i.e., see if the two intersection points correspond to the same value of $s$.