我有以下代码行: 我已经在不知道值(多少度)的情况下对矩形应用了很少的旋转。现在我想在2D中获得旋转或元素角度。
Rectangle element = (Rectangle)sender;
MatrixTransform xform = element.RenderTransform as MatrixTransform;
Matrix matrix = xform.Matrix;
third.Content = (Math.Atan(matrix.M21 / matrix.M22)*(180/Math.PI)).ToString();
and the matrix is like following
|M11 M12 0|
|M21 M22 0|
|dx dy 1| which is Transformation Matrix I guess !!
这似乎不是正确的价值。 我希望在 0到360度
中获得角度答案 0 :(得分:9)
您可以使用:
var x = new Vector(1, 0);
Vector rotated = Vector.Multiply(x, matrix);
double angleBetween = Vector.AngleBetween(x, rotated);
这个想法是:
你可以玩这个:
[TestCase(0,0)]
[TestCase(90,90)]
[TestCase(180,180)]
[TestCase(270,-90)]
[TestCase(-90, -90)]
public void GetAngleTest(int angle, int expected)
{
var matrix = new RotateTransform(angle).Value;
var x = new Vector(1, 0);
Vector rotated = Vector.Multiply(x, matrix);
double angleBetween = Vector.AngleBetween(x, rotated);
Assert.AreEqual(expected,(int)angleBetween);
}
答案 1 :(得分:8)
FOR FUTURE REFERENCE:
这将为您提供以弧度为单位的变换矩阵的旋转角度:
var radians = Math.Atan2(matrix.M21, matrix.M11);
如果需要,您可以将弧度转换为度数:
var degrees = radians * 180 / Math.PI;
答案 2 :(得分:1)
您的答案将以弧度为单位http://social.msdn.microsoft.com/forums/en-US/netfxbcl/thread/c14fd846-19b9-4e8a-ba6c-0b885b424439/。
因此,只需使用以下内容将值转换回度:
double deg = angle * (180.0 / Math.PI);