# surface.c
#include
/*
Author: Ioan Vancea (www.vioan.ro)
*****************************/
int main()
{
FILE *pipe = popen("gnuplot -persist","w");
fprintf(pipe, "set samples 40\n");
fprintf(pipe, "set isosamples 40\n");
fprintf(pipe, "set hidden3d\n");
fprintf(pipe, "set xrange [-8.000:8.000]\n");
fprintf(pipe, "set yrange [-8.000:8.000]\n");
fprintf(pipe, "set zrange [-2.000:2.000]\n");
fprintf(pipe, "set terminal png\n");
fprintf(pipe, "set output 'graph.png'\n");
fprintf(pipe, "set title 'We are plotting from C'\n");
fprintf(pipe, "set xlabel 'Label X'\n");
fprintf(pipe, "set ylabel 'Label Y'\n");
/* fprintf(pipe, "plot 'datafile.dat' using 1:2\n");*/
fprintf(pipe, "splot cos(x)+cos(y)\n");
close(pipe);
return 0;
}
也可以动态显示图形:
# popen.c
#include
#include
#include
#include
#include
#include
#define PANIC(a) do { \
perror(a); \
if (temp_name) unlink(temp_name);\
exit(1);\
} while(0)
int main() {
FILE *command,*data;
char *temp_name;
double a,b;
int i;
if ((temp_name = tmpnam((char *) 0)) == 0) PANIC("tmpnam failed");
if(mkfifo(temp_name, S_IRUSR | S_IWUSR) != 0) PANIC("mkfifo failed");
command = popen("gnuplot","w");
fprintf(command,"plot \"%s\" with lines\n",temp_name); fflush(command);
data = fopen(temp_name,"w");
for (i=0; i<20; i++) {
a = i/10.0;
b = sin(a);
fprintf(data,"%f %f\n",a,b);
}
fclose(data);
fprintf(stderr,"press enter to continue..."); fflush(stderr);
getchar();
fprintf(command,"plot \"%s\" with lines\n",temp_name); fflush(command);
data = fopen(temp_name,"w");
for (i=0; i<20; i++) {
a = i/10.0;
b = cos(a);
fprintf(data,"%f %f\n",a,b);
}
fclose(data);
fprintf(stderr,"press enter to continue..."); fflush(stderr);
getchar();
fclose(command);
unlink(temp_name);
return 0;
}
没有评论:
发表评论