練習問題



next up previous contents

Next: <コラム:makeでコンパイル&リンクを楽にしよう> Up: 4 絵を表示させる Previous: 練習問題


練習問題

ウィンドウ上に sin関数のグラフを表示してください。また、表示したグラ フをTgifで読み込み目盛やグラフの説明などを付け加えて下さい。

(注意) tgif を起動する時は -rv オプションを付けて起動して下さい。

-----------------------
/* "ex4-2.c" --- sin関数のグラフを表示する */

#include <stdio.h>
#include <math.h>
#include <Xtc.h>

#define ORIGIN_X        100
#define ORIGIN_Y        200
#define REAL_X_SIZE     M_PI
#define REAL_Y_SIZE     1.0
#define DISPLAY_X_SIZE  200
#define DISPLAY_Y_SIZE  100

/* 座標変換 */
#define gx(x) ( ... )			/* ... の部分を完成せよ */
#define gy(y) ( ... )			/* ... の部分を完成せよ */

void main(void)
{
    int  x,y;
    double rad;

    initgraph();                      /* グラフィックス環境の初期化 */
    openterminal(TGIF, "sin.obj");    /* TGIF形式でファイル名"sin.boj"に
                                             グラフを保存する */
    setcolor( WHITE );                /* 座標軸の表示色設定 */
    line( gx(0.0), gy( 0.0), gx(2*M_PI), gy(0.0) );  /* x軸の描画 */
    line( gx(0.0), gy(-1.0), gx(0.0),    gy(1.0) );  /* y軸の描画 */
    outtextxy( gx(0.0) - 20, gy(0.0) + 20, "0" );    /* 原点表示  */

    setcolor( GREEN );                           /* グラフの表示色設定 */
    moveto(gx(0.0), gy(0.0));
    for( rad = 0; rad <= 2*M_PI; rad += M_PI/36 ) {
        lineto( gx(rad), gy(sin(rad)) );         /* グラフ(点)の描画 */
    }

    xtcmainloop( 3 );                      /* 終了入力待ち */
    closeterminal(TGIF);                   /* ファイル出力終了 */
    closegraph();                          /* グラフィックス環境の終了 */
}
-----------------------



next up previous contents

Next: <コラム:makeでコンパイル&リンクを楽にしよう> Up: 4 絵を表示させる Previous: 練習問題




機械・知能系 コンピュータ実習担当教官