Last active
August 29, 2015 14:15
-
-
Save niusounds/dc28a4beadf94c7b300c to your computer and use it in GitHub Desktop.
AndroidのMatrixクラスから、平行移動、拡縮、回転それぞれの成分を取り出す処理。射影変換の行列はサポート対象外です。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import android.graphics.Matrix; | |
| public class MatrixUtils { | |
| public static Info decomposition(Matrix matrix, boolean reverseOrder) { | |
| double dx, dy, sx, sy, theta; | |
| float[] values = new float[9]; | |
| matrix.getValues(values); | |
| if (values[Matrix.MPERSP_0] != 0 && values[Matrix.MPERSP_1] != 0 && values[Matrix.MPERSP_2] != 1) { | |
| throw new IllegalArgumentException("this matrix is not supported"); | |
| } | |
| if (reverseOrder) { | |
| theta = Math.atan(values[3] / values[0]); | |
| double cos = Math.cos(theta); | |
| double sin = Math.sin(theta); | |
| dx = values[2] * cos + values[5] * sin; | |
| if (cos != 0.0) { | |
| sx = values[0] / cos; | |
| sy = values[4] / cos; | |
| dy = (values[5] - dx * sin) / cos; | |
| } else { | |
| sx = values[3] / sin; | |
| sy = -values[1] / sin; | |
| dy = (values[2] - dx * cos) / -sin; | |
| } | |
| } else { | |
| dx = values[2]; | |
| dy = values[5]; | |
| theta = Math.atan(-values[1] / values[0]); | |
| double cos = Math.cos(theta); | |
| double sin = Math.sin(theta); | |
| if (cos != 0.0) { | |
| sx = values[0] / cos; | |
| sy = values[4] / cos; | |
| } else { | |
| sx = -values[1] / sin; | |
| sy = values[3] / sin; | |
| } | |
| } | |
| return new Info(dx, dy, sx, sy, theta); | |
| } | |
| public static class Info { | |
| public final double dx; | |
| public final double dy; | |
| public final double sx; | |
| public final double sy; | |
| public final double theta; | |
| public Info(double dx, double dy, double sx, double sy, double theta) { | |
| this.dx = dx; | |
| this.dy = dy; | |
| this.sx = sx; | |
| this.sy = sy; | |
| this.theta = theta; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment