Skip to content

Instantly share code, notes, and snippets.

@jagt
Last active January 4, 2019 13:04
Show Gist options
  • Save jagt/10ea5fc3af26cfbf5e22b0bb939343f8 to your computer and use it in GitHub Desktop.
Save jagt/10ea5fc3af26cfbf5e22b0bb939343f8 to your computer and use it in GitHub Desktop.
c# 2d array example
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