/* * drawgraph_eggx.c --- drawgraph.c の EGGX 版 * コンパイル: egg drawgraph_eggx.c -o drawgraph_eggx */ #include #include #include double f(double); int main() { int win; int i, n; double a, b, xmin, xmax, ymin, ymax; double x, y, dx, pi; /* 円周率を求めておく */ pi = 4 * atan(1.0); /* EGGX を初期化する */ win = eggx_gopen(500, 500); /* ウィンドウをクリアする */ eggx_gsetbgcolor(win, "black"); eggx_gclr(win); /* 線の色を赤にする */ eggx_newcolor(win, "red"); /* グラフを描く範囲 [a,b] */ a = 0; b = 2 * pi; /* スクリーンに表示する範囲を指定 */ xmin = a - (b - a) / 10; xmax = b + (b - a) / 10; ymin = - 1.2; ymax = 1.2; eggx_window(win, xmin, ymin, xmax, ymax); /* 1変数関数 f のグラフ */ n = 100; dx = (b - a) / n; x = a; y = f(x); eggx_line(win, x, y, PENUP); for (i = 1; i <= n; i++) { x = a + i * dx; y = f(x); eggx_line(win, x, y, PENDOWN); } /* PNG形式で保存。"display mygraph.png" で再表示可能。 */ eggx_saveimg(win, 0, xmin, ymin, xmax, ymax, "pnmtopng", 256, "mygraph.png"); /* PostScript形式で保存。"ggv mygraph.eps" で再表示可能。 */ eggx_saveimg(win, 0, xmin, ymin, xmax, ymax, "pnmtops -noturn -rle", 256, "mygraph.ps"); eggx_saveimg(win, 0, xmin, ymin, xmax, ymax, "convert", 256, "mygraph.eps"); /* VOGLE のウィンドウ内でキー入力を待つ (入力された文字を捨てる) */ eggx_ggetch(win); /* VOGLE の終了 */ eggx_gclose(win); return 0; } double f(double x) { return sin(x) * cos(x); }