import java.awt.*;
import java.util.*;
import java.lang.Math;

public class Matrix2D
{
	private static double[][] MatrixTemp = new double[3][3];
	public static void Translate(double Matrix[][], double a, double b)
		//this method will translate a single point stored in a 3X3 matrix
		//translates point (x, y) to (x+a, y+b)
	{
		IdentityMatrix(MatrixTemp);
		MatrixTemp[0][2] = a;
		MatrixTemp[1][2] = b;
		MultiplyMatrix(Matrix, MatrixTemp);	
	}
	
	public static void Scale(double Matrix[][], double a, double b)
		//this method will scale a single point stored in a 3X3 matrix
		//scales point (x, y) to (a*x, b*y)
	{
		IdentityMatrix(MatrixTemp);
		MatrixTemp[0][0] = a;
		MatrixTemp[1][1] = b;		
		MultiplyMatrix(Matrix, MatrixTemp);	
	}
	
	public static void Rotate(double Matrix[][], double theta)
		//this method will rotate a single point about the origin
		//it will rotate it by theta, which is in radians
		//must put in a radians converter
	{
		IdentityMatrix(MatrixTemp);
		MatrixTemp[0][0] = Math.cos(theta);
		MatrixTemp[0][1] = -(Math.sin(theta));
		MatrixTemp[1][0] = Math.sin(theta);
		MatrixTemp[1][1] = Math.cos(theta);
		MultiplyMatrix(Matrix, MatrixTemp);	
	}
		
	public static void MultiplyMatrix(double MatrixA[][], double MatrixB[][])
		//this method will multiply two matrices together by doing
		//three dot products
	{
		double dTempMatrix[][] = new double[3][3];
		Copy(MatrixA, dTempMatrix);
		for (int i = 0; i < 3; i++)
		{
			for (int j = 0; j < 3; j++)
			{
				MatrixA[i][j] = MatrixB[i][0] * dTempMatrix[0][j] + 
								MatrixB[i][1] * dTempMatrix[1][j] +
								MatrixB[i][2] * dTempMatrix[2][j];
			}
		}
		
	}
	
	public static void Transform(double TransformationMatrix[][], double Coordinate[])
	//this method will take the matrix and coordinate vector passed in and multiply
	//the new transformed coordinate will be saved in the array Coordinate[]
	{
		double tempCoord[] = new double[3];
		for (int i=0; i<3; i++)
			tempCoord[i] = Coordinate[i];
		
		for (int j=0; j<3; j++)
		{			
			Coordinate[j] = (TransformationMatrix[j][0] * tempCoord[0] + 
							 TransformationMatrix[j][1] * tempCoord[1] + 
							 TransformationMatrix[j][2] * tempCoord[2]);
		}
	}
	
	public static void Copy(double Matrix[][], double dTempMatrix[][])
	//copies the values from one matrix to another temporary one
	//this method is assuming the matrices passed in are both
	//3x3 matrices, which is all we're dealing with in this assignment
	//For anything else, a more general method would have to be made
	{
		for (int i=0; i < 3; i++)
			for (int j=0; j < 3; j++)
				dTempMatrix[i][j] = Matrix[i][j];		
	}
	
	public static void IdentityMatrix(double Matrix[][])
	//makes any matrix passed into the identity matrix
	//this assumes the matrix is a square matrix
	{
		int len = Matrix.length;
		for (int i=0; i<len; i++)
			for (int j=0; j<len; j++)
			{
				if (i == j)
					Matrix[i][j] = 1;
				else
					Matrix[i][j] = 0;
			}
	}
		
}