1

下面的示例是在 C# 中从 XNA 中的 Texture2D 中提取数据。我的理解是 Texture2D.GetData 最初不能用于将数据拉入二维数组。

如果一维数组包含如下值:1, 2, 3, 4, 5, 6, 7, 8, 9

是否可以将该一维数组复制到二维数组中,然后二维数组具有如下值:

1, 2, 3
4, 5, 6
7, 8, 9

我的目标是将整个数组从一维复制到二维,而不是遍历和计算索引。我当前的代码是这样的:

    Color[,] TextureDataTo2DArray(Texture2D texture)
    {
        Color[] colors1D = new Color[texture.Width * texture.Height];
        texture.GetData(colors1D);

        Color[,] colors2D = new Color[texture.Width, texture.Height];
        for (int x = 0; x < texture.Width; x++)
            for (int y = 0; y < texture.Height; y++)
                colors2D[x, y] = colors1D[x + y * texture.Width];

        return colors2D;
    }
4

3 回答 3

1

在将一维数组复制到二维数组中时,模算术是您的朋友:

Color[,] TextureDataTo2DArray(Texture2D texture)
    {
        Color[] colors1D = new Color[texture.Width * texture.Height];
        texture.GetData(colors1D);

        Color[,] colors2D = new Color[texture.Width, texture.Height];
        for (int i = 0; i < colors1D.Length; i++)
            colors2D[Math.Floor(i / texture.Width), i % texture.Width] = colors1D[i];

        return colors2D;
    }

但是,最终,如果您要重塑一个数组,您将不得不计算一个形状与另一个形状之间的对应关系。

于 2013-05-02T14:11:55.750 回答
0

你可以只增加一个计数器

index = 0;

for (int x = 0; x < texture.Width; x++)
    for (int y = 0; y < texture.Height; y++)
        colors2D[x, y] = colors1D[index++];
于 2013-05-02T14:48:46.160 回答
0
        static void Main(string[] args)
        {
            int Size_of_OneDimensional = 0;
            int Start_Index = 0;
            int row = 0;
            int column=0;


            Console.WriteLine("Enter The number of elements you want to add in 1-D Array : ");
            Size_of_OneDimensional = Convert.ToInt32(Console.ReadLine());

            int[] One_Dimensional = new int[Size_of_OneDimensional];

                for (int i = 0; i < Size_of_OneDimensional; i++)
                    {
                        Console.WriteLine("Enter "+i+" Element");
                        One_Dimensional[i] = Convert.ToInt32(Console.ReadLine());
                    }

                Console.WriteLine("Emter Number of Row : ");
                row = Convert.ToInt32(Console.ReadLine());

                Console.WriteLine("Emter Number of Colum : ");
                column= Convert.ToInt32(Console.ReadLine());

                int[,] Two_Dimensional = new int[row, column];

                Console.WriteLine("Here is your 2-D Array");

                for (int i = 0; i < row; i++)
                    {
                    if (Start_Index == One_Dimensional.Length)
                        break;
                    for(int j = 0; j < column; j++)
                        {
                        Two_Dimensional[i, j] = One_Dimensional[Start_Index];
                        Start_Index++;     
                        Console.Write(Two_Dimensional[i, j]);
                        }
                        Console.WriteLine();
                    }
                Console.ReadKey();
        }
    }
   }
于 2017-07-14T10:11:23.523 回答