Home > published > ballAnimate.m

ballAnimate

PURPOSE ^

Animates a ball and moves it through the path described by arrays x and y

SYNOPSIS ^

function ballAnimate(x,y,trails)

DESCRIPTION ^

Animates a ball and moves it through the path described by arrays x and y
Creates a figure large enough to enclose x and y
if trails=0, Just shows the moving ball.
If trails=1, As the ball moves, it leaves a trail of its previous locations behind it.

When animation ends, the data is replotted (so it can be printed)
USAGE: t=linspace(0,10); v0=30; g=9.8; y=v0*t-0.5*g*t.^2; ballAnimate(t,y,0)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

function ballAnimate(x,y,trails)
%Animates a ball and moves it through the path described by arrays x and y
%Creates a figure large enough to enclose x and y
%if trails=0, Just shows the moving ball.
%If trails=1, As the ball moves, it leaves a trail of its previous locations behind it.
%
%When animation ends, the data is replotted (so it can be printed)
%USAGE: t=linspace(0,10); v0=30; g=9.8; y=v0*t-0.5*g*t.^2; ballAnimate(t,y,0)
close all
ballSize=12;
xmin=min(x);xmax=max(x);ymin=min(y);ymax=max(y)
h=ballSetup2(ballSize,'Time(s)','Height (m)',[xmin xmax ymin ymax]);

n=length(x);
set(h,'LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor','r','Marker','o')
% set(figure_handle,'DoubleBuffer','off') Can be helpful if Erasemode is
% normal for flicker free animation.  Renderer has to be set to painters.
%set(h,'EraseMode','normal')
if trails==0 set(h,'EraseMode','xor'); end
if trails==1 set(h,'EraseMode','none'); end
for i=1:n
    set(h,'Xdata',x(i),'Ydata',y(i))
    pause(0.01)
end
disp('Hit any key to create a printable figure'); pause
hold on
plot(x,y)
end

%====================================

function h=ballSetup2(ballsize, xtext, ytext, axissize)
%Prepares axes to show the motion of a ball
%Always creates as figure(1).  This script should be called
%by an animation script as the figure creation brings it to the foreground.
%
%Axes may be labeled arbitrarily.
%They may represent time and coordinate, or two coordinates.
%The limits are supplied by the user, and they need not be symettrical nor square.
%USAGE ball_setup2(8,'Time(s)','Height (m)',[-10 10 -3 7])
figure(1)
h=plot(0,0,'Marker','o','MarkerSize',ballsize,'MarkerFaceColor','w','MarkerEdgeColor','w');
as=axissize;
if as(1)>=as(2) error('You called ball_setup2 with incorrect x-limits'); end
if as(3)>=as(4) error('You called ball_setup2 with incorrect y-limits'); end
axis(as);
axis square; 
xlabel(xtext);ylabel(ytext);
end

Generated on Fri 07-Nov-2008 13:46:59 by m2html © 2005