プロが教える店舗&オフィスのセキュリティ対策術

一つ下の質問をした後に、いろいろ考えて作ったプログラムなのですが、コンパイルが通りません。
頭がこんがらがってしまったので、教えていただけるとうれしいです。

main(){
   int x, y, r, d;

   printf("Enter coordinates of radius of circle. R = ");
   scanf("%d\n", &r);

   x = 0;
   y = r;
   d = 3 - 2 * r;

   while(y > x){
     plotcircle(x, y, r);
     if(d < 0){ /* select U */
        d = d + 4 * x + 6;
     }else{ /* select D */
        d = d + 4 * (x - y) + 10;
        y = y - 1;
     }
     x = x + 1;
   }

   if(x == y){
     plotcircle(x, y, r);
   }
}


plotcircle(int x, int y, int r){
   putpixel( x, y, r);
   putpixel( y, x, r);
   putpixel( y, -x, r);
   putpixel( x, -y, r);
   putpixel(-x, -y, r);
   putpixel(-y, -x, r);
   putpixel(-y, x, r);
   putpixel(-x, y, r);
}

A 回答 (4件)

(Windows環境を前提にした話ですが違ったらごめんなさい。



mainがあることからコマンドラインベースのグラフィックを使用されようとされていますが、現在graphics.h(graphics.lib)が標準で入っていないため
上記のputpixel関数も定義されていないため利用できません。

下記にたのLibをもちいたサイトがありますのでそちらを参考にしてみてください
「コンピュータ・グラフィックス(CG)の初歩」
http://flow.dse.ibaraki.ac.jp/~lecture/CProg/gra …

また、現在Windows上でグラフィックスを行えるようにライブラリが用意されていますのでWinAPIのプログラムへの変更もいいと思います。

この回答への補足

すみません。環境について触れていませんでした。
Unix上で動かしていました。
また、graphics.hを利用することなしに描画する方法で行くことにしました。
お手数をおかけしました。

補足日時:2005/03/06 13:20
    • good
    • 0

No.1253337でも回答しましたが、環境を特定しない限り、適切な回答はできません。



提示されているソースは、こちらで見る限りコンパイルできましたが、putpixelがないのでリンク時にエラーになりました。
結局、グラフィック周りを解決しない限り、動かすことはできません。

この回答への補足

そうですね。環境特定は必要ですね。
現在Unix上で動かしています。
graphic.hを利用して書かせようと思ったのですが、そこまでする必要が無いことに気づき、ファイルに.とXを用いて円描画をするようにしてみました。が、やはりうまく行きません。以下のソースで間違いを指摘していただきたいです。

#include <stdlib.h>
#include <stdio.h>

int main(){
  int x, y, r, d;

  printf("Enter coordinates of radius of circle. R = ");
  scanf("%d\n", &r);

  x = 0;
  y = r;
  d = 3 - 2 * r;

  while(y > x){
    plotcircle(x, y, r);
    if(d < 0){ /* select up */
      d = d + 4 * x + 6;
    }else{ /* select bottom */
      d = d + 4 * (x - y) + 10;
      y = y - 1;
    }
    x = x + 1;
  }

  if(x == y){
    plotcircle(x, y, r);
  }
}

int plotcircle(int x, int y, int r){
  int xx, yy;
  int i, j, k;
  char output;
  FILE *fp;

  xx = -1 * (r + 2);
  yy = r + 1;

  if((fp = fopen("output.txt", "w")) == NULL){
    return(-1);
  }

  for(i = 0; i <= 2 * r; i++){
    for(j = 0; j <= 2 * r; j++){
      yy--;
      xx++;

      if(xx == x && yy == y){
        output = 'X';
      }else if(xx == y && yy == x){
        output = 'X';
      }else if(xx == y && yy == -x){
        output = 'X';
      }else if(xx == x && yy == -y){
        output = 'X';
      }else if(xx == -x && yy == -y){
        output = 'X';
      }else if(xx == -y && yy == -x){
        output = 'X';
      }else if(xx == -y && yy == x){
        output = 'X';
      }else if(xx == -x && yy == y){
        output = 'X';
      }else{
        output = '.';
      }
        fputc(output, fp);
        putchar(output);
    }
    output = '\n';
    fputc(output, fp);
    putchar(output);
  } 
  fclose(fp);
}

補足日時:2005/03/06 13:03
    • good
    • 0

> graphic.hを利用して書かせようと思ったのですが、そこまでする必要が無いことに気づき、ファイルに.とXを用いて円描画をするようにしてみました。

が、やはりうまく行きません。以下のソースで間違いを指摘していただきたいです。

まともにデバッグする気はありませんが、一つアドバイスさせていただくと、最初のソースにあったputpixelを、二次元配列にプロットする関数として自作し、最後にその配列の内容を出力する方が簡単です。

それから、scanfに渡している文字列の'\n'は不要です。
    • good
    • 0

計算は間違ってないようですので、描画をなんとかすれば動くと思います。



参考まで。(画面を50*50サイズで、円の中心を25,25に固定しています)

#include <stdlib.h>
#include <stdio.h>

char disp[50][50]; // 画面領域の定義(50*50固定)
void plotini(void); // 画面を消す
void plotcircle(int, int, int, int, int); // 画面に点を打つ
void plotdisp(void); // 画面を表示する

int main(){
 int x, y, r, d;

 printf("Enter coordinates of radius of circle. R = ");
 scanf("%d", &r);

 plotini();

 x = 0;
 y = r;
 d = 3 - 2 * r;

 while(y > x){
  plotcircle(x, y, 25, 25, 'X');
  if(d < 0){ /* select up */
   d = d + 4 * x + 6;
  }else{ /* select bottom */
   d = d + 4 * (x - y) + 10;
   y = y - 1;
  }
  x = x + 1;
 }

 if(x == y){
  plotcircle(x, y, 25, 25, 'X');
 }

 plotdisp();

 return 0;
}

void plotini(void) {
 int i,j;
 for (i=0; i<50; i++) for (j=0; j<50; j++) disp[i][j]=' ';
}

void plotcircle(int x, int y, int xc, int yc, int c) {
 int xx,yy;
 xx = xc+x; yy = yc+y; if (xx>=0 && x<50 && yy>=0 && yy<50) disp[yy][xx]=c;
 xx = xc+y; yy = yc+x; if (xx>=0 && x<50 && yy>=0 && yy<50) disp[yy][xx]=c;
 xx = xc+y; yy = yc-x; if (xx>=0 && x<50 && yy>=0 && yy<50) disp[yy][xx]=c;
 xx = xc+x; yy = yc-y; if (xx>=0 && x<50 && yy>=0 && yy<50) disp[yy][xx]=c;
 xx = xc-x; yy = yc-y; if (xx>=0 && x<50 && yy>=0 && yy<50) disp[yy][xx]=c;
 xx = xc-y; yy = yc-x; if (xx>=0 && x<50 && yy>=0 && yy<50) disp[yy][xx]=c;
 xx = xc-y; yy = yc+x; if (xx>=0 && x<50 && yy>=0 && yy<50) disp[yy][xx]=c;
 xx = xc-x; yy = yc+y; if (xx>=0 && x<50 && yy>=0 && yy<50) disp[yy][xx]=c;
}

void plotdisp(void) {
 int i,j;
 for (i=0; i<50; i++) {
  for (j=0; j<50; j++) {
   printf("%c",disp[i][j]);
  }
  printf("\n");
 }
}
    • good
    • 0
この回答へのお礼

実は、JaritenCatさんの投稿の前に似たようなプログラムができていました。
報告が遅くなってしまってすみません。
ありがとうございました。

お礼日時:2005/03/09 22:48

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!