Lama
Lama

Reputation: 2974

Implement 2x2 rotation matrix in c#

I rotate a vector using the following code:

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

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

I tried to create a 2x2 matrix so I just can multiply my normalized vector with the matrix. The result would be the new rotated vector instead the coordinates.

rotation matrix

Unfortunately System.Windows.Media.Matrix doesn't support 2x2 matrices. I couldn't find any implementation of this rotation matrix so far. How would you implement this?

Upvotes: 1

Views: 2365

Answers (1)

Gabe
Gabe

Reputation: 86818

Actually, System.Windows.Media.Matrix is exactly what you need. While it may seem that you want a 2x2 matrix, using a 3x3 matrix allows for translations too. Just use a System.Windows.Media.Matrix and ignore the part you don't need.

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

Upvotes: 1

Related Questions