c# - Is it possible to copy a 1D array to a 2D array as described and if so, how? -
the example below in c# pulling data texture2d in xna. understanding texture2d.getdata can not used pull data 2d array initially.
if 1d array contains values so: 1, 2, 3, 4, 5, 6, 7, 8, 9
is possible copy single dimensional array 2d array 2d array has values so:
1, 2, 3 4, 5, 6 7, 8, 9
my goal copy whole array 1d 2d rather iterating through , calculating indexes. current code this:
color[,] texturedatato2darray(texture2d texture) { color[] colors1d = new color[texture.width * texture.height]; texture.getdata(colors1d); color[,] colors2d = new color[texture.width, texture.height]; (int x = 0; x < texture.width; x++) (int y = 0; y < texture.height; y++) colors2d[x, y] = colors1d[x + y * texture.width]; return colors2d; }
in copying 1d array 2d array, modular arithmetic friend:
color[,] texturedatato2darray(texture2d texture) { color[] colors1d = new color[texture.width * texture.height]; texture.getdata(colors1d); color[,] colors2d = new color[texture.width, texture.height]; (int = 0; < colors1d.length; i++) colors2d[math.floor(i / texture.width), % texture.width] = colors1d[i]; return colors2d; }
ultimately, though, if you're reshaping array, you're going have calculate correspondence between 1 shape , another.
Comments
Post a Comment