获得从矩阵角度 [英] Get angle from matrix

查看:169
本文介绍了获得从矩阵角度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道一个矩阵[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.

这篇关于获得从矩阵角度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆