/* 
	Grid.java

	[ more java @ mlab - 10-17.1.2000 - juhuu@katastro.fi ]

*/

import java.awt.*;

public class Grid
{
	// grid size
	private	int		nGrid_width;
	private	int		nGrid_height;

	private int		nCoord_amount;
	private float[]		n3d_coords;
	private int[]		n2d_coords;
	private double		dCount1, dCount2;

	public Matrix3D		mat;

	public Grid(int x, int y)
	{
		nGrid_width	= x;
		nGrid_height	= y;
	
		nCoord_amount	= x * y;

		dCount1		= 0;
		dCount2		= 0;

		mat		= new Matrix3D();
		mat.xrot(20);
		mat.yrot(30);

		n3d_coords	= new float[nCoord_amount * 3];
		n2d_coords	= new int[nCoord_amount * 3];

		for (int i = 0 ; i < nGrid_width ; i++)
		{
			for (int j = 0 ; j < nGrid_height ; j++)
			{
				n3d_coords[i*3*nGrid_height + 3*j]	= (float) (i - nGrid_width / 2) * 40.0f;
				n3d_coords[i*3*nGrid_height + 3*j + 1]	= (float) 50.0f * (float) Math.random();
				n3d_coords[i*3*nGrid_height + 3*j + 2]	= (float) (j - nGrid_height / 2)* 40.0f;
			}
		}
	}

	public void draw(Graphics gDrawItHere)
	{
		dCount1 = dCount1 + 1;
		dCount2 = dCount2 + 2;

		// do the sinewave
		for (int i = 0 ; i < nGrid_width ; i++)
		{
			for (int j = 0 ; j < nGrid_height ; j++)
			{
				n3d_coords[i*3*nGrid_height + 3*j + 1]	= (float) 40.0f * ((float) Math.sin((dCount1 + (double) i * 30) / 100) + (float) Math.cos((dCount2 + (double) j * 40) / 100));
			}
		}

		// transform from 3d to 2d
		mat.transform(n3d_coords, n2d_coords, nCoord_amount);		

		// draw vertical lines
		for (int i = 0 ; i < (nGrid_width - 1) ; i++)
		{
			for (int j = 0 ; j < nGrid_height ; j++)
			{
				gDrawItHere.drawLine(n2d_coords[i*3*nGrid_height + 3*j],
						     n2d_coords[i*3*nGrid_height + 3*j + 1],
						     n2d_coords[(i+1)*3*nGrid_height + 3*j],
						     n2d_coords[(i+1)*3*nGrid_height + 3*j + 1]);
			}
		}

		// draw horizontal lines
		for (int i = 0 ; i < nGrid_width ; i++)
		{
			for (int j = 0 ; j < (nGrid_height - 1) ; j++)
			{
				gDrawItHere.drawLine(n2d_coords[i*3*nGrid_height + 3*j],
						     n2d_coords[i*3*nGrid_height + 3*j + 1],
						     n2d_coords[i*3*nGrid_height + 3*(j+1)],
						     n2d_coords[i*3*nGrid_height + 3*(j+1) + 1]);
			}
		}

	}
}
