Answers for "c# get position of element in two dimensional matrix"

C#
0

c# get position of element in two dimensional matrix

public static class ExtensionMethods
{
    public static Tuple<int, int> CoordinatesOf<T>(this T[,] matrix, T value)
    {
        int w = matrix.GetLength(0); // width
        int h = matrix.GetLength(1); // height

        for (int x = 0; x < w; ++x)
        {
            for (int y = 0; y < h; ++y)
            {
                if (matrix[x, y].Equals(value))
                    return Tuple.Create(x, y);
            }
        }

        return Tuple.Create(-1, -1);
    }
}
Posted by: Guest on June-10-2021

Code answers related to "c# get position of element in two dimensional matrix"

C# Answers by Framework

Browse Popular Code Answers by Language