【问题标题】:Calculate transformation matrix from scale, rotation, translate and skew从缩放、旋转、平移和倾斜计算变换矩阵
【发布时间】:2020-07-22 09:45:33
【问题描述】:

我需要代码来计算缩放、旋转、平移和倾斜分量的二维变换矩阵。

代码应该像这个计算器一样工作:http://angrytools.com/css-generator/transform,我试图理解下面的解释,但我数学不好。

这是我正在尝试实现的转换器的示例代码。

  public class TransformationMatrix2D
  {
       public double A { get; set; }

       public double B { get; set; }

       public double C { get; set; }

       public double D { get; set; }

       public double TX { get; set; }

       public double TY { get; set; }
  }




public class TransformationMatrix2DCalculator
{
     public (double X, double Y) Translate { get; set; }
     public double Rotate { get; set; }
     public (double X, double Y) Scale { get; set; }
     public double Skew { get; set; }

     private const double TransformDegrees = 180 / Math.PI;

     public TransformationMatrix2DCalculator(double translateX = 0, double translateY = 0, double rotate = 0, double scaleX = 1, double scaleY = 1, double skew = 0)
     {
         Translate = (translateX, translateY);
         Rotate = rotate;
         Scale = (scaleX, scaleY);
         Skew = skew;
     }

     public TransformationMatrix2D Matrix => null; //TODO: to be implemented
}

更新

我找到了如何应用旋转、平移和缩放,但我不知道如何应用倾斜:

public TransformationMatrix2D GetMatrix()
{
    var angle = Rotate / TransformDegrees;
    var a = Math.Cos(angle) * Scale.X;
    var b = Math.Sin(angle) * Scale.X; 
    var c = -Math.Sin(angle) * Scale.Y;
    var d = Math.Cos(angle) * Scale.Y;
    //TODO: complete transformations (skew support is missing)
    return new TransformationMatrix2D()
    {
        A =  a,
        B =  b,
        C =  c,
        D =  d,
        TX =  Translate.X,
        TY = Translate.Y
    };
}

【问题讨论】:

    标签: c# matrix


    【解决方案1】:

    通过将每个变换表示为单独的矩阵然后将它们相乘来组合变换。

    控制倾斜的剪切矩阵使用b和c参数分别控制x和y维度的倾斜。

    但是在乘法之后,所有 6 个 2d 矩阵元素都可能受到歪斜的影响。

    你需要在你的矩阵类上定义一个乘法方法,比如:

    public static void Mult(in Matrix3d left, in Matrix3d right, out Matrix3d result)
    {
        double leftM11 = left.Row0.X;
        double leftM12 = left.Row0.Y;
        double leftM13 = left.Row0.Z;
        double leftM21 = left.Row1.X;
        double leftM22 = left.Row1.Y;
        double leftM23 = left.Row1.Z;
        double leftM31 = left.Row2.X;
        double leftM32 = left.Row2.Y;
        double leftM33 = left.Row2.Z;
        double rightM11 = right.Row0.X;
        double rightM12 = right.Row0.Y;
        double rightM13 = right.Row0.Z;
        double rightM21 = right.Row1.X;
        double rightM22 = right.Row1.Y;
        double rightM23 = right.Row1.Z;
        double rightM31 = right.Row2.X;
        double rightM32 = right.Row2.Y;
        double rightM33 = right.Row2.Z;
    
        result.Row0.X = (leftM11 * rightM11) + (leftM12 * rightM21) + (leftM13 * rightM31);
        result.Row0.Y = (leftM11 * rightM12) + (leftM12 * rightM22) + (leftM13 * rightM32);
        result.Row0.Z = (leftM11 * rightM13) + (leftM12 * rightM23) + (leftM13 * rightM33);
        result.Row1.X = (leftM21 * rightM11) + (leftM22 * rightM21) + (leftM23 * rightM31);
        result.Row1.Y = (leftM21 * rightM12) + (leftM22 * rightM22) + (leftM23 * rightM32);
        result.Row1.Z = (leftM21 * rightM13) + (leftM22 * rightM23) + (leftM23 * rightM33);
        result.Row2.X = (leftM31 * rightM11) + (leftM32 * rightM21) + (leftM33 * rightM31);
        result.Row2.Y = (leftM31 * rightM12) + (leftM32 * rightM22) + (leftM33 * rightM32);
        result.Row2.Z = (leftM31 * rightM13) + (leftM32 * rightM23) + (leftM33 * rightM33);
    }
    

    来自OpenTK Matrix3d class

    并将您的复合变换定义为元素变换(例如旋转、平移、缩放和旋转)的产物。

    也阅读transformation matrices

    【讨论】:

      猜你喜欢
      • 2013-02-14
      • 2017-02-09
      • 2011-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-05
      相关资源
      最近更新 更多