[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
2.2.5 Animation
You can make animation by several methods in MathGL: by export in animated GIF, or by save each frame in separate file (usually JPEG) and convert these files into movie. Let me show both methods.
The simplest methods is making animated GIF. There are 3 steps: (1) open GIF file by StartGIF()
function; (2) create the frames by calling NewFrame()
before and EndFrame()
after plotting; (3) close GIF by CloseGIF()
function. So the simplest code for “running” sinusoid will look like this:
int sample(mglGraph *gr, void *) { mglData dat(100); char str[32]; gr->StartGIF("sample.gif"); for(int i=0;i<100;i++) { gr->NewFrame(); // start frame gr->Box(); // some plotting sprintf(str,"sin(pi*x+%g*pi)",0.02*i); dat.Modify(str); gr->Plot(dat,"b"); gr->EndFrame(); // end frame } gr->CloseGIF(); return 0; }
The second way is saving each frame in separate file (usually JPEG) and later make the movie from them. MathGL have special function for saving frames – it is WriteFrame()
. This function save each frame with automatic name ‘frame0001.jpg, frame0002.jpg’ and so on. Here prefix ‘frame’ is defined by PlotId variable of mglGraph
class. So the similar code will look like this:
int sample(mglGraph *gr, void *) { mglData dat(100); char str[32]; for(int i=0;i<100;i++) { gr->NewFrame(); // start frame gr->Box(); // some plotting sprintf(str,"sin(pi*x+%g*pi)",0.02*i); dat.Modify(str); gr->Plot(dat,"b"); gr->EndFrame(); // end frame gr->WriteFrame(); // save frame } return 0; }
Created files can be converted to movie by help of a lot of programs. For example, you can use ImageMagic (command ‘convert frame*.jpg movie.mpg’), MPEG library, GIMP and so on.
Finally, you can use mgl2gif
tool for doing the same with MGL scripts (see section Utilities for parsing MGL).
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |