Moving an object along a Bezier Curve. Part III

In my previous post, Moving an object along a Bezier Curve. Part II, I discussed how to compute the first derivative of a Bezier curve in order to find its tangent. Today, we will find the 2nd derivative of a beizer curve.

The 2nd derivative is computed by

 \frac{ (d^2)}{(dt^2)} = ((1-t)^3 P_0+3 (1-t)^2 t P_1+3 (1-t)^2 P_2+t^3 P_3) =

 6 (P_0 (-(t-1))+P_1 (3 t-2)+P_3 t+P_2)
.
.
local px2ndDer = 6 * (p1.x * ( -1 * ( 1-t )) + p2.x * ( (3 * t) -2 ) + ( p4.x * t ) + p3.x )
local py2ndDer = 6 * (p1.y * ( -1 * ( 1-t )) + p2.y * ( (3 * t) -2 ) + ( p4.y * t ) + p3.y )
.
.

Why is it important to find the 2nd derivative of a curve in regards to moving an object along a path? It is to calculate the curvature radius of the path at specified parameter t. It measures the rate of change of direction of the curve. It is the tangent’s line turn per unit distance moved along the curve.

To compute the curvature, using the first and second derivative, it is as follows:
  K = \frac { \begin{vmatrix} x'y'' - y'x'' \end{vmatrix} } { (x'^2 + y'^2)^\frac{3}{2} }

To compute the curvature radius, it is easy, its the inverse. And it is denoted as follows:

  R = \frac { (x'^2 + y'^2)^\frac{3}{2} } { \begin{vmatrix} x'y'' - y'x'' \end{vmatrix} }

Carlos

Coding as a skill is becoming a casualty of efficiency

Mashable discusses the efficiency of new tools for non programmers (and even for programmers) and the only name dropped is that of our Corona SDK.

They got it. Corona is for everyone, from 14 year olds like Robert Nay whose app became number 1 on the app store, tumbling “Angry Birds Seasons”, to studios, to ad-agencies, you name it. Corona SDK is becoming a standard in new app development framework. Mashable on Nay’s use of Corona, He’s a pioneering user of the next generation of platform dependencies — innovations upon which further innovations can be built.”

Mashable goes on to state Coding is a means to an end, and if new methods are developed that enable us normal folks to achieve comparable results, then that’s a win in my book.” And I couldn’t agree more.

To read more about what Mashable has to say, read http://mashable.com/2011/05/13/developer-platforms-jobs/

Carlos

Calculating The Length of a Bezier Curve. Part I

One of the fastest way to calculate the length of a bezier curve is by breaking down the curve into straight-line segments, and then, obviously, add the length of each segment to get the final length of the curve.

Looking at the code from my previous post “The Cubic Bezier Form and Code”, you can see where each segment is calculated. All that we would need to do is to add a hold value to hold the previous segment end point and compute the distance to the current start of the new segment.

.
.
.

local length = 0;

if ( j  > 1 ) then

    local xx = x - prevPt.x
    local yy = y - prevPt.y

    length = length + math.sqrt((xx*xx) + (yy*yy))

end

prevPt.x = x;
prevPt.y = y;

.
.
.

This method is what I would consider a brute force method. I will do a follow up on finding the length of Bezier curves by using subdivision, and arc-length computation, To learn more about calculating the length of a bezier curve, see Jens Gravesen: “Adaptive subdivision and the length of Bezier curves” and Gravesen, Jens, “The Length of Bézier Curves”, Graphics Gems V, p. 199-205, code: ch4-7.

Moving an object along a Bezier Curve. Part II

Part II of our of our journey in having an object move along a Bezier curve, and moving towards re-parametrizing the curve, we first need to understand a very important aspect of a specific attribute of a Bezier curve and that is computing its tangents via its first derivative.

By using the form on my first post on Beziers,

   \mathbf{P}(t)= (1 -t) ^3 \mathbf{P}_0+3(1-t)^2t\mathbf{P}_1+3(1-t)t^2\mathbf{P}_2+t^3\mathbf{P}_3

we can easily observe the first derivative is obtained as follows

   c'(t)=-3(i-t)^2\mathbf{P}_0+3((1-t)^2-2t(1-t))\mathbf{P}_1+3(2t(1-t)-t^2)\mathbf{P}_2+3t^2\mathbf{P}_3

which in turn can be reduced to

   c'(t)=3(\mathbf{P}_1-\mathbf{P}_0)(1-t)^2+3(\mathbf{P}_2-\mathbf{P}_1)2t(1-t)+3(\mathbf{P}_3-\mathbf{P}_2)t2

lastly, giving us

   c'(t)=3(1-t)^2(\mathbf{P}_1-\mathbf{P}_0)+6t(1-t)(\mathbf{P}_2-\mathbf{P}_1)+3t2(\mathbf{P}_3-\mathbf{P}_2)
local pax = 3*(p2.x-p1.x)*((1-t) *(1-t));
local pbx = pax + 6*t*(p3.x-p2.x)*(1-t)
local pcx = pax + pbx + 3*(p4.x-p3.x)*(t*t);

local pay = 3*(p2.y-p1.y)*((1-t) *(1-t));
local pby = pay + 6*t*(p3.y-p2.y)*(1-t)
local pcy = pay + pby + 3*(p4.y-p3.y)*(t*t);

And why is this important? Because when you want to do text on a path, move the text object along the path, (as well as an object that you want to be perpendicular to the curve) it needs to project away from it. Thus the Y direction is perpendicular to the curve.

Here is an image of the tangents of a curve at a given t, perpendicular to the curve. You can also see the parametrization not being equal in the corner cases of the curve.