Skip to content

Instantly share code, notes, and snippets.

@poemdexter
Last active March 7, 2016 16:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save poemdexter/225a98fd94eaba07aa29 to your computer and use it in GitHub Desktop.
Save poemdexter/225a98fd94eaba07aa29 to your computer and use it in GitHub Desktop.
flipping pixels something something for ian
/*
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
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];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment