座標を回転して新しい座標を取得する|Android開発

任意の座標を回転させた先の座標を取得する方法です。
Matrixを使うことで簡単に取得できます。

手順

  • postRotate()で角度と回転の中心座標を指定します。
  • mapPoints()で回転する座標を指定します。
    mapPoints()をコールするとパラメータに指定した座標が回転先の座標に変換されます。

サンプル

Matrix matrix = new Matrix();
// center of 1280x640
matrix.postRotate(90, 640, 360);

float[] points = new float[10];
for (int i = 0; i < points.length; i+=2) {
    points[i] = i;
    points[i + 1] = i * 10;
}
matrix.mapPoints(points);
for (int i = 0; i < points.length; i+=2) {
    android.util.Log.i("CNV90",
        String.format(Locale.US, "(%d, %d) > (%f, %f)",
            i, i * 10, points[i], points[i + 1]));
}

実行結果

: (0, 0) > (1000.000000, -280.000000)
: (2, 20) > (980.000000, -278.000000)
: (4, 40) > (960.000000, -276.000000)
: (6, 60) > (940.000000, -274.000000)
: (8, 80) > (920.000000, -272.000000)
このエントリーをはてなブックマークに追加
にほんブログ村 IT技術ブログへ

スポンサードリンク

関連コンテンツ

コメント

メールアドレスが公開されることはありません。 が付いている欄は必須項目です