1

長方形内の点を回転させるCプログラムを書きたい。

私のプログラムでは、長方形の中心がピボットポイントであり、長方形の寸法は320x480です。長方形の頂点の1つが原点にあるとすると、ピボットポイントは(160,240)です。

(px, py)ここで、ピボットを基準にして長方形内の点を回転させるために(ox, oy)、次の式を使用しています-

p'x = cos(theta) * (px-ox) - sin(theta) * (py-oy) + ox

p'y = sin(theta) * (px-ox) + cos(theta) * (py-oy) + oy

ただし、ポイントを90度回転させようとすると、すべてのポイントが直線にマッピングされます。

誰かがこの問題を解決できますか?

theta2=90;


        theta1=abs(theta2*3.1415926)/180;

        if(theta2>0)
        {
            for(int tc=0;tc<rstruct2->nrows;tc++)
            {
                rstruct2->xcol[tc]=round((rstruct2->xcol[tc]-160)*cos(theta1)-sin(theta1)*(rstruct2->ycol[tc]-240)+160);

                rstruct2->ycol[tc]=round((rstruct2->xcol[tc]-160)*sin(theta1)+cos(theta1)*(rstruct2->ycol[tc]-240)+240);


            }
        }
        else
        {
            for(int tc=0;tc<rstruct2->nrows;tc++)
            {
                rstruct2->xcol[tc]=round(160+(rstruct2->xcol[tc]-160)*cos(theta1)+(rstruct2->ycol[tc]-240)*sin(theta1));

                rstruct2->ycol[tc]=round(240+(-rstruct2->xcol[tc]-160)*sin(theta1)+(rstruct2->ycol[tc]-240)*cos(theta1));


            }
        }
4

1 に答える 1

4

y回転は変更されたx値を使用しますが、基本値を使用する必要があります-次のような一時変数を使用します。

double x_tmp = round((rstruct2->xcol[tc]-160)*cos(theta1)-sin(theta1)*(rstruct2->ycol[tc]-240)+160);
double y_tmp = round((rstruct2->xcol[tc]-160)*sin(theta1)+cos(theta1)*(rstruct2->ycol[tc]-240)+240);

rstruct2->xcol[tc] = x_tmp;
rstruct2->ycol[tc] = y_tmp;
于 2013-02-27T11:12:12.480 に答える