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
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
rubik's cubes are 3D
basically i have this
Sure they are. Matrices are 2D constructs by nature tho 😛
Ok so it's not a matrix, it's just an array of arrays
yeah sure
whatever this is ☝️
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:
And turn it into
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, ...
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[,]
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)ohhh thats what that means
oh thats pretty cool
yes of course
also these multidimensional arrays are trash in C#, save yourself the trouble
what do i do instead?
You can use a 1D array and use the handy formula
x + y * width
so a triple coordinate index?
this[int x, int y, int width]
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 inputohh ok i see now
makes sense
wait so
i treat the 3D array as a 1D array
A 2D as a 1D array
You can also treat a 3D one as 1D, the formula isn't that much harder actually 😄
ah ok makes sense
peter can you check out https://discord.com/channels/143867839282020352/1287311733794537502 for me?
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 😄
i updated the post and you need to scroll all the way to the bottom