Before I begin the problem, I use P0, P1, P2, and P3 for the four cubic Bezier points, and 't' since it's parametric. Also, I have searched for a similar problem in this site, as well as Google, and couldn't find one. I apologize if this is a common question.
The problem: I am getting a slope of 0 for both dx/dt and dy/dt for cubic Beziers in these two cases
1: t = 0 and P0 == P1
2: t = 1 and P2 == P3
Here's an example to illustrate (1), where t = 0 and P0 == P1.
Find the tangent (i.e dx/dt and dy/dt) of the following cubic Bezier at t = 0:
(100, 100) (100, 100) (150, 150) (200, 100)
To find the tangent, we'd want the first derivative of the cubic Bezier:
Cubic Bezier definition
B(t) = (1-t)^3P0 + 3t(1-t)^2P1 + 3t^2(1-t)P2 + t^3P3
First derivative of a bezier curve (if you'd like to see the steps I used to get here, let me know)
B'(t) = (-3P0 + 9P1 - 9P2 + 3P3)t^2 + (6P0 - 12P1 + 6P2)t + (-3P0 + 3P1)
Plugging in t = 0 to the first derivative equation, we get
B'(0) = -3P0 + 3P1
And finally, recall that P0 = P1 = (100, 100), so dx/dt and dy/dt are:
dx/dt = dy/dt = -3*(100) + 3*(100) = 0
This tells me...there is no tangent at t = 0 for this cubic Bezier. Which makes no sense if you were to graph and look at it.
What I'm doing to get a non-zero slope is to: Treat the points P1, P2, and P3 like a quadratic Bezier, convert them into the equivalent cubic Bezier, THEN find the first derivative at t = 0. Is there any way I can avoid doing that? I'm finding it difficult to accept a tangent that has 0 for dx/dt and dy/dt. Thanks for your help.
The derivative B'(t)
at t = 0
is indeed undefined for case 1 (and at t = 1
for case 2).
To see why this is the case, we can run the de Casteljau algorithm "backwards" on your example to double the parameter range of the curve from t = 0 ... 1
to t = -1 ... 1
. This results in the following cubic Bezier curve control points:
(300,400) (0,-100) (100,200) (200,100)
If you plot this curve, you'll see your original curve from t = 0.5 ... 1
. You'll also see that there is a cusp at t = 0.5
on this extended curve, right at the beginning of your original. This cusp is why your curve is not differentiable at its starting point.
However, the tangent of the curve is not quite the same thing as the derivative. So if all you need is the tangent, you're in luck. (The derivative is tangent to the curve, but so is any other vector perpendicular to the curve's normal.)
It turns out that the tangents at the ends of the curve are generally equivalent to:
P1 - P0 at t = 0
P3 - P2 at t = 1
However, if (and only if) P0 = P1
and/or P2 = P3
, then the tangent at the degenerate point (that is, at t = 0
if P0 = P1
and/or t = 1
if P2 = P3
) is equivalent to:
P2 - P1
You can verify that this is the case by evaluating B'(t)
as t->0
.
In fact, if you split the extended curve in two at t = 0.5
and then apply the P2 - P1
equation to each side, you'll see that there are two different tangents at the cusp. The tangent for each half of the curve point in the exact opposite directions. This is another illustration of why the derivative is undefined at this point.
One final note: your trick of treating the points P1, P2, and P3 like a quadratic Bezier will also give you a correct tangent. However, this will not give you the correct derivative.