http://www.mathworks.com/matlabcentral/newsreader/view_thread/305510
MATLAB Central Newsreader  Moving a straight line segment along the curve (Animation)
Feed for thread: Moving a straight line segment along the curve (Animation)
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Fri, 01 Apr 2011 19:52:05 +0000
Moving a straight line segment along the curve (Animation)
http://www.mathworks.com/matlabcentral/newsreader/view_thread/305510#828821
Liana
Hello,<br>
<br>
I have a curve specified as follows:<br>
cubic_spline = cscvn(points); % Data interpolation with a cubic spline<br>
[curve,t]=fnplt(cubic_spline,'r'); %"Natural" or periodic interpolating cubic spline curve<br>
path_length = curve_length(curve);<br>
fnplt(cubic_spline,'r');<br>
<br>
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?<br>
<br>
for i = 1:path_length<br>
xlabel('x');<br>
ylabel('y');<br>
hold on <br>
plot(curve(1,i),curve(2,i),'o'); <br>
M(:,i) = getframe;<br>
end<br>
<br>
Thanks a lot for any help!

Fri, 01 Apr 2011 21:03:04 +0000
Re: Moving a straight line segment along the curve (Animation)
http://www.mathworks.com/matlabcentral/newsreader/view_thread/305510#828830
Roger Stafford
"Liana" wrote in message <in5ad5$6np$1@fred.mathworks.com>...<br>
> Hello,<br>
> <br>
> I have a curve specified as follows:<br>
> cubic_spline = cscvn(points); % Data interpolation with a cubic spline<br>
> [curve,t]=fnplt(cubic_spline,'r'); %"Natural" or periodic interpolating cubic spline curve<br>
> path_length = curve_length(curve);<br>
> fnplt(cubic_spline,'r');<br>
> <br>
> 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?<br>
> <br>
> for i = 1:path_length<br>
> xlabel('x');<br>
> ylabel('y');<br>
> hold on <br>
> plot(curve(1,i),curve(2,i),'o'); <br>
> M(:,i) = getframe;<br>
> end<br>
> <br>
> Thanks a lot for any help!<br>
         <br>
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 splinefit 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.<br>
<br>
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 threehalves power of the quantity: one plus the square of the first derivative. Cubic polynomials have very welldefined second, as well as first, derivatives.<br>
<br>
Roger Stafford

Sat, 02 Apr 2011 00:25:05 +0000
Re: Moving a straight line segment along the curve (Animation)
http://www.mathworks.com/matlabcentral/newsreader/view_thread/305510#828868
Liana
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.<br>
<br>
"Roger Stafford" wrote in message <in5ei8$igt$1@fred.mathworks.com>...<br>
> "Liana" wrote in message <in5ad5$6np$1@fred.mathworks.com>...<br>
> > Hello,<br>
> > <br>
> > I have a curve specified as follows:<br>
> > cubic_spline = cscvn(points); % Data interpolation with a cubic spline<br>
> > [curve,t]=fnplt(cubic_spline,'r'); %"Natural" or periodic interpolating cubic spline curve<br>
> > path_length = curve_length(curve);<br>
> > fnplt(cubic_spline,'r');<br>
> > <br>
> > 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?<br>
> > <br>
> > for i = 1:path_length<br>
> > xlabel('x');<br>
> > ylabel('y');<br>
> > hold on <br>
> > plot(curve(1,i),curve(2,i),'o'); <br>
> > M(:,i) = getframe;<br>
> > end<br>
> > <br>
> > Thanks a lot for any help!<br>
>          <br>
> 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 splinefit 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.<br>
> <br>
> 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 threehalves power of the quantity: one plus the square of the first derivative. Cubic polynomials have very welldefined second, as well as first, derivatives.<br>
> <br>
> Roger Stafford

Sat, 02 Apr 2011 00:53:05 +0000
Re: Moving a straight line segment along the curve (Animation)
http://www.mathworks.com/matlabcentral/newsreader/view_thread/305510#828871
Roger Stafford
"Roger Stafford" wrote in message <in5ei8$igt$1@fred.mathworks.com>...<br>
> 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 splinefit 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.<br>
> <br>
> 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 threehalves power of the quantity: one plus the square of the first derivative. Cubic polynomials have very welldefined second, as well as first, derivatives.<br>
> <br>
> Roger Stafford<br>
         <br>
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:<br>
<br>
K = ((dxds)*(d2yds2)(dyds)*(d2xds2))/((dxds)^2+(dyds)^2)^(3/2)<br>
<br>
where dxds and dyds designate the first derivatives with respect to s and d2xds2 and d2yds2 are the second derivatives.<br>
<br>
The tangent line segment should have direction cosines proportional to (dxds) and (dyds).<br>
<br>
Roger Stafford