現在地 >> メニュー >> サンプルコード::OpenGL >> 座標の回転

問題


2次元平面上でp(x,y)をある角度θだけ、半時計まわりに回転させたとき、
その座標がどうなるか出力せよ。

座標系の変換(座標軸を回転させる場合)

答え



#include <cstdio>
#include <cmath>
#include <iostream>


struct _POINT2D
{
double x;
double y;
};
typedef struct _POINT2D POINT_2D;


using namespace std;

#define PI_OVER_180 0.0174532925

int main(int argc,char*argv[])
{

POINT_2D p;
POINT_2D q;
double theta;

while(1){

cout<<"(x,y,θ)=? ";
cin >> p.x >> p.y >> theta;

cout << endl;

printf("(x,y,θ)=(%f,%f,%f) \n",p.x,p.y,theta );

cout <<"Rotate !"<< endl;

q.x = p.x * cos(theta*PI_OVER_180) - p.y * sin(theta*PI_OVER_180);
q.y = p.x * sin(theta*PI_OVER_180) + p.y * cos(theta*PI_OVER_180);

printf("(s,t)=(%f,%f) \n\n",q.x,q.y);

}


return 0;
}

メモ


この場合、移動後の座標q(s,t)は

s = x*cosθ - y*sinθ
t = x*sinθ + y*cosθ

となる。

座標系の変換(座標軸を回転させる場合)

目次

― その他 ―

Wiki内検索

計測中...(07.10.8〜)

Save The World






▲よろしければ広告のクリックもお願いします


▲ランキングに参加しました

管理人/副管理人のみ編集できます