在c#中实现2x2旋转矩阵

时间:2013-02-20 09:15:24

标签: c# vector matrix rotational-matrices

我使用以下代码旋转矢量:

   var newVectorX = Math.Cos(step) * normalizedVector.X 
                                 - Math.Sin(step) * normalizedVector.Y;

                var newVectorY = - Math.Sin(step) * (normalizedVector.X )
                                 + Math.Cos(step) * normalizedVector.Y;

我试图创建一个2x2矩阵,所以我可以将我的规范化向量与矩阵相乘。结果将是新的旋转矢量而不是坐标。

rotation matrix

不幸的是System.Windows.Media.Matrix不支持2x2矩阵。到目前为止,我找不到这个旋转矩阵的任何实现。你会如何实现这个?

1 个答案:

答案 0 :(得分:1)

实际上,System.Windows.Media.Matrix正是您所需要的。虽然看起来你想要一个2x2矩阵,但使用3x3矩阵也可以进行翻译。只需使用System.Windows.Media.Matrix并忽略您不需要的部分。

Matrix rotate = Matrix.Identity;
rotate.Rotate(step * 180 / Math.PI);    // Rotate() takes degrees
Vector newVector = rotate.Transform(normalizedVector);