Reputation: 1
I'm getting started with XNA and trying to access the fields (M11, M21, etc) in a for loop, as follows.
for (int x = 0; x < 4; x++) {
for (int y = 0; y < 4; y++)
{
// Can't use array notation with this struct. Doesn't work.
matrix[x][y]
// Similar to above, but like a 2D array. Doesn't work either.
matrix[x, y]
// ERROR - I'm using a variable like a method. Doesn't work.
matrix(x, y)
}
}
So, I know I'm going to kick myself after this, but please tell me how I can access the individual fields of an XNA matrix in order to loop through them with a for loop. If I can do this in C++ why not in C#?
It's an example in '3D Game Programming with DirectX 11' by Frank Luna in the matrix chapter and I want to find out more about matrices, so I'd like to be able to do this shorthand. In the meantime I'll write it all out, from M11...M44.
Upvotes: 0
Views: 162
Reputation: 4857
As others have already pointed out, there is no built-in way to access the fields of a matrix by their coordinates. It's not something you'll need to do very often in non-contrived scenarios, and accessing the fields directly is theoretically faster (and the Framework's matrix code is the sort of place where these sorts of micro-optimizations can be important).
That said, if I was hellbent on writing code to enable this behavior, it would probably look very similar to this:
public static class MatrixUtil
{
public static void Set(ref Matrix m, Int32 row, Int32 col, Single value)
{
if (row < 1 || row > 4) throw new ArgumentOutOfRangeException("row");
if (col < 1 || col > 4) throw new ArgumentOutOfRangeException("col");
switch ((row * 10) + col)
{
case 11: m.M11 = value; break;
case 12: m.M12 = value; break;
case 13: m.M13 = value; break;
case 14: m.M14 = value; break;
case 21: m.M21 = value; break;
case 22: m.M22 = value; break;
case 23: m.M23 = value; break;
case 24: m.M24 = value; break;
case 31: m.M31 = value; break;
case 32: m.M32 = value; break;
case 33: m.M33 = value; break;
case 34: m.M34 = value; break;
case 41: m.M41 = value; break;
case 42: m.M42 = value; break;
case 43: m.M43 = value; break;
case 44: m.M44 = value; break;
}
}
public static Single Get(ref Matrix m, Int32 row, Int32 col)
{
if (row < 1 || row > 4) throw new ArgumentOutOfRangeException("row");
if (col < 1 || col > 4) throw new ArgumentOutOfRangeException("col");
switch ((row * 10) + col)
{
case 11: return m.M11;
case 12: return m.M12;
case 13: return m.M13;
case 14: return m.M14;
case 21: return m.M21;
case 22: return m.M22;
case 23: return m.M23;
case 24: return m.M24;
case 31: return m.M31;
case 32: return m.M32;
case 33: return m.M33;
case 34: return m.M34;
case 41: return m.M41;
case 42: return m.M42;
case 43: return m.M43;
case 44: return m.M44;
}
throw new InvalidOperationException();
}
}
Which could then be called like this:
var m = Matrix.Identity;
for (int row = 1; row <= 4; row++)
{
for (int col = 1; col <= 4; col++)
{
Console.WriteLine(MatrixUtil.Get(ref m, row, col));
}
}
Is there something specific you think this will be useful for?
Upvotes: 1