c# - Rotating matrices -


objectives

imagine that, have matrix like

a11 a12 a13 a21 a22 a23 a31 a32 a33 

what want is, textbox value rotate matrix that, example if write 2 , press rotate, program must keep both diagonal values of matrix (in case a11, a22, a33, a13, a31) , rotate 2 times clockwise other values. result must like

a11 a32 a13 a23 a22 a21 a31 a12 a33 

it must work n x n size matrices, , see every 4 rotation takes matrix default state.

what i've done

so idea that, have 2 forms. first takes size of matrix (1 value, example if it's 5, generates 5x5 matrix). when press ok generates second forms textbox matrix that

form 1 code

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

form 2 code generates textbox matrix given value , puts random values fields

public form2(int matrsize)         {             int counter = 0;             initializecomponent();             textbox[] matrixnodes = new textbox[matrsize*matrsize];             random r = new random();             (int = 1; <= matrsize; i++)             {                 (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, * 50);                     tb.width = 30;                     tb.visible = true;                     this.splitcontainer1.panel2.controls.add(tb);                     counter++;                 }             }          } 

form 2 has 1 textbox controlling rotation (others generated on fly, programmatically). want is, when enter rotation count , press enter on textbox, want rotate textbox matrix explained above. can't figure out how it.

copy both diagonals separate arrays, rotate matrix , replace diagonals. below code shows each step:

class program {     static void main(string[] args)     {         int matrixsize = 3;         string[,] matrix = new string[matrixsize,matrixsize];          //create square matrix         (int x = 0; x < matrixsize; x++)         {             (int y = 0; y < matrixsize; y++)             {                 matrix[x, y] = "a" + (x + 1).tostring() + (y + 1).tostring();             }         }          console.writeline(environment.newline + "base square matrix");          (int x = 0; x < matrixsize; x++)         {                           (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];         (int x = 0; x < matrixsize; x++)         {             leftdiagonal[x] = matrix[x, x];             rightdiagonal[x] = matrix[matrixsize - 1 - x, x];         }          console.writeline(environment.newline + "diagonals");          (int x = 0; x < matrixsize; ++x)         {             console.write(leftdiagonal[x] + " " + rightdiagonal[x] + environment.newline);         }         console.readkey();          //rotate matrix         string[,] rotatedmatrix = new string[matrixsize, matrixsize];         (int x = 0; x < matrixsize; x++)         {             (int y = 0; y < matrixsize; y++)             {                 rotatedmatrix[x, y] = matrix[matrixsize - y - 1, x];             }         }         console.writeline(environment.newline + "rotated");          (int x = 0; x < matrixsize; x++)         {             (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];         (int x = 0; x < matrixsize; x++)         {             (int y = 0; y < matrixsize; y++)             {                 rotatedmatrixagain[x, y] = rotatedmatrix[matrixsize - y - 1, x];             }         }         console.writeline(environment.newline + "rotated again");          (int x = 0; x < matrixsize; x++)         {             (int y = 0; y < matrixsize; y++)             {                 console.write(rotatedmatrixagain[x, y] + " ");             }             console.write(environment.newline);         }         console.readkey();          //replace diagonals         (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);          (int x = 0; x < matrixsize; x++)         {             (int y = 0; y < matrixsize; y++)             {                 console.write(rotatedmatrixagain[x, y] + " ");             }             console.write(environment.newline);         }         console.readkey();     } } 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -