C言語+gnuplot+popenを使った簡単なアニメーション作成プログラム

popenコマンドを使ってC言語からgnuplotを呼び出す短いプログラム。



$ gcc a.c && ./a.out



でサイン関数のアニメーションが作成される。


a.c



#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double t,dt;
int main(){

double t=0,dt=0.01;
FILE *gp;
 gp = popen("gnuplot --persist","w");
 fprintf(gp,"set yrange [-1:1]\n");
 while(1){
   fprintf(gp,"plot %lf*sin(x-%lf)\n",sin(t),t);
   t+=dt;
 }
 pclose(gp);
 return 0;
}