sin、cosを再計算させない

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
みけCAT

sin、cosを再計算させない

#1

投稿記事 by みけCAT » 14年前

Dev-C++4.9.9.2、コンパイラはデフォルトです。
DXライブラリを使わずに龍神録を作ることに挑戦中です。
現在13章ですが、弾が発射されると処理落ちします。
DrawRotaGraphF関数の中で使われているkaiten関数
(座標を指定した点を中心に指定した角度反時計回りに回転させる関数です)
で、指定された角度が前回と同じならばsinとcosを再計算させないようにしたいのですが、
現在のコードだと再計算しているようです。
解決法がわかる方がいらっしゃいましたらよろしくお願いします。
int DrawRotaGraphF(int x,int y,float ExtRate,float Angle,
        gazou* graph,int TransFlag,int TurnFlag) {
    int sx,sy;
    int dx,dy;
    int mx,my;
    int siy;
    double rx,ry;
    color col;
    sx=(int)(graph->sizex*ExtRate);
    sy=(int)(graph->sizey*ExtRate);
    for(dy=0;dy<sy;dy++) {
        for(dx=0;dx<sx;dx++) {
            mx=(int)(dx/ExtRate);my=(int)(dy/ExtRate);
            if(TurnFlag)MyGetPixel(graph->sizex-mx-1,my,graph,&col);
            else MyGetPixel(mx,my,graph,&col);
            kaiten(&rx,&ry,x,y,x+dx-sx/2,y+dy-sy/2,Angle);
            if(!TransFlag || col.m!=RGB(255,255,255)) {
                MySetPixel((int)rx,(int)ry,&col);
            }
        }
    }
    return 0;
}
void kaiten(double* ox,double* oy,
        double cx,double cy,double mx,double my,double angle) {
    double length;/*斜辺の長さ*/
    double sinmoto,cosmoto;/*元のsin,cos*/
    double sinsaki,cossaki;/*回転後のsin,cos*/
    double sinc=0,cosc=1;/*sin,cosのキャッシュ*/
    double maeangle=0;/*キャッシュの角度*/
    length=sqrt((mx-cx)*(mx-cx)+(cy-my)*(cy-my));
    if(length==0) {
        *ox=mx;
        *oy=my;
        return;
    }
    if(angle!=maeangle) {
        sinc=sin(angle);
        cosc=cos(angle);
        maeangle=angle;
    }
    sinmoto=(cy-my)/length;
    cosmoto=(mx-cx)/length;
    /*加法定理*/
    sinsaki=sinmoto*cosc+cosmoto*sinc;
    cossaki=cosmoto*cosc-sinmoto*sinc;
    *ox=cx+length*cossaki;
    *oy=cy-length*sinsaki;
}

/*ニュートン法*/
double sqrt(double in) {
    double x=2.0;
    double nextx=1.0;
    while((x>=nextx?1:-1)*(x-nextx)>0.0000000001) {
        x=nextx;
        nextx=x-(x*x-in)/(2*x);
    }
    return nextx;
}

/*
高校生のための マクローリン展開(1)
http://bit.ly/9Z2PNX
より
*/
double cos(double angle) {
    double result=1.0;
    double temp;
    int i,j;
    double fugou=-1;
    for(i=2;i<=30;i+=2) {
        temp=fugou;
        for(j=0;j<i;j++)temp*=angle;
        for(j=1;j<=i;j++)temp/=j;
        result+=temp;
        fugou=-fugou;
    }
    return result;
}

/*
高校生のための マクローリン展開(2)
http://bit.ly/bfVyXy
より 
*/
double sin(double angle) {
    double result;
    double temp;
    int i,j;
    double fugou=-1;
    result=angle;
    for(i=3;i<=31;i+=2) {
        temp=fugou;
        for(j=0;j<i;j++)temp*=angle;
        for(j=1;j<=i;j++)temp/=j;
        result+=temp;
        fugou=-fugou;
    }
    return result;
}
プロジェクト全体は
http://www.play21.jp/board/formz.cgi?ac ... &rln=64454
のNo:64454のものです。

みけCAT

Re:sin、cosを再計算させない

#2

投稿記事 by みけCAT » 14年前

自己解決しました。
staticのつけ忘れが原因でした。
void kaiten(double* ox,double* oy,
        double cx,double cy,double mx,double my,double angle) {
    double length;/*斜辺の長さ*/
    double sinmoto,cosmoto;/*元のsin,cos*/
    double sinsaki,cossaki;/*回転後のsin,cos*/
    static double sinc=0,cosc=1;/*sin,cosのキャッシュ*/
    static double maeangle=0;/*キャッシュの角度*/
    length=sqrt((mx-cx)*(mx-cx)+(cy-my)*(cy-my));
    if(length==0) {
        *ox=mx;
        *oy=my;
        return;
    }
    if(angle!=maeangle) {
        sinc=sin(angle);
        cosc=cos(angle);
        maeangle=angle;
    }
    sinmoto=(cy-my)/length;
    cosmoto=(mx-cx)/length;
    /*加法定理*/
    sinsaki=sinmoto*cosc+cosmoto*sinc;
    cossaki=cosmoto*cosc-sinmoto*sinc;
    *ox=cx+length*cossaki;
    *oy=cy-length*sinsaki;
}

閉鎖

“C言語何でも質問掲示板” へ戻る