Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Moving a straight line segment along the curve (Animation)

Subject: Moving a straight line segment along the curve (Animation)

From: Liana

Date: 1 Apr, 2011 19:52:05

Message: 1 of 4

Hello,

I have a curve specified as follows:
cubic_spline = cscvn(points); % Data interpolation with a cubic spline
[curve,t]=fnplt(cubic_spline,'-r'); %"Natural" or periodic interpolating cubic spline curve
path_length = curve_length(curve);
fnplt(cubic_spline,'-r');

Now I'd like to make a simple animation. I want a straight line segment to move along the 'curve'. So far I can only move a point. Well, further I will need to rotate the line while moving. However, for the beginning I need just to move it without any rotation. How can I do that?

for i = 1:path_length
    xlabel('x');
    ylabel('y');
    hold on
    plot(curve(1,i),curve(2,i),'o');
    M(:,i) = getframe;
end

Thanks a lot for any help!

Subject: Moving a straight line segment along the curve (Animation)

From: Roger Stafford

Date: 1 Apr, 2011 21:03:04

Message: 2 of 4

"Liana" wrote in message <in5ad5$6np$1@fred.mathworks.com>...
> Hello,
>
> I have a curve specified as follows:
> cubic_spline = cscvn(points); % Data interpolation with a cubic spline
> [curve,t]=fnplt(cubic_spline,'-r'); %"Natural" or periodic interpolating cubic spline curve
> path_length = curve_length(curve);
> fnplt(cubic_spline,'-r');
>
> Now I'd like to make a simple animation. I want a straight line segment to move along the 'curve'. So far I can only move a point. Well, further I will need to rotate the line while moving. However, for the beginning I need just to move it without any rotation. How can I do that?
>
> for i = 1:path_length
> xlabel('x');
> ylabel('y');
> hold on
> plot(curve(1,i),curve(2,i),'o');
> M(:,i) = getframe;
> end
>
> Thanks a lot for any help!
- - - - - - - - - -
  I presume when you say "rotate the line" you mean that you want the line segment to be tangent to the interpolated curve. You could accomplish that by using the 'ppform', which is the output of 'cscvn', to directly calculate the derivative of the cubic polynomial within whatever cubic section of the spline-fit the point of tangency is located. That establishes the slope desired for the line segment. In your animation this would of course have to be repeated for each point of tangency along the curve.

  Yesterday in a different thread you inquired about curvature in connection with the use of 'cscvn'. You can also use its 'ppform' output to directly calculate the curvature of cubic sections at any desired points within them. The formula is well known in calculus: curvature is equal to the second derivative divided by the three-halves power of the quantity: one plus the square of the first derivative. Cubic polynomials have very well-defined second, as well as first, derivatives.

Roger Stafford

Subject: Moving a straight line segment along the curve (Animation)

From: Liana

Date: 2 Apr, 2011 00:25:05

Message: 3 of 4

Thank you very much, Roger! This info was very helpful. I'll come back to this thread in case of further questions that may arise during the coding.

"Roger Stafford" wrote in message <in5ei8$igt$1@fred.mathworks.com>...
> "Liana" wrote in message <in5ad5$6np$1@fred.mathworks.com>...
> > Hello,
> >
> > I have a curve specified as follows:
> > cubic_spline = cscvn(points); % Data interpolation with a cubic spline
> > [curve,t]=fnplt(cubic_spline,'-r'); %"Natural" or periodic interpolating cubic spline curve
> > path_length = curve_length(curve);
> > fnplt(cubic_spline,'-r');
> >
> > Now I'd like to make a simple animation. I want a straight line segment to move along the 'curve'. So far I can only move a point. Well, further I will need to rotate the line while moving. However, for the beginning I need just to move it without any rotation. How can I do that?
> >
> > for i = 1:path_length
> > xlabel('x');
> > ylabel('y');
> > hold on
> > plot(curve(1,i),curve(2,i),'o');
> > M(:,i) = getframe;
> > end
> >
> > Thanks a lot for any help!
> - - - - - - - - - -
> I presume when you say "rotate the line" you mean that you want the line segment to be tangent to the interpolated curve. You could accomplish that by using the 'ppform', which is the output of 'cscvn', to directly calculate the derivative of the cubic polynomial within whatever cubic section of the spline-fit the point of tangency is located. That establishes the slope desired for the line segment. In your animation this would of course have to be repeated for each point of tangency along the curve.
>
> Yesterday in a different thread you inquired about curvature in connection with the use of 'cscvn'. You can also use its 'ppform' output to directly calculate the curvature of cubic sections at any desired points within them. The formula is well known in calculus: curvature is equal to the second derivative divided by the three-halves power of the quantity: one plus the square of the first derivative. Cubic polynomials have very well-defined second, as well as first, derivatives.
>
> Roger Stafford

Subject: Moving a straight line segment along the curve (Animation)

From: Roger Stafford

Date: 2 Apr, 2011 00:53:05

Message: 4 of 4

"Roger Stafford" wrote in message <in5ei8$igt$1@fred.mathworks.com>...
> I presume when you say "rotate the line" you mean that you want the line segment to be tangent to the interpolated curve. You could accomplish that by using the 'ppform', which is the output of 'cscvn', to directly calculate the derivative of the cubic polynomial within whatever cubic section of the spline-fit the point of tangency is located. That establishes the slope desired for the line segment. In your animation this would of course have to be repeated for each point of tangency along the curve.
>
> Yesterday in a different thread you inquired about curvature in connection with the use of 'cscvn'. You can also use its 'ppform' output to directly calculate the curvature of cubic sections at any desired points within them. The formula is well known in calculus: curvature is equal to the second derivative divided by the three-halves power of the quantity: one plus the square of the first derivative. Cubic polynomials have very well-defined second, as well as first, derivatives.
>
> Roger Stafford
- - - - - - - - - -
  The curvature formula I mentioned needs a clarification. In the spline representation given by 'cscvn' using the 'ppform' format, the coordinates are given as cubic polynomial sections of a common parameter, namely an approximate arc length s. For that reason the curvature formula that applies should be the corresponding parametric form:

 K = ((dxds)*(d2yds2)-(dyds)*(d2xds2))/((dxds)^2+(dyds)^2)^(3/2)

where dxds and dyds designate the first derivatives with respect to s and d2xds2 and d2yds2 are the second derivatives.

  The tangent line segment should have direction cosines proportional to (dxds) and (dyds).

Roger Stafford

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us