本文介绍了Matrix3D在WPF中围绕z正旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让P是一个Point3D,这样P = {1,0,0}.我需要定义绕+ 45deg的Z轴旋转并将其应用于P.根据右手惯例,变换M定义为:

Let P be a Point3D such that P = {1,0,0} . I need to define a rotation around the Z axis of +45deg and apply it to P. Under a right-hand convention, the transformation M is defined:

M =

0.707 -0.707 0 0
0.707 0.707 0 0
0 0 1 0
0 0 0 1

数学上:

M x P =

0.707 -0.707 0 0         1           0.707
0.707 0.707 0 0     X    0       =   0.707
0 0 1 0                  0           0
0 0 0 1                  1           1

这是我的C#代码:

    private void testMatrix3D() {
        double angle_rad = Math.PI/4;
        double cos = Math.Cos(angle_rad);
        double sin = Math.Sin(angle_rad);
        Matrix3D mat = new Matrix3D(cos, -sin, 0, 0, sin, cos, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);

        Point3D p = new Point3D(1, 0, 0);

        Point3D transformedP = mat.Transform(p);


        Debug.WriteLine("p = " + p);
        Debug.WriteLine("mat = " + mat);
        Debug.WriteLine("transformedP = " + transformedP);

    }

不幸的是,结果被转换为P = {0.707,-0.707,0}而未转换为P = {0.707,0.707,0}

Unfortunately, the result is transformedP = {0.707, -0.707, 0} and NOT transformedP= {0.707, 0.707, 0}

我在做什么错了?

推荐答案

由于WPF将向量按行存储在矩阵中,因此您需要交换它们:

Since WPF stores vectors in matrices row-wise you need to exchange those:

Matrix3D mat = new Matrix3D(cos, sin, 0, 0, -sin, cos, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);

这篇关于Matrix3D在WPF中围绕z正旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 12:51