⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 matrix3x3.java

📁 一个J2ME的3D 一个J2ME的3D 一个J2ME的3D
💻 JAVA
字号:
/**
 * 
 */
package Math;

/**
 * @author PanXu
 *
 */
public class Matrix3X3 {
	
	/**
	 * 
	 */
	public float M[][] = null;
	public Matrix3X3() {
		super();
		// TODO 自动生成构造函数存根
		M = new float [3][3];
	}
	public Matrix3X3(float m00 ,float m01 ,float m02 ,
			float m10 , float m11 , float m12 ,
			float m20 , float m21 , float m22) {
		super();
		// TODO 自动生成构造函数存根
		M = new float [3][3];
		M[0][0] = m00; M[0][1] = m01 ; M[0][2] = m02;
		M[1][0] = m10; M[1][1] = m11 ; M[1][2] = m12;
		M[2][0] = m20; M[2][1] = m21 ; M[2][2] = m22;
	}
	public void InitMat4X4(float m00 ,float m01 ,float m02 ,
			float m10 , float m11 , float m12 ,
			float m20 , float m21 , float m22) {
		M[0][0] = m00; M[0][1] = m01 ; M[0][2] = m02;
		M[1][0] = m10; M[1][1] = m11 ; M[1][2] = m12;
		M[2][0] = m20; M[2][1] = m21 ; M[2][2] = m22;
	}
	/*
	 * 生成特定矩阵
	 */
	//归零
	public void Set_Zero (){
		M[0][0] = 0; M[0][1] = 0 ; M[0][2] = 0;
		M[1][0] = 0; M[1][1] = 0 ; M[1][2] = 0;
		M[2][0] = 0; M[2][1] = 0 ; M[2][2] = 0;
	}
	//单位化
	public void IDENTITY (){
		M[0][0] = 1.0f; M[0][1] = 0 ; M[0][2] = 0;
		M[1][0] = 0; M[1][1] = 1.0f ; M[1][2] = 0;
		M[2][0] = 0; M[2][1] = 0 ; M[2][2] = 1.0f;
	}
	/**
	 * 平移矩阵
	 * @param x 偏移量
	 * @param y
	 */
	public void Set_MOVMAT (float x , float y)
	{
		M[0][0] = 1.0f; M[0][1] = 0 ; M[0][2] = 0;
		M[1][0] = 0; M[1][1] = 1.0f ; M[1][2] = 0;
		M[2][0] = x; M[2][1] = y ; M[2][2] = 1.0f;
	}
	/**
	 * 旋转矩阵
	 * @param r 旋转角度 
	 */
	public void Set_CIRMAT (int r)
	{
		float cos = MathTool .Cos [r];
		float sin = MathTool .Sin [r];
		this .InitMat4X4(cos , sin , 0 ,
				-sin , cos , 0 ,
				0 , 0 , 1.0f);
	}
	/**
	 * 缩放矩阵
	 * @param divw 缩放比例的倒数
	 */
	public void Set_ScaleMat (float divw)
	{
		M[0][0] = 1.0f; M[0][1] = 0 ; M[0][2] = 0;
		M[1][0] = 0; M[1][1] = 1.0f ; M[1][2] = 0;
		M[2][0] = 0; M[2][1] = 0 ; M[2][2] = divw;		
	}
	/*
	 * 运算
	 */
	//加
	public Matrix3X3 MAT3X3_ADD_Stack (Matrix3X3 a , Matrix3X3 b)
	{
		Matrix3X3 res = null;
		res .M[0][0] = a.M[0][0] + b .M[0][0];
		res .M[0][1] = a.M[0][1] + b .M[0][1];
		res .M[0][2] = a.M[0][2] + b .M[0][2];
		
		res .M[1][0] = a.M[1][0] + b .M[1][0];
		res .M[1][1] = a.M[1][1] + b .M[1][1];
		res .M[1][2] = a.M[1][2] + b .M[1][2];
		
		res .M[2][0] = a.M[2][0] + b .M[2][0];
		res .M[2][1] = a.M[2][1] + b .M[2][1];
		res .M[2][2] = a.M[2][2] + b .M[2][2];
		return res ;
	}
	public void MAT3X3_ADD (Matrix3X3 a , Matrix3X3 b)
	{
		this .M[0][0] = a.M[0][0] + b .M[0][0];
		this .M[0][1] = a.M[0][1] + b .M[0][1];
		this .M[0][2] = a.M[0][2] + b .M[0][2];
		
		this .M[1][0] = a.M[1][0] + b .M[1][0];
		this .M[1][1] = a.M[1][1] + b .M[1][1];
		this .M[1][2] = a.M[1][2] + b .M[1][2];
		
		this .M[2][0] = a.M[2][0] + b .M[2][0];
		this .M[2][1] = a.M[2][1] + b .M[2][1];
		this .M[2][2] = a.M[2][2] + b .M[2][2];
	}
	//减
	public Matrix3X3 MAT3X3_SUB_Stack (Matrix3X3 a , Matrix3X3 b)
	{
		Matrix3X3 res = null;
		res .M[0][0] = a.M[0][0] - b .M[0][0];
		res .M[0][1] = a.M[0][1] - b .M[0][1];
		res .M[0][2] = a.M[0][2] - b .M[0][2];
		
		res .M[1][0] = a.M[1][0] - b .M[1][0];
		res .M[1][1] = a.M[1][1] - b .M[1][1];
		res .M[1][2] = a.M[1][2] - b .M[1][2];
		
		res .M[2][0] = a.M[2][0] - b .M[2][0];
		res .M[2][1] = a.M[2][1] - b .M[2][1];
		res .M[2][2] = a.M[2][2] - b .M[2][2];
		return res ;
	}
	public void MAT3X3_SUB (Matrix3X3 a , Matrix3X3 b)
	{
		this .M[0][0] = a.M[0][0] - b .M[0][0];
		this .M[0][1] = a.M[0][1] - b .M[0][1];
		this .M[0][2] = a.M[0][2] - b .M[0][2];
		
		this .M[1][0] = a.M[1][0] - b .M[1][0];
		this .M[1][1] = a.M[1][1] - b .M[1][1];
		this .M[1][2] = a.M[1][2] - b .M[1][2];
		
		this .M[2][0] = a.M[2][0] - b .M[2][0];
		this .M[2][1] = a.M[2][1] - b .M[2][1];
		this .M[2][2] = a.M[2][2] - b .M[2][2];
	}
	//矩阵相乘
	public void MAT3X3_MUL (Matrix3X3 a , Matrix3X3 b)
	{
		int j = 0 , i = 0;
		for (i = 0 ; i < 3 ; i ++)
		{
			for (j = 0 ; j < 3 ; j ++)
			{
				this .M[i][j] = a .M[i][0] * b .M[0][j] + 
				a .M[i][1] * b .M[1][j] +
				a .M[i][2] * b .M[2][j];
			}//for 
		}//for 
	}
	
	public Matrix3X3 MAT3X3_MUL_Stack (Matrix3X3 a , Matrix3X3 b)
	{
		Matrix3X3 res = null;
		int j = 0 , i = 0;
		for (i = 0 ; i < 3 ; i ++)
		{
			for (j = 0 ; j < 3 ; j ++)
			{
				res .M[i][j] = a .M[i][0] * b .M[0][j] + 
				a .M[i][1] * b .M[1][j] +
				a .M[i][2] * b .M[2][j];
			}//for 
		}//for 
		return res ;
	}
	//求逆矩阵
	public Matrix3X3 MAT3x3_INVERSE()
	{
		Matrix3X3 res = null;
		
		float det_inv = 1.0f / 
		(this.M[0][0]*(this.M[1][1]*this.M[2][2] - this.M[2][1]*this.M[1][2]) -
				this.M[0][1]*(this.M[1][0]*this.M[2][2] - this.M[2][0]*this.M[1][2]) + 
				this.M[0][2]*(this.M[1][0]*this.M[2][1] - this.M[2][0]*this.M[1][1]));
		
		res.M[0][0] =  det_inv*(this.M[1][1]*this.M[2][2] - this.M[2][1]*this.M[1][2]);
		res.M[1][0] = -det_inv*(this.M[1][0]*this.M[2][2] - this.M[2][0]*this.M[1][2]);
		res.M[2][0] =  det_inv*(this.M[1][0]*this.M[2][1] - this.M[2][0]*this.M[1][1]);
		
		res.M[0][1] = -det_inv*(this.M[0][1]*this.M[2][2] - this.M[2][1]*this.M[0][2]);
		res.M[1][1] =  det_inv*(this.M[0][0]*this.M[2][2] - this.M[2][0]*this.M[0][2]);
		res.M[2][1] = -det_inv*(this.M[0][0]*this.M[2][1] - this.M[2][0]*this.M[0][1]);
		
		res.M[0][2] =  det_inv*(this.M[0][1]*this.M[1][2] - this.M[1][1]*this.M[0][2]);
		res.M[1][2] = -det_inv*(this.M[0][0]*this.M[1][2] - this.M[1][0]*this.M[0][2]);
		res.M[2][2] =  det_inv*(this.M[0][0]*this.M[1][1] - this.M[1][0]*this.M[0][1]);
		
		return res ;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -