本文介绍了WPF:旋转二维矢量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想旋转给定的 2D (!) Vector,是否有 WPF 内置函数?目前我正在手动进行:

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!

推荐答案

您应该看看 System.Windows.Media.Matrix.Rotate(...).使用此方法,您可以创建一个旋转矩阵,然后您可以使用静态 Vector.Mulitply(...) 方法或 Matrix.Transform(...) 方法.

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.

到目前为止我从未使用过 Matrix 类,但我的第一个想法是使用这样的东西:

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);

请注意,Matrix 类使用 3x3 矩阵,但这并不意味着它适用于 3D.它更适用于 2D(正如您可以在文档中阅读的那样).附加参数用于在one 转换中将一个转换与另一个转换组合.有关详细信息,请参阅齐次坐标.

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.

这篇关于WPF:旋转二维矢量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 04:23