6

I am trying to make a little GUI library in c#, but after doing some research on matrix transformations I found out that I need a Matrix3x3 to store rotation, scale, and translation of a Vector2. But in the C# System.Numerics there is only a Matrix3x2 or Matrix4x4? Could I use one of those instead? If so how would I go about it? And why isnt there a Matrix3x3 in the standard library?

I am very new to Matrix and Vector programming, So sorry if this is a stupid question.

Thanks in advance.

Fortega
  • 19,463
  • 14
  • 75
  • 113
FutureCake
  • 2,614
  • 3
  • 27
  • 70
  • Either a List> or a List – jdweng Sep 13 '19 at 10:15
  • You may be looking for [System.Drawing2D.Matrix](https://learn.microsoft.com/de-de/dotnet/api/system.drawing.drawing2d.matrix?view=netframework-4.8) which is 3x3. - And there is also [System.Drawing.ColorMatrix](https://learn.microsoft.com/de-de/dotnet/api/system.drawing.imaging.colormatrix?view=netframework-4.8), which is 5x5. - Or take a look at [Math.NET](http://numerics.mathdotnet.com/) – TaW Sep 13 '19 at 11:10
  • @Taw I cant acces the System.Drawing2D namespace, i am using Dotnet on mac – FutureCake Sep 13 '19 at 11:58
  • You really need to add that to the question and to the Tags! – TaW Sep 13 '19 at 12:13
  • Sorry so late but the reason Matrix3x2 is not Matrix3x3 is because of the homogeneous coordinates and linearity which makes the 3rd column always (0 0 1) so it is not stored. So why not Matrix4x3? The last column comes into play when you get to 3d projections. – escape-llc Jan 31 '22 at 11:57

1 Answers1

3

You can use Matrix4x4. Start with an identity matrix and fill the top left 3×3 elements with your matrix.

For example to solve a 3×3 system of equations:

// Find the barycentric coordinates
//
// Solve for (wA,wB,wC):
// | px |   | ax bx cx | | wA |
// | py | = | ay by cy | | wB |
// | pz |   | az bz cz | | wC |

var m = new Matrix4x4(
    A.X, B.X, C.X, 0,
    A.Y, B.Y, C.Y, 0,
    A.Z, B.Z, C.Z, 0,
    0, 0, 0, 1);

if (Matrix4x4.Invert(m, out Matrix4x4 u))
{
    var w = new Vector3(
        u.M11*P.X + u.M12*P.Y + u.M13*P.Z,
        u.M21*P.X + u.M22*P.Y + u.M23*P.Z,
        u.M31*P.X + u.M32*P.Y + u.M33*P.Z);

    // ...
}

As for the reasons, the intent of System.Numerics has to be related to computer graphics since it utilizes Homogeneous Coordinates in which 3D vectors contain 4 elements. Three regular coordinates and a scalar weight factor. The math with homogeneous coordinates for computer graphics is vastly simplified. The only reason there is a Vector3 is because a Vector4 should be treated as a vector of 3 elements plus a scalar, and thus Vector3 should be used in composition and decomposition of the homogeneous coordinates. It means that not all 4 elements can be treated equally, and sometimes you need to do things with the vector (first three elements) separately from the scalar value (fourth element).

Also System.Numerics uses single precision float elements, which are almost never used in scientific computing, but universally applied to computer graphics for their speed. Hopefully one day when the CLR supports AVX512 there will be double precision numeric intrinsics that scientists can actually use.

John Alexiou
  • 28,472
  • 11
  • 77
  • 133