views:

341

answers:

1

Hello,

I want to rotate a given 2D (!) Vector, is there a WPF built-in function for this? Currently I'm doing it manually:

        Vector v = new Vector();
        v.X = 10; v.Y = 10;

        Vector v2 = new Vector();

        v2.X = v.X * Math.Cos(-90 * 180 / Math.PI) - v.Y * Math.Sin(-90 * 180 / Math.PI);
        v2.Y = v.Y * Math.Cos(-90 * 180 / Math.PI) + v.X * Math.Sin(-90 * 180 / Math.PI);

I think this should be also possible by multiplying the given vector with a rotation matrix? Anyways, I don't get it, can someone please give me an example? Thanks!

+1  A: 

You should have a look at System.Windows.Media.Matrix.Rotate(...). Using this method, you can create a rotation matrix which you can then apply to your vector using the static Vector.Mulitply(...) method or the Matrix.Transform(...) method.

I have never used the Matrix class so far, but my first idea was to use something like this:

Matrix m = Matrix.Identity;
m.Rotate(90);
Vector v2 = m.Transform(v);

Note that the Matrix class uses 3x3 matrices, but that does not mean it is intended for 3D. It is rather intended for 2D (as you can read in the documentation). The additional parameters are used for combining a translation with another transformation in one transformation. See Homogenous coordinates for details.

gehho
Thanks, I ended up using Matrix.Rotate(angle). Note that this Method returns void.
stefan.at.wpf
You're right. Edited the code, so that `Rotate(...)` is used correctly.
gehho