# Nested For Loop Confusion

Hello,

I would appreciate anyones help on this nested for loop problem I’m having - I can’t seem to get my head around it, and I need a fresh pair of eyes to take a look for me.

Explanation

I have a simple float array that is actually a broken down image data that has a total Length of 217,088 (pixels essentially). The original image rect is 512 x 424 before it’s externally broken down into this single float array.

I also have 4 Meshes, each mesh is 128 x 424 along the X axis, which give a total mesh coverage of 512 x 424. (each mesh has a vertex count of 54,272. [4 * 54,272 = 217,088] : Same length as my array/pixels).

Each vertex in my Mesh represents a Pixel in the image array. However, the Image array is 1D, and the Mesh Array is 2D

## [69938-screen-shot-2016-05-12-at-221300.png*|69938]

Problem

I’m trying to match the ImageArray (well the data) with the Mesh. A Pixel for a Vertex
But because there are a mesh in 4 columns, it’s confusing me

The entire ImageArray is fed into each Mesh Object, which has its own script attached to it that tries to ‘convert’ the 1D into 2D, with respect to the Mesh Object. But my for loops appears to be wrong, as I don’t get the correct outcome.

This script is attached to all 4 of the MeshObjects, and is called every frame with the entire ImageArray being fed into it:

``````public void UpdateMesh(double[] _data){

int i = 0;

for(int _y = 0; _y < height; _y++){

for(int _x = 0; _x < width; _x++){

//int index = (_y * width) + _x;
int point_index = (_y ) + (x + _x);

double point = _data[point_index];
points *= new Vector3(_x, _y, (float)point);*
``````
• `````` 		i++;*
``````
• `````` 	}*
``````
• `````` }*
``````
• `````` _Mesh.vertices = points;*
``````
• }*
Where ‘y’ always equals zero, and ‘x’ equals 0 on the Mesh 0. 128 on the Mesh 1. 256 on the Mesh 2. And 384 on the Mesh 3. (It just progresses in 128 each time as the mesh is moving along the X axis). I think its more my ‘_Y’ loop that is causing the issue. I’m not sure?!

Well, your code is missing conceptionally those 4 meshes. Your point_index calculation seems strange. First because we don’t know what “x” actually is and second image data is usually layed out row by row in a flattened array, so your commented “index” is the right way to calculate the index into the image data array.

How you manage your 4 meshes is up to you. You don’t necessarily need to process the image row by row. You can process it column by column as well. That way you would work on one mesh at a time, however if you store all 4 vertices arrays anyways it doesn’t matter how you process the image.

``````Vector3[][] verts = new Vector3[4][];
for(int i = 0; i < verts.Length; i++)
{
verts _= new Vector3[128*height];_
``````

}

for(int _y = 0; _y < height; _y++)
{
for(int _x = 0; _x < width; _x++)
{
int index = (y * width) + x;
float point = (float)_data[index];
verts[x / 128][(x%128) + ywidth] = new Vector3(_x, _y, point);
}
}
for(int i = 0; i < verts.Length; i++)
{
Mesh m = _Meshes;
_m.vertices = verts;*

// calculate / assign triangles here

}

Another way would be to iterate the pixels column by column. That way you can process one mesh at a time:
for (int i = 0; i < 4; i++)
{
Vector3[] verts = new Vector3[128*height ];
for(int _x = 0; _x < 128; _x++)
{
for(int _y = 0; _y < height; _y++)
{
int index = (y * width) + x + 128i;
float point = (float)_data[index];
verts[_x + _ywidth] = new Vector3(x + 128i, y, point);
}*

}

Meshes*.vertices = verts;*
// assign triangles for the mesh here
}_

I solved it.

First of all - instead of having columns of mesh, I made rows instead - as that seems like a more easier way.

As mentioned before, each of my rows of mesh has a Script attached to it, with a function called ‘UpdateMesh’ which is called every frame, and has the entire ImageArray passed to it.

``````public void UpdateMesh(double[] _data){

for(int _y = 0; _y < height; _y++){

for(int _x = 0; _x < width; _x++){

int index = (_y * width) + _x;

int point_index = (height * width) * chunkIndex;
double point = _data[ index + point_index ];
points[index] = new Vector3(points[index].x, points[index].y, (float)point);

}

}

_Mesh.vertices = points;

}
``````

Where ‘width’ is the width of the mesh, where in this case it’s now the entire width of the image too which is 512 (as the mesh is now Row by Row, not Col by Col).

And ‘height’ is the height of the mesh which is 106 (424 / 4 of the entire image).

And ‘chuckIndex’ is the index given to each mesh in order (0 to 3);