Last active
January 4, 2019 13:04
-
-
Save jagt/10ea5fc3af26cfbf5e22b0bb939343f8 to your computer and use it in GitHub Desktop.
c# 2d array example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Swap(ref float lhs, ref float rhs) | |
{ | |
float tmp = lhs; | |
lhs = rhs; | |
rhs = tmp; | |
} | |
void InplaceFlipX(float[,] arr) | |
{ | |
int rowLength = arr.GetLength(1); | |
for (int ix = 0; ix < arr.GetLength(0); ix++) // rows | |
for (int jx = 0; jx < rowLength / 2; jx++) // columns | |
Swap(ref arr[ix, jx], ref arr[ix, rowLength - jx - 1]); | |
} | |
void InplaceFlipY(float[,] arr) | |
{ | |
int columnLength = arr.GetLength(0); | |
for (int ix = 0; ix < arr.GetLength(1); ix++) // rows | |
for (int jx = 0; jx < columnLength / 2; jx++) // columns | |
Swap(ref arr[jx, ix], ref arr[columnLength - jx - 1, ix]); | |
} | |
private void InplaceFlipX(float[,,] arr) | |
{ | |
int rowLength = arr.GetLength(1); | |
for (int ix = 0; ix < arr.GetLength(0); ix++) // rows | |
for (int jx = 0; jx < rowLength / 2; jx++) // columns | |
for (int kx = 0; kx < arr.GetLength(2); kx++) | |
Swap(ref arr[ix, jx, kx], ref arr[ix, rowLength - jx - 1, kx]); | |
} | |
private void InplaceFlipY(float[,,] arr) | |
{ | |
int columnLength = arr.GetLength(0); | |
for (int ix = 0; ix < arr.GetLength(1); ix++) // rows | |
for (int jx = 0; jx < columnLength / 2; jx++) // columns | |
for (int kx = 0; kx < arr.GetLength(2); kx++) | |
Swap(ref arr[jx, ix, kx], ref arr[columnLength - jx - 1, ix, kx]); | |
} | |
void Main() | |
{ | |
var foo = new float[2, 3] | |
{ | |
{1,2,3}, | |
{4,5,6}, | |
}; | |
foo.Dump(); | |
InplaceFlipX(foo); | |
foo.Dump(); | |
InplaceFlipY(foo); | |
foo.Dump(); | |
var boo = new float [2,3,2] { | |
{ {1,2}, {2,3}, {3,4} }, | |
{ {4,5}, {5,6}, {6,7} }, | |
}; | |
boo.Dump(); | |
InplaceFlipX(boo); | |
boo.Dump(); | |
InplaceFlipY(boo); | |
boo.Dump(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment