Home > Net >  Multiply element in multidimensional array
Multiply element in multidimensional array

Time:10-25

-> Please help with the implementation of a function

Basically I want to manipulate an image

Imagine that the image can be brake apart in pixels -> each pixel of the image will be (for an easy example) "a number"

        int[,] originalImage = new int[,]
        {
            { 1,2,3 },
            { 4,5,6 }
        };

And I need to double the size of the image x2 (or x3, x4, etc) the pixel "1" when double the size will be copied on te right position, down and on the corner

            int[,] expectedResult = new int[,]
        {
            { 1,1,2,2,3,3 },
            { 1,1,2,2,3,3 },
            { 4,4,5,5,6,6 },
            { 4,4,5,5,6,6 }
        };

How does the function can be implemented?

Multiply(int[,] originalImage, int multiply)

CodePudding user response:

Note there are oodles of libraries that would do this in math or image processing. However, purely for academic purposes

Given

private static int[,] DoStuff(int[,] array,int size)
{
   var sizeX = array.GetLength(0);
   var sizeY = array.GetLength(1);

   var newArray = new int[sizeX * size, sizeY * size];

   for (var i = 0; i < newArray.GetLength(0); i  )
   for (var j = 0; j < newArray.GetLength(1); j  )
      newArray[i, j] = array[i / size, j / size];

   return newArray;
}

Usage

int[,] originalImage = 
{
   { 1, 2, 3 },
   { 4, 5, 6 }
};

var newArray = DoStuff(originalImage,3);

for (var i = 0; i < newArray.GetLength(0); i  )
{
   for (var j = 0; j < newArray.GetLength(1); j  )
      Console.Write(newArray[i, j]   " ");
   Console.WriteLine();
}

Output

1 1 1 2 2 2 3 3 3
1 1 1 2 2 2 3 3 3
1 1 1 2 2 2 3 3 3
4 4 4 5 5 5 6 6 6
4 4 4 5 5 5 6 6 6
4 4 4 5 5 5 6 6 6
  • Related