# Intersections Between a Cubic Bézier Curve and a Line

If one end and a control point of a cubic Bézier curve is connected by a straight line, is there a simple way to find out whether this straight line intersects the Bezier curve? If it intersects then what will be the corresponding Bézier curve parameter's value?

Cubic Bezier curve and a straight line intersection tells two end point as a line, but I need one end point and one control point.

The calculations are roughly the same regardless of what line is used.

Suppose the line has equation $ax+by+cz = d$. In vector form, this is $$\mathbf{A}\cdot \mathbf{X} = d,$$ where $\mathbf{A} = (a,b,c)$. Also, suppose the Bézier curve has equation $$\mathbf{X}(t) = (1-t)^3\mathbf{P}_0 + 3t(1-t)^2\mathbf{P}_1 + 3t^2(1-t)\mathbf{P}_2 + t^3\mathbf{P}_3$$ Substituting this into the previous equation, we get $$(1-t)^3(\mathbf{A} \cdot \mathbf{P}_0) + 3t(1-t)^2(\mathbf{A} \cdot \mathbf{P}_1) + 3t^2(1-t)(\mathbf{A} \cdot \mathbf{P}_2) + t^3(\mathbf{A} \cdot \mathbf{P}_3) - d = 0$$ This is a cubic equation that you need to solve for $t$. The $t$ values you obtain (at most 3 of them) are the parameter values on the Bézier curve at the intersection points. If any of these $t$ values is outside the interval $[0,1]$, you'll probably want to ignore it.

In your particular case, you know that the line passes through either the start-point or the end-point of the Bézier curve, so either $t=0$ or $t=1$ is a solution of the cubic equation, which makes things easier. Suppose $t=0$ is a solution. Then the cubic above can be written in the form $$t(at^2 + bt + c) =0$$ Now you only have to find $t$ values in $[0,1]$ that satisfy $at^2 + bt +c =0$, which is easy.

• And you may need to require that $t \in [0,1]$. – lhf Jul 8 '17 at 13:24
• @lhf -- yes, good point. I'll add a comment about that. Thanks. – bubba Jul 8 '17 at 13:53

this can be solved analytically, by solving a cubic function within a cubic function, one rather complicated cubic function.

getting a points closest euclidean [distance to a cubic bezier] is solved on shadertoy.com

doing that for all points along a line gives you a cubic function with 1 or 2 local minima (or a line if all 3 CV are colinear AD line is parallel to the linear bezier).

for a quick estimation, cubic beziers are constrained to a triangular bounding volume (or line if the CV are colinear), this allows you to skip solving a big cubic function for many cases.