how do i rotate part of a multidimensional matrix?

i have a 2D matrix representing a rubik's cube (figured that a 3D matrix would be both quite difficult and a massive overhead) and i want to rotate just one of the inner 1D matrices to rotate just one face of the side. how would i do this?
21 Replies
LPeter1997
LPeter19972mo ago
There's a lot of missing detail here. How do you represent a 2D 3D rubik's cube in a 2D matrix? (What even is a 2D matrix compared to a 3D one, don't you mean tensors?) Edit: whoops typo
it’s raining outside
rubik's cubes are 3D basically i have this
LPeter1997
LPeter19972mo ago
Sure they are. Matrices are 2D constructs by nature tho 😛
it’s raining outside
[
# Green side
[0, 1, 2,
3, 4, 5,
6, 7, 8],

# Orange side
[0, 1, 2,
3, 4, 5,
6, 7, 8],

... # Other sides
]
[
# Green side
[0, 1, 2,
3, 4, 5,
6, 7, 8],

# Orange side
[0, 1, 2,
3, 4, 5,
6, 7, 8],

... # Other sides
]
LPeter1997
LPeter19972mo ago
Ok so it's not a matrix, it's just an array of arrays
it’s raining outside
yeah sure
public Colour[,] Matrix;
public Colour[,] Matrix;
whatever this is ☝️
LPeter1997
LPeter19972mo ago
2D array 😄 You basically need to implement 3 operations on these arrays: 1. "Rotate" the array elements by 90 degrees. This is the trickiest one, where you basically take:
1 2 3
4 5 6
7 8 9
1 2 3
4 5 6
7 8 9
And turn it into
7 4 1
8 5 2
9 6 3
7 4 1
8 5 2
9 6 3
2. Replace a column of numbers with another column of numbers 3. Replace a row of numbers with another row of numbers Once you have these, the sad news is that this is just case bashing then, making sure you invoke the right methods on the right sides of your cube Note that you don't need to implement fancy crap like counter-clockwise turning, you can just call your rotation method 3 times But out of these 3 elementary operations you can basically implement turning. The "which side performs which operations" depends on how you flattened out your sides, what orientation they are, ...
it’s raining outside
yeah okay got it i wouldve just done: 1. index 0 goes to index 2 2. index 1 goes to index 5 3. index 2 goes to index 8 ... i heard theres an Array.Rotate method but how come i cant use that? or do i have to redefine my array to be Colour[][] instead of Colour[,]
LPeter1997
LPeter19972mo ago
Array rotation is a little different, it generally means shifting elements in a way where the ones pushed out of bounds appear on the other side. For example, if you rotate [a, b, c, d, e, f, g] left by 2, you'd get [c, d, e, f, g, a, b] I mean if it's a fixed 3 by 3, that's a perfectly fine implementation for it, just watch out for erasing an element when doing it (you'll need one temporary variable to do this swapping successfully)
it’s raining outside
ohhh thats what that means oh thats pretty cool yes of course
Anton
Anton2mo ago
also these multidimensional arrays are trash in C#, save yourself the trouble
it’s raining outside
what do i do instead?
LPeter1997
LPeter19972mo ago
You can use a 1D array and use the handy formula x + y * width
it’s raining outside
so a triple coordinate index? this[int x, int y, int width]
LPeter1997
LPeter19972mo ago
This is an index into a 1D array to handle it like 2D Instead of a[x, y] you can do a[x + y * width] Width is the width of the array so it’s not an actual input
it’s raining outside
ohh ok i see now makes sense wait so i treat the 3D array as a 1D array
LPeter1997
LPeter19972mo ago
A 2D as a 1D array You can also treat a 3D one as 1D, the formula isn't that much harder actually 😄
it’s raining outside
ah ok makes sense peter can you check out https://discord.com/channels/143867839282020352/1287311733794537502 for me?
LPeter1997
LPeter19972mo ago
What's the exact question there? Attributes are basically "just" metadata for .NET you can read with reflection (a more advanced thing is using them in SourceGenerators, tho that's really not a beginners topic). In python, decorators are basically sugar for a function call, so they are really really different mechanisms If the thing became about doing your own CLI, I'd personally use a library (like System.CommandLine, but there's a bunch of them) Doing your own isn't particularly hard, just freaking annoying 😄
it’s raining outside
i updated the post and you need to scroll all the way to the bottom
Want results from more Discord servers?
Add your server