• 締切済み

Cプログラムが円ではなく楕円になってしまう

円を描くCプログラムを書きたいのですが、 楕円形になってしまいます。どこが間違っていますか。 御教示ください。 main () { int x,y1,y2; double pow(), d ; openwindow(100,100,"02kc951:1-3",3); setfgcolorbyRGB(255,255,255); for(x=0;x<100;x++) { d = pow(-100.0,2.0)-(4*pow(50.0,2.0)-pow(40.0,2.0)+pow(x-50.0,2.0)); /* 判別式 */ if( d>=0 ) { y1=(100-sqrt(d))/2.0; /* y1を求める*/ y2=(100+sqrt(d))/2.0; /* y2を求める*/ setpixel(x,y1); setpixel(x,y2); } } closewindow(); }

みんなの回答

  • hofuhofu
  • ベストアンサー率70% (336/476)
回答No.3

判別式のところをもう一度見なおしてみてください。 http://www.mlab.im.dendai.ac.jp/~saitoh/GRA/hint1.htm

hihidede
質問者

お礼

判別式を直したら、円が描けました。どうもありがとうございました。 d = pow(-100.0,2.0)-4*(pow(50.0,2.0)-pow(40.0,2.0)+pow(x-50.0,2.0));

全文を見る
すると、全ての回答が全文表示されます。
noname#74310
noname#74310
回答No.2

c#またはc-Builder6を使って下さい。

参考URL:
http://www.h5.dion.ne.jp/~tuyano/CsharpTutor/CsharpTutor5.html
hihidede
質問者

お礼

どうもありがとうございました。

全文を見る
すると、全ての回答が全文表示されます。
  • uyama33
  • ベストアンサー率30% (137/450)
回答No.1

昔、ピクセルの形が縦に長いとのことで アスペクト比を考えて 少し詰める。 ようなことがあったのを思い出します。 自信はありません。

hihidede
質問者

お礼

どうもありがとうございました。

全文を見る
すると、全ての回答が全文表示されます。

関連するQ&A

  • c言語

    c言語で写真の課題を出されたのですが自分のプログラムでは上手くいきません。どこが間違っているのか教えて欲しいです。 自分のプログラム #include<stdio.h> #include<math.h> int main(){ int i,j; double c,d,x,y,z; for(i=0;i<=360;i++){ c=10*cos(i*M_PI/180); d=10*sin(i*M_PI/180); if(c>=0 && d>=0){ for(j=0;j<=1000;j++){ x=0.001*j; y =x*d/c; z=1-x*x-(sqrt(x)+y)*(sqrt(x)+y); if(z<=0.0){break;} } } if(c<=0 && d>=0){ for(j=0;j<=1000;j++){ x=-0.001*j; y=x*d/c; z=1-x*x-(sqrt(-x)+y)*(sqrt(-x)+y); if(z<=0.0){break;} } } if(c<=0 && d<=0){ for(j=0;j<=1000;j++){ x=-0.001*j; y=x*d/c; z=1-x*x-(sqrt(-x)+y)*(sqrt(-x)+y); if(z<=0.0){break;} } } if(c>=0 && d<=0){ for(j=0;j<=1000;j++){ x=0.001*j; y=x*d/c; z=1-x*x-(sqrt(x)+y)*(sqrt(x)+y); if(z<=0.0){break;} } } printf("x=%lf y=%lf z=%lf\n",x,y,z); } return(0); }

  • C++の質問です

    C++の質問です。 c++をコンパイルしたとき、 出力されるのが次のように3つ同時にされるようにしたいのですが、 1、 名前 ○○○○ 番号 ○○○○ x= {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}の平均 2、 x= {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}の分散 y= {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}の標準偏差 3、 x={1,2,3,4,5,6,7,8,9,10}と y={10,9,8,7,6,5,4,3,2,1}の相関係数 下記のソースをどのように変えればいいでしょうか。 ちなみに、C言語ではなくC++なので C++形式でお願いします。 どうかお願いします。 #include <iostream> #include <cmath> using namespace std; double Mean(int *a, int size); double StandardDeviation(int *a, int size); double CoefficientOfCorrelation(int *a, int *b, int sizeA, int sizeB); int main() { int x[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int y[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; cout << " 標準偏差 : " << StandardDeviation(x, sizeof(x) / sizeof(int)) << endl << " 相関係数 : " << CoefficientOfCorrelation(x, y, sizeof(x) / sizeof(int), sizeof(y) / sizeof(int)) << endl; return 0; } double Mean(int *a, int size) { if (size <= 0) { return -1.0; } double d = 0.0; for (int i = 0; i < size; i++) { d += *(a + i); } return d / size; } double StandardDeviation(int *a, int size) { if (size <= 0) { return -1.0; } double mean = Mean(a, size); double d = 0.0; for (int i = 0; i < size; i++) { d += pow(*(a + i) - mean, 2); } return sqrt(d / size); } double CoefficientOfCorrelation(int *a, int *b, int sizeA, int sizeB) { if (sizeA > 0 && sizeB > 0 && sizeA != sizeB) { return -1.0; } double meanX = Mean(a, sizeA); double meanY = Mean(b, sizeB); double sdX = StandardDeviation(a, sizeA); double sdY = StandardDeviation(b, sizeB); double coeff = 0.0; for (int i = 0; i < sizeA; i++) { coeff += (*(a + i) - meanX) * (*(b + i) - meanY); } return (coeff / (sizeA * sdX * sdY)); }

  • C言語でテーブル引きしたら速度が遅くなった

    プログラムに悩んでいるものです. とある画像処理のプログラムを組んでいるのですが,処理が遅くテーブル引きを組んでいます. この前もこの場を借りて質問しsqrt()のテーブル引きは実現したのですが,処理速度が遅くなってしまい原因が分からないので質問させていただきました. 前の質問URL:http://okwave.jp/qa/q7103550.html 前回から修正した現在のプログラムの一部を示します. ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー void filter(unsigned char* d, short *dx, short *dy, int w, int h) { ///// テーブル生成 ///// static int c_size = 0; // static 値を保持 static double c_sqrt[1020][1020]; if(c_size != 1020){ // 初回呼び出しのみ実行 c_size = 1020; for(int i=0; i<c_size; i++){ // 有りえるすべての値を生成 for(int j=0; j<c_size; j++){ c_sqrt[i][j] = sqrt( (double)(i*i + j*j) ); } } } ///// d = sqrt(dx^2 + dy^2) ///// for(int y = 1; y < h-1; ++y){ for(int x = 1; x < w-1; ++x){ double u = (double)dx[y*w+x]; double v = (double)dy[y*w+x]; if(u<0) u=-u; if(v<0) v=-v; int val = (int)c_sqrt[(int)u][(int)v] /4; if(val>255) val=255; d[y*w+x] = val; } } } ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー テーブル引きをしない場合(プログラム省略)はこの関数の処理時間が約9[ms]だったのに対し,上記のプログラムは約15[ms]となってしまいました. どういう風に修正すれば,テーブル引きの効果が出せるでしょうか? 長い文章を最後までお読みいただきありがとうございます. ご回答,よろしくお願い致します.

  • SetPixel関数のmath.h

    WindowsXPのノートでVisualC++2008ExpressEditionを使っています。 SetPixel関数で円を描くプログラムで、math.hの問題だと思われる4つのエラーでコンパイルできません。 宣言は #include<windows.h> #include<math.h> ウィンドウプロシージャ内で switch (msg) { case WM_PAINT: hdc = BeginPaint(hWnd, &ps); for (i = a - r;i <= a + r; i++) { x = i; y = (int)sqrt(pow(r, 2) - pow(x - a, 2)) + b;←96行目 SetPixel(hdc, x, y, RGB(0, 0, 0)); y = (int)-sqrt(pow(r, 2) - pow(x - a, 2)) + b;←98行目 1>c:\documents and settings\REI\my documents\visual studio 2008\projects\setpixel01\setpixel01\setpixel01.cpp(96) : error C2668: 'pow' : オーバーロード関数の呼び出しを解決することができません。(新機能 ; ヘルプを参照) 1> c:\program files\microsoft visual studio 9.0\vc\include\math.h(575): 'long double pow(long double,int)' の可能性があります。 1> c:\program files\microsoft visual studio 9.0\vc\include\math.h(527): または 'float pow(float,int)' 1> c:\program files\microsoft visual studio 9.0\vc\include\math.h(489): または 'double pow(double,int)' 1> 引数リスト '(int, int)' を一致させようとしているとき 同じ内容のエラーが96行目なのが2回、98行目なのが2回出ます。math.hを編集すればいいんでしょうか?gccならコマンドラインから-lmでいいかもしれないんですが。自分の入力ミスはないと思います。 どなたかお分かりになる方、教えていただけないでしょうかm(_ _)m

  • 関数のプログラムについて

    任意の二次方程式ax^2+bx+c=0をとくプログラムの作成です 引数をa,b,cとして、解の大きい方を返すというものなのですが、 僕は以下のようにして組んだのですが、うまくいきません。 と、いうより、関数の作り方がいまいちわからないです。 どこが駄目なのか教えてください。 作ってみたやつ↓ #include<math.h> #include<stdio.h> int a,b,c; double d; double x,y,z; int main(void) { a=1; b=2; c=1; printf("ax^2+bx+c=0\n "); d=b^2-4*a*c; if (d<0){printf("kyosuukai\n)} else if(d>=0) { x=(b+sqrt(b^2-4*a*c))/2*a; y=(b-sqrt(b^2-4*a*c))/2*a; if(x>=y){z=x} else if(x<y){z=y} printf("x= %f\n",z); } }

  • C言語でsqrt(a^2+b^2)のテーブル引き

    プログラムに悩んでいるものです. とある画像処理のプログラムを組んでいるのですが,処理が遅くテーブル引きを組んでいます. 三角関数などはすんなりできたのですが,質題にもある通りsqrt(a^2+b^2)が実現できず,この場を借りて質問させていただきました. 以下にプログラムの一部を示します. ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー void filter(unsigned char* d, short *dx, short *dy, int w, int h) { ///// テーブル生成 ///// static int c_size = 0; // static 次の呼び出しでも値保持 static double *c_sqrt = NULL; // c_size = 255;              // u,v:0~255 c_sqrt = (double *)malloc(sizeof(double)*c_size*c_size); // 領域確保 for(int i=0; i<c_size; ++i){     // 有りえるすべての値を生成 for(int j=0; j<c_size; ++i){ c_sqrt[i*j] = sqrt( (double)(i*i + j*j) ); } } ///// d = sqrt(dx^2 + dy^2) ///// for(int y = 1; y < h-1; ++y){ for(int x = 1; x < w-1; ++x){ double u = (double)dx[y*w+x]; double v = (double)dy[y*w+x]; int val = (int)c_sqrt[ (int)(u*v) ] /4; if (val>255) val=255; d[y*w+x] = val; } } } ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー 見てご察し頂ける(?)と思いますが,この関数は何回も呼び出すので,上のほうででテーブル引きしようとしてます. ただ明らかなプログラム経験不足のためかうまくいってません. 個人的にはc_sqrtを別途関数c_sqrt(u,v)にしたほうがよいのかと思ってます. どういうプログラム記述をすれば,このテーブル引きが実現できるでしょうか? ご回答,お力添え,よろしくお願い致します.

  • 実行すると if文 に矛盾

    #include <stdio.h> #include <math.h> int main(void) { /* */ double c, l, m, n, w=1, x[4]={1,2,3,4}, y[4]={1,2,3,4}; /* */ int j, k, u, v; /* */ for(j=0; j<3; j++){ /*   */ for(k=j+1; k<4; k++){ /*     */ l = sqrt( pow(x[j]-x[k], 2) + pow(y[j]-y[k], 2) ); /*     */ m = sqrt( pow(x[j], 2) + pow(y[j], 2) ); /*     */ n = sqrt( pow(x[k], 2) + pow(y[k], 2) ); /*     */ c = ( pow(m, 2) + pow(n, 2) - pow(l, 2) ) / (2*m*n); /*☆    */ /* if(c=w) ; */ /*※    */ if(c<=w){ /*       */ w = c; /*       */ u = j; /*       */ v = k; /*●    */ } else if(c>w){ /*       */ if(c=w) /*         */ printf("no way\n"); /*     */ } /*   */ } /* */ } /* */ printf("[ P%d P%d ]\n", u, v); /* */ return(0); } -------------------------------------------------------------------------------- 上記プログラムなのですが、Visual C++ でこれを実行すると、 ------------------------------ no way no way no way no way [ P0 P2 ] Press any key to continue ------------------------------ と出力されてしまいました。 ☆行のif文のコメントを外して直前で c=w を評価すると、※の部分が正常に処理されました。 なぜこのような矛盾が生じているのでしょうか? 何かご意見いただければ幸いです。 ※今回 Visual C++ でしか実行できるものがなかったため、他では確認できておりません。恐縮です。 ※この投稿の文字量の制限のため、プログラムの解説は割愛しました; ※補足などでお伝えできればよいのですが…。

  • C+のsqrt

    icpcの過去問(http://www.deqnotes.net/acmicpc/3006/ja)で、答えを #include <cmath> #include <iostream> using namespace std; #define max 1000000 int main(){ //int max = 100; int a,d,n,number[1000010]; for(int i=0;i<max;i++){ number[i] = 1; } number[0]=0;number[1]=0; for(int i=2;i<=sqrt(max)+1;i++){ if(number[i]){ for(int j=i*2;j<max;j=j+i){ number[j] = 0; } } } while(cin >> a >> d >>n,a||d||n){ int count =0; for(int i=a;i<=max;i=i+d){ if(number[i]){count ++; if(count == n){cout << i << endl;break;} } } } } と書いたところ、PKUで Main.cpp F:\temp\11386803.6056\Main.cpp(15) : error C2668: 'sqrt' : ambiguous call to overloaded function math.h(581): could be 'long double sqrt(long double)' math.h(533): or 'float sqrt(float)' math.h(128): or 'double sqrt(double)' while trying to match the argument list '(int)' F:\temp\11386803.6056\Main.cpp(16) : error C2108: subscript is not of integral type というコンパイルエラーがでます。 sqrtの前後で型が違っているのは分かるのですが、具体的にどうなおせばよいかわかりません。

  • segmentation fault

    ソースは以下です 途中までは実行されるんですけど,,, 教えてください #include <stdio.h> #include <stdlib.h> #include <math.h> #define M_PI 3.14159265358979 /* 円周率 */ int main(int argc, char **argv) { int xsize,ysize,depth,x,y,c; char code[10]; FILE *fp; unsigned char ***image,***outimage; double sum_kido,sum_kido_x,grad; int xmax,xmin,ymax,ymin,count; char grad_im; int sumx, sumy, N; char dummy[1024]; double Deltax; double Deltay; if(argc != 3){ fprintf(stderr,"Usage : a.out input_filename output_filename\n"); exit(1); } if((fp=fopen(argv[1],"r"))==NULL){ fprintf(stderr,"File can not open : %s\n",argv[1]); exit(1); } fscanf(fp,"%s",code); /* fscanf(fp,"%s",dummy); */ fscanf(fp,"%d %d",&xsize,&ysize); fscanf(fp,"%d ",&depth); fprintf(stderr,"%s, %d, %d, %d\n",code,xsize,ysize,depth); getchar(); image = (unsigned char ***)malloc(sizeof(unsigned char**)*ysize); for(y=0;y<ysize;y++){ image[y] = (unsigned char **)malloc(sizeof(unsigned char*)*xsize); for(x=0;x<xsize;x++){ image[y][x] = (unsigned char *)malloc(sizeof(unsigned char)*3); } } outimage = (unsigned char ***)malloc(sizeof(unsigned char**)*ysize); for(y=0;y<ysize;y++){ outimage[y] = (unsigned char **)malloc(sizeof(unsigned char*)*xsize); for(x=0;x<xsize;x++){ outimage[y][x] = (unsigned char *)malloc(sizeof(unsigned char)*3); } } // load image for(y=0;y<ysize;y++){ for(x=0;x<xsize;x++){ for(c=0;c<3;c++){ image[y][x][c] = (unsigned char)(fgetc(fp)); printf("%d\n", image[y][x][c]); } } } fclose(fp); sumx = 0; sumy = 0; N = 0; for(y=0;y<ysize;y++){ for(x=0;x<xsize;x++){ if((image[y][x][0]*2<image[y][x][2])&&(image[y][x][1]*2<image[y][x][2])){ outimage[y][x][0]=255; outimage[y][x][1]=0; outimage[y][x][2]=0; }else{ outimage[y][x][0]=image[y][x][0]; outimage[y][x][1]=image[y][x][1]; outimage[y][x][2]=image[y][x][2]; sumx += x; sumy += y, N += 1; } } } // output image if((fp=fopen(argv[2],"w"))==NULL){ fprintf(stderr,"File can not open : %s\n",argv[2]); exit(1); } fprintf(fp,"%s\n",code); fprintf(fp,"%d %d\n",xsize,ysize); fprintf(fp,"%d\n",depth); count = 0; for(y=0;y<ysize;y++){ for(x=0;x<xsize;x++){ for(c=0;c<3;c++){ fputc(outimage[y][x][c],fp); } } } fclose(fp); fprintf(stdout, "%d, %d, %d, Gravity (x,y) = (%lf, %lf)\n",sumx,sumy,N,(double)(sumx)/(double)(N), (double)(sumy)/(double)(N)); for(y=0;y<ysize;y++){ for(x=0;x<xsize;x++){ Deltax = sqrt(( pow (image[x+1][y][0] , 2.0 ) + pow (image[x+1][y][1] , 2.0 ) + pow (image[x+1][y][2] , 2.0 )) - ( sqrt( pow (image[x-1][y][0] , 2.0) + (image[x-1][y][1] , 2.0) + (image[x-1][y][2] , 2.0)))); Deltay = sqrt(( pow (image[x][y+1][0] , 2.0 ) + pow (image[x][y+1][1] , 2.0 ) + pow (image[x][y+1][2] , 2.0 )) - ( sqrt( pow (image[x][y-1][0] , 2.0) + (image[x][y-1][1] , 2.0) + (image[x][y-1][2] , 2.0)))); } } for(y=0;y<ysize;y++){ for(x=0;x<xsize;x++){ double T = atan( Deltax / Deltay ); //勾配ベクトルの方向 double U = sqrt( pow ( Deltax , 2.0 ) + pow (Deltay , 2.0 ) ); //勾配ベクトルの勾配量  double V = atan( ( x - (double)(sumx) / (double)(N) ) / ( y - (double)(sumy) / (double)(N) ) ); printf(" %f\n " , V - ( T + ( M_PI / 2 ))); } } }

  • C言語のプログラムでうまく動きません。

    xとyを入力してその商を求めよという問題なのですが、大きい数字を小さい数字で割るようになっています。また、当然分母が0の時には不定となるので、次のようなプログラムを作りましたが、うまく動きません。どこが悪いか考えてもわかりませんでした。教えていただけませんでしょうか。 #include<stdio.h> main() { int x; int y; double syou; printf("xを入力せよ"); scanf("%d",&x); printf("yを入力せよ"); scanf("%d",&y); syou=x/y; if("syou>=1") { if("y==0") { printf("不定です"); } printf("答えは%fです。",&syou); } else { if("x==0") { printf("不定です"); } syou=y/x; printf("答えは%fです。",&syou); } } これでx、yを入力しても不定と出てきて、xの方が大きいときには見当はずれの大きな数字が出てきます。逆にyの方が大きいと同じく不定と出てきて答えは0.000と表示します。