画像の回転

画像の回転にチャレンジしてみました。

座標の求め方がかなり難しかったので色々なサイトで数学やアルゴリズムの勉強をしました。

7割くらいは理解できましたが、やっぱ難しいですね。ちょっと数学そのものの勉強を再開しないといけません。

座標の求め方はこんな感じになりました。

// -- dxclass.cpp --
if ( rotation ) {
    float center_x = rc.left + rc.right / 2.0f;
    float center_y = rc.top + rc.bottom / 2.0f;
    long rw = rc.right/2;
    long rh = rc.bottom/2;

    v[0].x = center_x + cosf(rotation)*rw - sinf(rotation)*(-rh);
    v[0].y = center_y + sinf(rotation)*rw + cosf(rotation)*(-rh);

    v[1].x = center_x + cosf(rotation)*rw - sinf(rotation)*rh;
    v[1].y = center_y + sinf(rotation)*rw + cosf(rotation)*rh;

    v[2].x = center_x + cosf(rotation)*(-rw) - sinf(rotation)*(-rh);
    v[2].y = center_y + sinf(rotation)*(-rw) + cosf(rotation)*(-rh);

    v[3].x = center_x + cosf(rotation)*(-rw) - sinf(rotation)*rh;
    v[3].y = center_y + sinf(rotation)*(-rw) + cosf(rotation)*rh;
}

ややこしすぎですね。

ABCをランダム表示するプログラムに画像の回転処理を入れてみました。

実行結果



前は画面の中央に集まってくる処理でしたが、今回は逆に画面の外へ移動する処理に変更してみました。だからどうしたって話ですが。