Find a tangent to two quadratic bezier curves

1.1k Views Asked by At

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?

2

There are 2 best solutions below

3
On BEST ANSWER

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$.

4
On

Here is an outline of an approach.

Take the point with parameter value $t$ on the first parabola. Let's say the position of this point is $\mathbf{P}(t)$ and the first derivative vector there is $\mathbf{P}'(t)$. Then the tangent line at this point can be described by the parametric equation $\mathbf{L}(u) = \mathbf{P}(t) + u \mathbf{P}'(t)$.

Next, we can eliminate the parameter $s$ from the equation of the second parabola, and get an equation of the form $ax^2 + by^2 +cxy +dx + ey + f = 0$. Or, in matrix form, $\mathbf{X}^T\mathbf{M}\mathbf{X} = 0$. Actually, any conic section curve can be written in this implicit form, not just parabolas. For an easy approach to implicitizing a quadratic curve in Bezier form, see section 17.6 in these notes by Tom Sederberg.

Now let's look at the points where the tangent line intersects this second parabola. This happens for values of $u$ that satisfy $\mathbf{L}(u)^T\mathbf{M}\mathbf{L}(u) = 0$. This is a quadratic equation in $u$. To get tangency to the second parabola, we need this quadratic to have equal roots. Write down the condition for equal roots. This condition will say that some function of $t$ is equal to zero. Solve for $t$.

It might still be pretty messy, but I think that's unavoidable.