Display a 3-D array as 2-D

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 1378 Views - Last Post: 13 August 2010 - 11:24 AM

#1 monkadelicd  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 08-July 10

Display a 3-D array as 2-D

Posted 07 August 2010 - 07:15 PM

I have an array with three dimensions. There are 10 elements in each dimension so I've got a 10x10x10 cube. How can I combine all the elements from one dimension into one layer so I can display the whole cube in a 2-D view?

This post has been edited by monkadelicd: 07 August 2010 - 07:16 PM

Is This A Good Question/Topic? 0
  • +

Replies To: Display a 3-D array as 2-D

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3103
  • View blog
  • Posts: 10,899
  • Joined: 08-August 08

Re: Display a 3-D array as 2-D

Posted 07 August 2010 - 07:29 PM

You can make one of the dimensions part of one of the others.
pseudo-code:
for(x= 0; x < max_x; x++)
{
  for(y=0; y < max_y; y++)
  {
    for(z=0; z < max_z; z++)
    {
     //output array value;
    }
  }
// output a line return
} 

Was This Post Helpful? 1
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4437
  • View blog
  • Posts: 12,308
  • Joined: 18-April 07

Re: Display a 3-D array as 2-D

Posted 07 August 2010 - 07:40 PM

Think of your cube as a stack of paper. When you draw an x in the upper left corner of a sheet and it bleeds through to the sheets below it, they are located in the same upper left corner, but one sheet down. Now with each sheet marked in the upper left, take each sheet and stack them end to end and what do you see? If your upper left corner was 0,0 you would see the mark would have bled through to now 10,0... 20, 0... 30,0 etc.

This is essentially unfolding the cube. Your 10x10x10 would then unfold to a 2D array that is 10x10 stacked end to end 10 times... or a 100x10 2D array. Where each X value in the first 10x10 corresponds to (X + (10 * 3D index)). Thus if I have a mark in 2,3 then its next 3D dimension value would be 22,3, third level is 32,3... 42,3 etc.

Hope you get what I am saying here. :)
Was This Post Helpful? 1
  • +
  • -

#4 monkadelicd  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 08-July 10

Re: Display a 3-D array as 2-D

Posted 07 August 2010 - 08:13 PM

So I before I read the responses to my original question I started trying to write some code. What I thought might work doesn't exactly.

I created the original 3-D array and two 2-D arrays (one for a "front" view and one for a "side" view). Here's the code I tried:
var row, col, layer;
var space = new Array()
	for (layer = 0; layer <= 9; layer++)
	{	space[layer] = new Array();
		for(col = 0; col <= 9; col++)
		{	space[layer][col] = new Array();
			for(row = 0; row <= 9; row++)
			{	space[layer][col][row] = " ....";
			}
		}
	}

var yzspace = new Array()
	for (col = 0; col <= 9; col++)
	{	yzspace[col] = new Array();
		for (row = 0; row <= 9; row++)
		{	yzspace[col][row] = " ....";
		}
	}

var xzspace = new Array()
	for (layer = 0; layer <= 9; layer++)
	{	xzspace[layer] = new Array();
		for (row = 0; row <= 9; row++)
		{	xzspace[layer][row] = " ....";
		}
	}

space[1][3][4] = "&nbsp;&nbsp;E&nbsp;";
space[7][9][7] = "&nbsp;&nbsp;K&nbsp;";
space[1][3][9] = "&nbsp;&nbsp;S&nbsp;";
space[4][2][7] = "&nbsp;&nbsp;S&nbsp;";
space[2][7][3] = "&nbsp;&nbsp;S&nbsp;";
space[8][5][6] = "&nbsp;&nbsp;S&nbsp;";
space[7][2][5] = "&nbsp;&nbsp;S&nbsp;";


for (layer = 0; layer <= 9; layer++)
{	for (col = 0; col <= 9; col++)
	{	for (row = 0; row <= 9; row++)
		{	if (space[layer][col][row] != " ....")
			{	space[layer][col][row] = yzspace[col][row];
				space[layer][col][row] = xzspace[layer][row];
			}
		}
	}
}



I was thinking that the last block would write and overwrite the secondary arrays with the contents of each layer from the original array. But writing the contents of the secondary arrays shows all elements are " ...." and no E, K, or S.

I think the pseudo code CTphpnwb wrote would basically give me the unfolded 3-D array that Martyr2 is speaking of. I'll try doing it that way if the way I'm trying isn't possible.
Was This Post Helpful? 0
  • +
  • -

#5 JMRKER  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 130
  • View blog
  • Posts: 838
  • Joined: 25-October 08

Re: Display a 3-D array as 2-D

Posted 07 August 2010 - 08:44 PM

Sort of a brute force way to demonstrate this concept:
<html>
<head>
<title>3 dimension arrays</title>
<script type="text/javascript">

var Z = new Array(10);
// initialize 3 dimension array
for (var z=0; z<10; z++) {
  Z[z] = new Array(10);
  for (var y=0; y<10; y++) {
    Z[z][y] = new Array(10);
    for (var x=0; x<10; x++) {
      Z[z][y][x] = 'Z'+z+'Y'+y+'X'+x;
    }
  }
}      

function DisplayZ(zndx) {
//  alert(Z[zndx].join('\n'));
  var str = '';
  str += '<table border="1"><tr>';
  for (var y=0; y<10; y++) {
	for (var x=0; x<10; x++) {
	  str += '<td>'+Z[zndx][y][x]+'</td>';
	}
	str += '</tr><tr>';
  }
  str += '</tr></table>';
  return str;
}

function DisplayY(yndx) {
  var str = '';
  str += '<table border="1"><tr>';
  for (var z=0; z<10; z++) {
	for (var x=0; x<10; x++) {
	  str += '<td>'+Z[z][yndx][x]+'</td>';
	}
	str += '</tr><tr>';
  }
  str += '</tr></table>';
  return str;
}

function DisplayX(xndx) {
  var str = '';
  str += '<table border="1"><tr>';
  for (var z=0; z<10; z++) {
	for (var y=0; y<10; y++) {
	  str += '<td>'+Z[z][y][xndx]+'</td>';
	}
	str += '</tr><tr>';
  }
  str += '</tr></table>';
  return str;
}
</script>
</head>
<body>
<button onclick="document.getElementById('ArrayInfo').innerHTML=DisplayZ(0)">Z0</button>
<button onclick="document.getElementById('ArrayInfo').innerHTML=DisplayZ(1)">Z1</button>
<button onclick="document.getElementById('ArrayInfo').innerHTML=DisplayZ(2)">Z2</button>
<button onclick="document.getElementById('ArrayInfo').innerHTML=DisplayZ(3)">Z3</button>
<button onclick="document.getElementById('ArrayInfo').innerHTML=DisplayZ(4)">Z4</button>
<button onclick="document.getElementById('ArrayInfo').innerHTML=DisplayZ(5)">Z5</button>
<button onclick="document.getElementById('ArrayInfo').innerHTML=DisplayZ(6)">Z6</button>
<button onclick="document.getElementById('ArrayInfo').innerHTML=DisplayZ(7)">Z7</button>
<button onclick="document.getElementById('ArrayInfo').innerHTML=DisplayZ(8)">Z8</button>
<button onclick="document.getElementById('ArrayInfo').innerHTML=DisplayZ(9)">Z9</button>
<br>
<button onclick="document.getElementById('ArrayInfo').innerHTML=DisplayY(0)">Y0</button>
<button onclick="document.getElementById('ArrayInfo').innerHTML=DisplayY(1)">Y1</button>
<button onclick="document.getElementById('ArrayInfo').innerHTML=DisplayY(2)">Y2</button>
<button onclick="document.getElementById('ArrayInfo').innerHTML=DisplayY(3)">Y3</button>
<button onclick="document.getElementById('ArrayInfo').innerHTML=DisplayY(4)">Y4</button>
<button onclick="document.getElementById('ArrayInfo').innerHTML=DisplayY(5)">Y5</button>
<button onclick="document.getElementById('ArrayInfo').innerHTML=DisplayY(6)">Y6</button>
<button onclick="document.getElementById('ArrayInfo').innerHTML=DisplayY(7)">Y7</button>
<button onclick="document.getElementById('ArrayInfo').innerHTML=DisplayY(8)">Y8</button>
<button onclick="document.getElementById('ArrayInfo').innerHTML=DisplayY(9)">Y9</button>
<br>
<button onclick="document.getElementById('ArrayInfo').innerHTML=DisplayX(0)">X0</button>
<button onclick="document.getElementById('ArrayInfo').innerHTML=DisplayX(1)">X1</button>
<button onclick="document.getElementById('ArrayInfo').innerHTML=DisplayX(2)">X2</button>
<button onclick="document.getElementById('ArrayInfo').innerHTML=DisplayX(3)">X3</button>
<button onclick="document.getElementById('ArrayInfo').innerHTML=DisplayX(4)">X4</button>
<button onclick="document.getElementById('ArrayInfo').innerHTML=DisplayX(5)">X5</button>
<button onclick="document.getElementById('ArrayInfo').innerHTML=DisplayX(6)">X6</button>
<button onclick="document.getElementById('ArrayInfo').innerHTML=DisplayX(7)">X7</button>
<button onclick="document.getElementById('ArrayInfo').innerHTML=DisplayX(8)">X8</button>
<button onclick="document.getElementById('ArrayInfo').innerHTML=DisplayX(9)">X9</button>
<br>
<div id="ArrayInfo"></div>
</body>
</html>


Was This Post Helpful? 0
  • +
  • -

#6 monkadelicd  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 08-July 10

Re: Display a 3-D array as 2-D

Posted 08 August 2010 - 01:05 PM

Ok JMRKER. I see what you are showing me but what I'd like to do is squish the 3rd dimension into a flat space. The end result of squishing a 10x10x10 cube would be a 10x10x1 plane but the information from the many z planes is represented in just one plane instead of only showing one plane of information at a time.

In the example I'm working with i have many empty elements in the array. Flattening one dimension won't overlap any information.

Imagine a two checker boards stacked on top of each other so the red and black squares are opposite. If these two were combined all squares would be either black or red.

Can anyone explain why my attempt posted above doesn't give the desired result?

In line 38-47 I assumed looping through all 3 dimensions and writing all the elements to just 2 dimensions would have done the trick. Why is the result an empty array if I'm only writing when the original is not " ...."


Edit - Ok I figured out why my method wasn't working. Still learning to find my beginner mistakes. I had line 42 and 43 backwards. I reversed the and now I have a flattened version of my 3d array.

This post has been edited by monkadelicd: 08 August 2010 - 01:37 PM

Was This Post Helpful? 0
  • +
  • -

#7 monkadelicd  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 08-July 10

Re: Display a 3-D array as 2-D

Posted 08 August 2010 - 03:01 PM

I've got everything displaying nicely in a table with enumeration on left and bottom sides for coordinates.

Now all have to do is part 2 of the exercise; plotting a course from E to K.

Exercise:
Create a three-dimensional array which represents a section of the galaxy. Your array should have 10 rows, 10 columns and 10 layers. The star ship Enterprise is at position (1, 3, 4), a Klingon battleship at (7, 9, 7) and stars at (1, 3, 9), (4, 2, 7), (2, 7, 3), (8, 5, 6) and (7, 2, 5).

1. How would you display this information on the screen (which can only represent two dimensions)?

2. Add lines to your program which allows the captain of the Enterprise to plot a course to intercept the Klingons, and which informs him if the course will take him through a star.

<html>
<head>
<title>A Section of Sci-Fi Space</title>
</head>
<body>
<script type="text/javascript">
<!--
var row, col, layer;
var space = new Array()
	for (layer = 0; layer <= 9; layer++)
	{	space[layer] = new Array();
		for(col = 0; col <= 9; col++)
		{	space[layer][col] = new Array();
			for(row = 0; row <= 9; row++)
			{	space[layer][col][row] = "&nbsp;";
			}
		}
	}

var yzSpace = new Array()
	for (col = 0; col <= 9; col++)
	{	yzSpace[col] = new Array();
		for (row = 0; row <= 9; row++)
		{	yzSpace[col][row] = "&nbsp;";
		}
	}

var xzSpace = new Array()
	for (layer = 0; layer <= 9; layer++)
	{	xzSpace[layer] = new Array();
		for (row = 0; row <= 9; row++)
		{	xzSpace[layer][row] = "&nbsp;";
		}
	}

space[1][3][4] = "E";
space[7][9][7] = "K";
space[1][3][9] = "S";
space[4][2][7] = "S";
space[2][7][3] = "S";
space[8][5][6] = "S";
space[7][2][5] = "S";


for (layer = 0; layer <= 9; layer++)
{	for (col = 0; col <= 9; col++)
	{	for (row = 0; row <= 9; row++)
		{	
			if (space[layer][col][row] != "&nbsp;")
			{	yzSpace[col][row] = space[layer][col][row];
				xzSpace[layer][row] = space[layer][col][row];
			}
		}
	}
}

var strYZ = " ";
strYZ += "<table border=\"1\" width=\"250\"><tr><th colspan=\"11\">Z and Y Axis (Front View)</th></tr><tr>";
for (row = 9; row >= -1; row--)
{	if (row >= 0)
	{	strYZ += "<th>" + row + "</th>"
		for (col = 0; col <= 9; col++)
		{	strYZ += "<td>" + yzSpace[col][row] + "</td>";
		}
	}
	else
	{	strYZ += "<th>&nbsp;</th>";
		for (col = 0; col <= 9; col++)
		{	strYZ += "<th>" + col + "</th>";
		}
	}
	strYZ += "</tr><tr>";
}
strYZ += "</tr></table>";

document.write(strYZ);

document.write("<br />");

var strXZ = " ";
strXZ += "<table border=\"1\" width=\"250\"><tr><th colspan=\"11\">X and Z Axis (Side View)</tr><tr>";
for (row = 9; row >= -1; row--)
{	if (row >= 0)
	{	strXZ += "<th>" + row + "</th>";
		for (layer = 0; layer <= 9; layer++)
		{	strXZ += "<td>" + xzSpace[layer][row] + "</td>";
		}
	}
	else
	{	strXZ += "<th>&nbsp;</th>";
		for (layer = 0; layer <= 9; layer++)
		{	strXZ += "<th>" + layer + "</th>";
		}
	}
	strXZ += "</tr><tr>";
}
strXZ += "</tr></table>";

document.write(strXZ);


//-->
</script>
</body>
</html>


Was This Post Helpful? 0
  • +
  • -

#8 JMRKER  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 130
  • View blog
  • Posts: 838
  • Joined: 25-October 08

Re: Display a 3-D array as 2-D

Posted 08 August 2010 - 03:47 PM

Looks like you've been busy while I was away.

To answer your question, even if you don't care anymore ...

View Postmonkadelicd, on 08 August 2010 - 12:05 PM, said:

Ok JMRKER. I see what you are showing me but what I'd like to do is squish the 3rd dimension into a flat space. The end result of squishing a 10x10x10 cube would be a 10x10x1 plane but the information from the many z planes is represented in just one plane instead of only showing one plane of information at a time.

...

The buttons represented the planes of the 3 axes.
Pressing any of the X, Y, or Z buttons showed the
contents of that particular plane, which I had initialize
with the [z][y][x] positions of the array.
The display was the "squished" cube, showing a particular plane

The contents of each 3-D array could be words, dots, stars, klingons, etc.
Was This Post Helpful? 0
  • +
  • -

#9 monkadelicd  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 08-July 10

Re: Display a 3-D array as 2-D

Posted 09 August 2010 - 05:39 PM

Maybe this should be in a new thread but I've gotta get this done somehow:

2. Add lines to your program which allows the captain of the Enterprise to plot a course to intercept the Klingons, and which informs him if the course will take him through a star.

In the lessons I'm studying I don't think anything has been covered to accomplish this. I'm drawing a blank. Any hints would be much appreciated. I can see by the two 2-D displays output by the script that no stars are directly in line. How can I get Javascript to test for this?

In the script:
E = Enterprise
K = Klingons
S = Star

This post has been edited by monkadelicd: 09 August 2010 - 05:41 PM

Was This Post Helpful? 0
  • +
  • -

#10 JMRKER  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 130
  • View blog
  • Posts: 838
  • Joined: 25-October 08

Re: Display a 3-D array as 2-D

Posted 09 August 2010 - 06:00 PM

Assuming you know the Enterprise position E[x][y][z]
and you know where you wish to intercept K[x][y][z]
and you have populated various elements with an 'S',
you can increment/decrement the Enterprise positions
until they match the Klingon position, checking each
move position to determine if a star is in the way.

You could add logic to determine which x,y,z position
is most away and increment/decrement that element more than
one standard move increment/decrement value.

Have fun with your game project.
:bananaman:
Was This Post Helpful? 1
  • +
  • -

#11 monkadelicd  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 08-July 10

Re: Display a 3-D array as 2-D

Posted 11 August 2010 - 02:29 PM

Alright I've been working on the logic to plot the path of the Enterprise.

I keep getting an error of Error: space[eZ][eY] is undefined Line: 110

This code worked fine when my logic was a little different. I changed the course plot logic and this line gives me an error.

I can't figure this out. Any ideas?

<html>
<head>
<title>A Section of Sci-Fi Space</title>
</head>
<body>
<script type="text/javascript">
<!--
var starInPath;
var distanceZ, distanceY, distanceZ;
var eZ = 1;
var eY = 3;
var eX = 4;
var kZ = 7;
var kY = 9;
var kX = 7;
var row, col, layer;
var space = new Array()
	for (layer = 0; layer <= 9; layer++)
	{	space[layer] = new Array();
		for(col = 0; col <= 9; col++)
		{	space[layer][col] = new Array();
			for(row = 0; row <= 9; row++)
			{	space[layer][col][row] = "&nbsp;";
			}
		}
	}

var xySpace = new Array()
	for (col = 0; col <= 9; col++)
	{	xySpace[col] = new Array();
		for (row = 0; row <= 9; row++)
		{	xySpace[col][row] = "&nbsp;";
		}
	}

var yzSpace = new Array()
	for (layer = 0; layer <= 9; layer++)
	{	yzSpace[layer] = new Array();
		for (col = 0; col <= 9; col++)
		{	yzSpace[layer][col] = "&nbsp;";
		}
	}

space[1][3][4] = "E";
space[7][9][7] = "K";
space[1][3][9] = "S";
space[4][2][7] = "S";
space[2][7][3] = "S";
space[8][5][6] = "S";
space[7][2][5] = "S";

for (layer = 9; layer >= 0; layer--)
{	for (col = 9; col >= 0; col--)
	{	for (row = 9; row >= 0; row--)
		{	
			if (space[layer][col][row] != "&nbsp;")
			{	xySpace[col][row] = space[layer][col][row];
				yzSpace[layer][col] = space[layer][col][row];
			}
		}
	}
}

do
{	document.write("The Enterprise is at position " + eX + ", " + eY + ", " + eZ + ".<br />");

	distanceZ = kZ - eZ;
	distanceY = kY - eY;
	distanceX = kX - eX;

	if ((distanceX > distanceY) && (distanceX > distanceZ))
	{	eX++;
	}
	else
	{	if ((distanceY > distanceX) && (distanceY > distanceZ))
		{	eY++;
		}
		else
		{	if ((distanceZ > distanceX) && (distanceZ > distanceY))
			{	eZ++;
			}
			else
			{	if ((distanceX == distanceY) && (distanceX > distanceZ))
				{	eX++;
					eY++;
				}
				else
				{	if ((distanceX == distanceZ) && (distanceX > distanceY))
					{	eX++;
						eZ++;
					}
					else
					{	if ((distanceY == distanceZ) && (distanceY > distanceX))
						{	eY++;
							eZ++;
						}
						else
						{	if ((distanceZ == distanceY) && (distanceZ == distanceX))
							{	eZ++;
								eY++;
								eX++;
							}
						}
					}
				}
			}
		}
	}		

	if (space[eZ][eY][eX] == "S")
	{	starInPath = "true";
	}
	else
	{	starInPath = "false";
	}
}
while (distanceZ > 0 && distanceY > 0 && distanceX > 0);

if (starInPath == "true")
{	document.write("A direct course to the Klingons will lead the Enterprise through a star.<br /><br />");
}
else
{	document.write("The Enterprise has a clear path to the Klingons.<br /><br />");
}

var strXY = " ";
strXY += "<table border=\"1\" width=\"250\"><tr><th colspan=\"11\">X and Y Axis (Front View)</th></tr><tr>";
for (col = 9; col >= -1; col--)
{	if (col >= 0)
	{	strXY += "<th>" + col + "</th>"
		for (row = 0; row <= 9; row++)
		{	strXY += "<td>" + xySpace[col][row] + "</td>";
		}
	}
	else
	{	strXY += "<th>&nbsp;</th>";
		for (row = 0; row <= 9; row++)
		{	strXY += "<th>" + row + "</th>";
		}
	}
	strXY += "</tr><tr>";
}
strXY += "</tr></table>";

document.write(strXY);

document.write("<br />");

var strYZ = " ";
strYZ += "<table border=\"1\" width=\"250\"><tr><th colspan=\"11\">Y and Z Axis (Side View)</tr><tr>";
for (col = 9; col >= -1; col--)
{	if (col >= 0)
	{	strYZ += "<th>" + col + "</th>";
		for (layer = 0; layer <= 9; layer++)
		{	strYZ += "<td>" + yzSpace[layer][col] + "</td>";
		}
	}
	else
	{	strYZ += "<th>&nbsp;</th>";
		for (layer = 0; layer <= 9; layer++)
		{	strYZ += "<th>" + layer + "</th>";
		}
	}
	strYZ += "</tr><tr>";
}
strYZ += "</tr></table>";

document.write(strYZ);


//-->
</script>
</body>
</html>


Was This Post Helpful? 0
  • +
  • -

#12 JMRKER  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 130
  • View blog
  • Posts: 838
  • Joined: 25-October 08

Re: Display a 3-D array as 2-D

Posted 11 August 2010 - 03:30 PM

The problem is that you let one of your E array references
become larger than 9.

Add this as a test to see the problem just before line 110
    }  
    alert(eZ+','+eY+','+eX);
    if (space[eZ][eY][eX] == "S")


:gun_bandana:
Was This Post Helpful? 1
  • +
  • -

#13 monkadelicd  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 08-July 10

Re: Display a 3-D array as 2-D

Posted 11 August 2010 - 08:13 PM

I changed the do...while loop to a while loop and moved that problem block to the top of the loop before all the incrementing. Now all is good and it works. I can finally move on to the next exercise!!

Edit: Or so I thought. I tried putting a star at the one of the coordinates that the enterprise travels through. I still get the message that the course is clear though. Any ideas?

<html>
<head>
<title>A Section of Sci-Fi Space</title>
</head>
<body>
<script type="text/javascript">
<!--
var starInPath;
var distanceZ = 10;
var distanceY = 10;
var distanceX = 10;
var eZ = 1;
var eY = 3;
var eX = 4;
var kZ = 7;
var kY = 9;
var kX = 7;
var row, col, layer;
var space = new Array()
	for (layer = 0; layer <= 9; layer++)
	{	space[layer] = new Array();
		for(col = 0; col <= 9; col++)
		{	space[layer][col] = new Array();
			for(row = 0; row <= 9; row++)
			{	space[layer][col][row] = "&nbsp;";
			}
		}
	}

var xySpace = new Array()
	for (col = 0; col <= 9; col++)
	{	xySpace[col] = new Array();
		for (row = 0; row <= 9; row++)
		{	xySpace[col][row] = "&nbsp;";
		}
	}

var yzSpace = new Array()
	for (layer = 0; layer <= 9; layer++)
	{	yzSpace[layer] = new Array();
		for (col = 0; col <= 9; col++)
		{	yzSpace[layer][col] = "&nbsp;";
		}
	}

space[1][3][4] = "E";
space[7][9][7] = "K";
space[1][3][9] = "S";
space[4][2][7] = "S";
space[2][7][3] = "S";
space[8][5][6] = "S";
space[7][2][5] = "S";
space[5][7][5] = "S";

for (layer = 9; layer >= 0; layer--)
{	for (col = 9; col >= 0; col--)
	{	for (row = 9; row >= 0; row--)
		{	
			if (space[layer][col][row] != "&nbsp;")
			{	xySpace[col][row] = space[layer][col][row];
				yzSpace[layer][col] = space[layer][col][row];
			}
		}
	}
}


while (distanceZ > 0 && distanceY > 0 && distanceX > 0)
{	document.write("The Enterprise is at position " + eX + ", " + eY + ", " + eZ + ".<br />");

	if (space[eZ][eY][eX] == "S")
	{	starInPath = "true";
	}
	else
	{	starInPath = "false";
	}

	distanceZ = kZ - eZ;
	distanceY = kY - eY;
	distanceX = kX - eX;

	if ((distanceX > distanceY) && (distanceX > distanceZ))
	{	eX++;
	}
	else
	{	if ((distanceY > distanceX) && (distanceY > distanceZ))
		{	eY++;
		}
		else
		{	if ((distanceZ > distanceX) && (distanceZ > distanceY))
			{	eZ++;
			}
			else
			{	if ((distanceX == distanceY) && (distanceX > distanceZ))
				{	eX++;
					eY++;
				}
				else
				{	if ((distanceX == distanceZ) && (distanceX > distanceY))
					{	eX++;
						eZ++;
					}
					else
					{	if ((distanceY == distanceZ) && (distanceY > distanceX))
						{	eY++;
							eZ++;
						}
						else
						{	if ((distanceZ == distanceY) && (distanceZ == distanceX))
							{	eZ++;
								eY++;
								eX++;
							}
						}
					}
				}
			}
		}
	}
}

if (starInPath == "true")
{	document.write("A direct course to the Klingons will lead the Enterprise through a star.<br /><br />");
}
else
{	document.write("The Enterprise has a clear path to the Klingons.<br /><br />");
}

var strXY = " ";
strXY += "<table border=\"1\" width=\"250\"><tr><th colspan=\"11\">X and Y Axis (Front View)</th></tr><tr>";
for (col = 9; col >= -1; col--)
{	if (col >= 0)
	{	strXY += "<th>" + col + "</th>"
		for (row = 0; row <= 9; row++)
		{	strXY += "<td>" + xySpace[col][row] + "</td>";
		}
	}
	else
	{	strXY += "<th>&nbsp;</th>";
		for (row = 0; row <= 9; row++)
		{	strXY += "<th>" + row + "</th>";
		}
	}
	strXY += "</tr><tr>";
}
strXY += "</tr></table>";

document.write(strXY);

document.write("<br />");

var strYZ = " ";
strYZ += "<table border=\"1\" width=\"250\"><tr><th colspan=\"11\">Y and Z Axis (Side View)</tr><tr>";
for (col = 9; col >= -1; col--)
{	if (col >= 0)
	{	strYZ += "<th>" + col + "</th>";
		for (layer = 0; layer <= 9; layer++)
		{	strYZ += "<td>" + yzSpace[layer][col] + "</td>";
		}
	}
	else
	{	strYZ += "<th>&nbsp;</th>";
		for (layer = 0; layer <= 9; layer++)
		{	strYZ += "<th>" + layer + "</th>";
		}
	}
	strYZ += "</tr><tr>";
}
strYZ += "</tr></table>";

document.write(strYZ);


//-->
</script>
</body>
</html>


This post has been edited by monkadelicd: 11 August 2010 - 08:34 PM

Was This Post Helpful? 0
  • +
  • -

#14 JMRKER  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 130
  • View blog
  • Posts: 838
  • Joined: 25-October 08

Re: Display a 3-D array as 2-D

Posted 12 August 2010 - 07:46 AM

Put a temporary alert into your logic to make sure the 'S' element of the array is being passed through.
071	    if (space[eZ][eY][eX] == "S")
072	    {   starInPath = "true";
073	    }
074	    else
075	    {   starInPath = "false";
076	    }
alert('Star in path = '+starInPath+': '+eZ+','+eY+','+eX+' :'+space[eZ][eY][eX]);
077	 
078	    distanceZ = kZ - eZ;
079	    distanceY = kY - eY;
080	    distanceX = kX - eX;


Was This Post Helpful? 1
  • +
  • -

#15 monkadelicd  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 08-July 10

Re: Display a 3-D array as 2-D

Posted 12 August 2010 - 11:21 AM

I took out the else statement and it works fine. The statement doesn't get written until the loop is done so if a star wasn't present in the last run of the loop starInPath was set back to false. Now once starInPath == "true" it doesn't change.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2