
using System;

namespace Math2D
{
	
	
	public class Matrix2D
	{
		private float[,] FMatrix = new float[3,3];
		public Matrix2D()
		{
			
		}
		
		public float[,] M {
			get { return FMatrix; }			
		}
		
		public Matrix2D(float m11, float m12, float m21, float m22)
		{
			InitIdent();
			FMatrix[0,0] = m11;
			FMatrix[0,1] = m12;
			FMatrix[1,0] = m21;
			FMatrix[1,1] = m22;
		}
		
		public Matrix2D(float[] m) {
			for (int i=0; i<3; i++) 
				for (int j=0; j<3; j++)
					FMatrix[i,j] = m[i*3+j];
		}
		
		public void InitIdent() {
			for (int i=0; i<3; i++) 
				for (int j=0; j<3; j++)
					FMatrix[i,j] = i==j ? 1.0f : 0.0f;
		}
		
		public void Clear() {
			for (int i=0; i<3; i++) 
				for (int j=0; j<3; j++)
					FMatrix[i,j] = 0.0f;
		}
		
		public static Matrix2D operator+  (Matrix2D a, float b) {
			Matrix2D res = new Matrix2D();
			for (int i=0; i<3; i++) 
				for (int j=0; j<3; j++)
					res.FMatrix[i,j] = 
						a.FMatrix[i,j] + b;
			return res;
		}
		
		public static Matrix2D operator+  (Matrix2D a, Matrix2D b) {
			Matrix2D res = new Matrix2D();
			for (int i=0; i<3; i++) 
				for (int j=0; j<3; j++)
					res.FMatrix[i,j] = 
						a.FMatrix[i,j] + b.FMatrix[i,j];
			return res;
		}
		
		public static Matrix2D operator*  (Matrix2D a, float b) {
			Matrix2D res = new Matrix2D();
			for (int i=0; i<3; i++) 
				for (int j=0; j<3; j++)
					res.FMatrix[i,j] = 
						a.FMatrix[i,j] * b;
			return res;
		}
		
		public static Matrix2D operator*  (Matrix2D a, Matrix2D b) {
			Matrix2D res = new Matrix2D();
			for (int i=0; i<3; i++) 
				for (int j=0; j<3; j++)
					res.FMatrix[i,j] = 
						a.FMatrix[i,0] * b.FMatrix[0,j] + 
						a.FMatrix[i,1] * b.FMatrix[1,j] + 
						a.FMatrix[i,2] * b.FMatrix[2,j];
			return res;
		}
		
		public static Matrix2D GetInitIdent() {
			Matrix2D m = new Matrix2D();
			m.InitIdent();
			return m;
		}
		
		public static Matrix2D GetRotate(float angle) {			
			float s = (float)Math.Sin(angle);
			float c = (float)Math.Cos(angle);			
			return new Matrix2D(c, s, -s, c);
		}
		
		public static Matrix2D GetTranslate(float x, float y) {						
			return new Matrix2D(new float[]{
				1, 0, 0,
				0, 1, 0,
				x, y, 1});
		}
		
		public static Matrix2D GetScale(float x, float y) {						
			return new Matrix2D(new float[]{
				x, 0, 0,
				0, y, 0,
				0, 0, 1});
		}
	}
}
