1

How can I rotate and flip the 2D char array so the letters display in different order?

char[,] array = new char[6,4] { 
    { 'A', 'B', 'C', 'D' },
    { 'E', 'F', 'G', 'H' },
    { 'I', 'J', 'K', 'L' },
    { 'M', 'N', 'O', 'P' },
    { 'Q', 'R', 'S', 'T' },
    { 'U', 'V', 'W', 'X' }
};

6x4 Matrix

Orig.       90°             180°        270°            Flip H      Flip V
A B C D     U Q M I E A     X W V U     D H L P T X     D C B A     U V W X
E F G H     V R N J F B     T S R Q     C G K O S W     H G F E     Q R S T
I J K L     W S O K G C     P O N M     B F J N R V     L K J I     M N O P
M N O P     X T P L H D     L K J I     A E I M Q U     P O N M     I J K L
Q R S T                     H G F E                     T S R Q     E F G H
U V W X                     D C B A                     X W V U     A B C D

After the array has been rotated, I want to String.Join() all the rows into a single line for another use.

So if you were to flip the array 90° and String.Join()
ABCDEFGHIJKLMNOPQRSTUVWX would appears as UQMIEAVRNJFBWSOKGCXTPLHD.


Example

Here is a rotation method 90°.

https://stackoverflow.com/a/42535/6806643

char[,] rotated = RotateMatrix(array, 4);


static char[,] RotateMatrix(char[,] matrix, int n) {
    char[,] ret = new char[n, n];

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            ret[i, j] = matrix[n - j - 1, i];
        }
    }

    return ret;
}

But I can only get it to display up to 4 columns

M I E A
N J F B
O K G C
P L H D

when it should be 6

U Q M I E A
V R N J F B
W S O K G C
X T P L H D

http://rextester.com/BAMTJ38063

Herohtar
  • 5,347
  • 4
  • 31
  • 41
Matt McManis
  • 4,475
  • 5
  • 38
  • 93
  • 3
    The code you included is for a square 2d array, you'll need to updated it to work with a rectangular one. – ameer Dec 08 '17 at 18:52
  • 2
    How is this related to WPF? – Clemens Dec 08 '17 at 18:53
  • @Clemens I had WPF grid in my original question but changed it. I'll remove the tag. – Matt McManis Dec 08 '17 at 19:01
  • @Odrai As ameer pointed out, that seems to be for a square array. How can I change it to a rectangular? – Matt McManis Dec 08 '17 at 19:03
  • 1
    @MattMcManis Take a look at other answers from the linked (duplicate) question - not just accepted. – Ivan Stoev Dec 08 '17 at 19:16
  • FYI, standard matrix notation is rows x columns, so your original matrix is 6x4. – Herohtar Dec 08 '17 at 19:25
  • [There](https://github.com/leofun01/Transformer/blob/4402eb577ce84c177ef7e1d6b8df597e09a4f63f/DotNetTransformer/Extensions/ArrayExtension.cs#L16) exist an algorithm that allows to rotate and flip 2d array. Also you can use [this](https://github.com/leofun01/Transformer/blob/3aff9fa0563f6cfc2f1296278fac7a7015c6c18a/DotNetTransformer/Extensions/ArrayExtension.cs#L49) algorithm with System.Drawing.RotateFlipType. – leofun01 Aug 16 '18 at 19:54

0 Answers0