Advertisement Jump to content
Sign in to follow this  
menyo

Combine a 2D array with a 3D array?

This topic is 1746 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

 

I am looking for a way to combine a 2D array with a 3D array.

 

I have a function that returns a float[,] it gets populated with the width and height of my map. Now i am trying to do something like this:

 

float[,,] 3dArray = new float[amount, width, height];

 

for (int i = 0; i < amount; i++)

{

    3dArray = MethodReturningFloatArray[,];

}

 

So basically i want to populate the 2nd and 3th dimension of my 3D array with a 2D array generated by a method (float[,]).

Edited by menyo

Share this post


Link to post
Share on other sites
Advertisement

I don't know if my code is 100% correct, this I'm writing completely from head smile.png

 

Function for populating 2nd and 3rd dimension should go like this:

void PopulateJKDimensions(ref float[,,] 3dArray, int i, float[,] 2dArray)
{
    for (int j = 0; j < 2dArray.GetLength(0); j++)
        for (int k = 0; k < 2dArray.GetLength(1); k++)
            3dArray[i, j, k] = 2dArray[j, k];
}

What's important to say here is that appropriate dimensions have to be of same sizes, otherwise you need to add additional code to take care of it.

ref is to ensure that changes made inside function to 3dArray remain visible outside the function. I'm still not 100% sure if it would be the same without ref keyword.

 

And then for main code:

// ...

float[,,] 3dArray = new float[amount, width, height];
float[,] 2dArray;

for (int i = 0; i < amount; i++)
{
    2dArray = MethodReturning2dFloatArray( );
    PopulateJKDimensions(ref 3dArray, i, 2dArray);
}

// ...

EDIT: Or even easier:

// ...

float[,,] 3dArray = new float[amount, width, height];
float[,] 2dArray;

for (int i = 0; i < amount; i++)
{
    2dArray = MethodReturning2dFloatArray( );

    for (int j = 0; j < width; j++)
        for (int k = 0; k < height; k++)
            3dArray[i, j, k] = 2dArray[j, k];
}

// ...
Edited by Aurioch

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!