Skip to content

Instantly share code, notes, and snippets.

@poemdexter
Last active March 7, 2016 16:07

Revisions

  1. poemdexter revised this gist Mar 7, 2016. 1 changed file with 9 additions and 4 deletions.
    13 changes: 9 additions & 4 deletions ian.cs
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,12 @@
    // oldArray newArray
    // 0,1,2 6,7,8
    // 3,4,5 --> 3,4,5
    // 6,7,8 0,1,2
    /*
    a 2D array can easily be represented by a standard array. given an array that has a size equal to a square number,
    reverse the order of the "rows" into a new array without changing the order of the elementes in each row.
    example:
    oldArray newArray
    row 1 [0,1,2 row 3 [6,7,8
    row 2 3,4,5 --> row 2 3,4,5
    row 3 6,7,8] row 1 0,1,2]
    */

    int x = width; // known size
    int size = width * width; // always a square
  2. poemdexter created this gist Mar 7, 2016.
    15 changes: 15 additions & 0 deletions ian.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    // oldArray newArray
    // 0,1,2 6,7,8
    // 3,4,5 --> 3,4,5
    // 6,7,8 0,1,2

    int x = width; // known size
    int size = width * width; // always a square
    int[] newArray = new int[size];

    for (int i = 0; i < width; i++) {
    for (int j = 0; j < width; j++) {
    int oldArrayPos = size - width * (i + 1);
    newArray[i * width + j] = oldArray[oldArrayPos + j];
    }
    }