Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

This question is already correctly answered but I thought you'd like to know the underlying mathematics:

You are looking to find the end-slopes of a cubic bezier. Since the curve (i.e. its x and y values) is parametric in t, you will differentiate x and y w.r.t. t separately. The pair that you arrive at may be conceived of as the instantaneous "velocity" of a point travelling along the curve. So the point's initial velocity is zero (or more precisely a null-vector) in this case, but (most probably) the acceleration (or failing that, at least the rate of change of acceleration) will be non-zero, and hence the point's velocity will become non-zero (a non-null vector) and hence it will will move from those coordinates and trace out the curve.

But the slope as you visually perceive it is not parametric i.e. it does not depend on the time. I.O.W. what you are looking for is dy/dx, and not the pair (dx/dt, dy/dt), and given that dx/dt and dy/dt both are zero at t=0 for your curve, dy/dx = (dy/dt)/(dx/dt) = 0/0 which is indeterminate. To evaluate this, one must apply L'Hopital's rule. You can get the detailed treatment of the rule from the Wikipedia article but basically it means that to evaluate such indeterminate limits, we can differentiate the numerator f and denominator g separately to get f' and g' and then limit(f/g) is equal to limit(f'/g'). Now if p0, p1, p2 and p3 are the points defining your cubic, then:

dy / dt = ypart ( 3 * ( p1 - p0 ) + 6 * t * ( p2 - 2 * p1 + p0 ) + 3 * t ** 2 * ( p3 - 3 * p2 + 3 * p1 - p0 ) )

dx / dt = xpart ( 3 * ( p1 - p0 ) + 6 * t * ( p2 - 2 * p1 + p0 ) + 3 * t ** 2 * ( p3 - 3 * p2 + 3 * p1 - p0 ) )

-> (dy/dt) / (dx/dt) = ypart ( p1 - p0 ) / xpart ( p1 - p0 )

But this becomes indeterminate when p0 == p1. Now by L'Hopital's rule,

limit(t->0) [ (dy/dt) / (dx/dt) ] = limit(t->0) [ (d2y/dt2) / (d2x/dt2) ]

Now:

d2y/dt2 = ypart ( 6 * ( p2 - 2 * p1 + p0 ) + 6 * t * ( p3 - 3 * p2 + 3 * p1 - p0 ) )

d2x/dt2 = xpart ( 6 * ( p2 - 2 * p1 + p0 ) + 6 * t * ( p3 - 3 * p2 + 3 * p1 - p0 ) )

and at t = 0, (d2y/dt2) / (d2x/dt2) = ypart ( p2 - 2 * p1 + p0 ) / xpart ( p2 - 2 * p1 + p0 )

But since p1 == p0, this becomes: ypart ( p2 - p0 ) / xpart ( p2 - p0 ), which is exactly the result what Naaff told you. Note that if even p2 == p0 (a really degenerate curve, especially if cubic, in which case it will be just a straight line!), then even this will be indeterminate, and you can once more differentiate the numerator and denominator to get:

limit(dy/dx) = limit(t->0) [ (d3y/dt3) / (d3x/dt3) ] = ypart ( p3 - p0 ) / xpart ( p3 - p0 )

I hope it was useful for you... (BTW it doesn't seem as if TeX-like notation works here unlike on math.stackexchange, else I would provide math markup.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top