2015-01-16 30 views
0

當我編寫一個C++程序,包括通過管道使用GNU-Plot時,繪製了繪圖,但是缺少所有的x11交互性,例如,下面的代碼基於HEREC++ GNU-Plot在x11窗口中是非交互的

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 x11\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, "splot cos(x)+cos(y)\n"); 

    pclose(pipe); 
    return 0; 
} 

不過,如果我打開一個命令行,運行gnuplot,然後手動輸入所有的命令,全交互存在,即,縮放,旋轉等...

有人知道如何通過C++程序調用GNU-Plot來獲得交互性嗎?

回答

4

僅當gnuplot主進程正在運行時才能與gnuplot進行交互。關閉管道後,主gnuplot進程退出,並且它留下的gnuplot_x11進程不再處理輸入。

解決方案是讓管道保持打開狀態,並且只有當您不想再使用該圖時才關閉它。你可以有以下改動嘗試了這一點:

#include <stdio.h> 

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 x11\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, "splot cos(x)+cos(y)\n"); 

    fflush(pipe); // force the input down the pipe, so gnuplot 
       // handles the commands right now. 

    getchar(); // wait for user input (to keep pipe open) 

    pclose(pipe); 
    return 0; 
} 

有了這個,在窗口中的情節可以被處理,直到你按下你的程序運行(然後程序關閉管道的控制檯輸入,gnuplot的退出,並停止輸入處理)。

+0

感謝百萬... –