旋转矩阵

her*_*ron -2 c# math matrix winforms

目标

想象一下,我们有像矩阵一样

a11 a12 a13
a21 a22 a23
a31 a32 a33
Run Code Online (Sandbox Code Playgroud)

我想要做的是,从文本框值旋转此矩阵,以便,例如,如果我写2并按rotate,程序必须保持矩阵的两个对角值(在这种情况下a11,a22,a33,a13,a31)和旋转2顺时针其他值.所以结果必须如此

a11 a32 a13
a23 a22 a21
a31 a12 a33
Run Code Online (Sandbox Code Playgroud)

它必须适用于所有N x N大小的矩阵,并且如您所见,每4次旋转将矩阵置于默认状态.

我做了什么

所以想法就是这样,我有两种形式.首先获取矩阵的大小(1值,例如,如果它是5,则生成5x5矩阵).当我按下OK它时会生成第二个表单文本框矩阵

表格1代码

    private void button1_Click(object sender, EventArgs e)
    {
        int matrixSize;
        matrixSize = int.Parse(textBox1.Text);
        Form2 form2 = new Form2(matrixSize);
        form2.Width = matrixSize * 50 + 100;
        form2.Height = matrixSize *60 + 200;
        form2.Show();            
        //this.Hide();
    }
Run Code Online (Sandbox Code Playgroud)

表单2代码从给定值生成文本框矩阵,并将随机值放入此字段中

public Form2(int matrSize)
        {
            int counter = 0;
            InitializeComponent();
            TextBox[] MatrixNodes = new TextBox[matrSize*matrSize];
            Random r = new Random();
            for (int i = 1; i <= matrSize; i++)
            {
                for (int j = 1; j <= matrSize; j++)
                {
                    var tb = new TextBox();                    
                    int num = r.Next(1, 1000);
                    MatrixNodes[counter] = tb;
                    tb.Name = string.Format("Node_{0}{1}", i, j);
                    Debug.Write(string.Format("Node_{0}{1}", i, j));
                    tb.Text = num.ToString();
                    tb.Location = new Point(j * 50, i * 50);
                    tb.Width = 30;
                    tb.Visible = true;
                    this.splitContainer1.Panel2.Controls.Add(tb);
                    counter++;
                }
            }

        }
Run Code Online (Sandbox Code Playgroud)

表单2有1个用于控制旋转的文本框(其他文本框是以编程方式生成的).我想要做的是,当我输入旋转计数并按下Enter此文本框时,我想旋转文本框矩阵,如上所述.无法弄清楚该怎么做.

Sta*_*Who 9

将两个对角线复制到单独的阵列,然后旋转矩阵并替换对角线.下面的代码显示了每个步骤

class Program
{
    static void Main(string[] args)
    {
        int matrixSize = 3;
        string[,] matrix = new string[matrixSize,matrixSize];

        //create square matrix
        for (int x = 0; x < matrixSize; x++)
        {
            for (int y = 0; y < matrixSize; y++)
            {
                matrix[x, y] = "a" + (x + 1).ToString() + (y + 1).ToString();
            }
        }

        Console.WriteLine(Environment.NewLine + "Base square matrix");

        for (int x = 0; x < matrixSize; x++)
        {              
            for (int y = 0; y < matrixSize; y++)
            {
                Console.Write(matrix[x, y] + " ");
            }
            Console.Write(Environment.NewLine);
        }
        Console.ReadKey();

        //copy diagonals
        string[] leftDiagonal = new string[matrixSize];
        string[] rightDiagonal = new string[matrixSize];
        for (int x = 0; x < matrixSize; x++)
        {
            leftDiagonal[x] = matrix[x, x];
            rightDiagonal[x] = matrix[matrixSize - 1 - x, x];
        }

        Console.WriteLine(Environment.NewLine + "Diagonals");

        for (int x = 0; x < matrixSize; ++x)
        {
            Console.Write(leftDiagonal[x] + " " + rightDiagonal[x] + Environment.NewLine);
        }
        Console.ReadKey();

        //rotate matrix
        string[,] rotatedMatrix = new string[matrixSize, matrixSize];
        for (int x = 0; x < matrixSize; x++)
        {
            for (int y = 0; y < matrixSize; y++)
            {
                rotatedMatrix[x, y] = matrix[matrixSize - y - 1, x];
            }
        }
        Console.WriteLine(Environment.NewLine + "Rotated");

        for (int x = 0; x < matrixSize; x++)
        {
            for (int y = 0; y < matrixSize; y++)
            {
                Console.Write(rotatedMatrix[x, y] + " ");
            }
            Console.Write(Environment.NewLine);
        }
        Console.ReadKey();

        //rotate matrix again
        string[,] rotatedMatrixAgain = new string[matrixSize, matrixSize];
        for (int x = 0; x < matrixSize; x++)
        {
            for (int y = 0; y < matrixSize; y++)
            {
                rotatedMatrixAgain[x, y] = rotatedMatrix[matrixSize - y - 1, x];
            }
        }
        Console.WriteLine(Environment.NewLine + "Rotated again");

        for (int x = 0; x < matrixSize; x++)
        {
            for (int y = 0; y < matrixSize; y++)
            {
                Console.Write(rotatedMatrixAgain[x, y] + " ");
            }
            Console.Write(Environment.NewLine);
        }
        Console.ReadKey();

        //replace diagonals
        for (int x = 0; x < matrixSize; x++)
        {
            rotatedMatrixAgain[x, x] = leftDiagonal[x];
            rotatedMatrixAgain[matrixSize - 1 - x, x] = rightDiagonal[x];
        }

        Console.WriteLine(Environment.NewLine + "Completed" + Environment.NewLine);

        for (int x = 0; x < matrixSize; x++)
        {
            for (int y = 0; y < matrixSize; y++)
            {
                Console.Write(rotatedMatrixAgain[x, y] + " ");
            }
            Console.Write(Environment.NewLine);
        }
        Console.ReadKey();
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 答案中的代码并不意味着成品.它详细解释了算法,它可以达到您所需的结果.具体实现,无论框架如何,完全取决于应用程序设计,在您的情况下,这意味着如果我在表单中为您实现这一点,我将剥夺您从挫折,通过理解到成就的惊人旅程:) (8认同)
  • 您需要将用户界面与逻辑分离.良好的编程并不关心您是使用控制台还是表单,甚至是Web.尝试构建一个只执行所需逻辑的类.如果不起作用,请在此处发布.你有什么需要参考. (4认同)