获得从矩阵角度
我知道一个矩阵[X缩放比例,y倾斜中,x偏移,y缩放比例,反式X,反Y],并希望得到度的角度。
I know a matrix [x scale, y skew, x skew, y scale, trans x, trans y], and would like to get the angle in degrees.
谢谢!
考虑下面的矩阵
| x_sc y_sk 0 |
| x_sk y_sc 0 |
| x_tr y_tr 1 |
与 SK
表示歪斜, SC
显示比例和 TR
表示翻译。
with sk
indicating skew, sc
indicating scale and tr
indicating translation.
这仅重presents一个纯轮换,如果所有三个属实
This only represents a pure rotation if all three are true
y_sk == -x_sk
y_sc == x_sc
x_sc * y_sc - x_sk * y_sk == 1
在这种情况下,如果 THETA
是旋转的角度,然后
In this case, if theta
is the angle of rotation, then
theta == arcos(x_sc)
这会给你弧度的答案(最有可能),所以你需要转换为度。
This will give you the answer in radians (most likely), so you'll need to convert to degrees.
假设你有一个名为M上的对象,再presenting矩阵,满足我上面的定义,属性,你可以这样做:
Assuming you have an object called M, representing the matrix, the properties that match my definitions above, you could do:
function toPureRotation(var M) {
if( (M.y_sk != (-1 * M.x_sk)) ||
(M.y_sc != M.x_sc) ||
((M.x_sc * M.y_sc - M.x_sk * M.y_sk) != 1)
) {
return Number.NaN;
}
else {
return Math.acos(M.x_sc); // For radians
return Math.acos(M.x_sc) * 180 / Math.PI; // For degrees
}
}
修改
对于一个纯粹的旋转再缩放变换(或pceded $ P $)维持长宽比:
For a pure rotation followed by (or preceded by) a scaling transform that maintains aspect ratio:
| sc 0 0 |
| 0 sc 0 |
| 0 0 1 |
然后你可以我们以下标识:
Then you can us the following identity:
x_sc * y_sc - x_sk * y_sk == sc^2
这给了我们
function toRotation(var M) {
if( (M.y_sk != (-1 * M.x_sk)) ||
(M.y_sc != M.x_sc)
)
) {
return Number.NaN;
}
else {
var scale_factor = Math.sqrt((M.x_sc * M.y_sc - M.x_sk * M.y_sk));
return Math.acos(M.x_sc/scale_factor); // For radians
return Math.acos(M.x_sc/scale_factor) * 180 / Math.PI; // For degrees
}
}
如果你想在翻译到的因素,你进入伤的世界。
If you want to factor in translations, you're entering a world of hurt.