View Single Post
Old May 30th, 2006, 12:54 AM   #1
grimpirate
King of Portal
 
grimpirate's Avatar
 
Join Date: Sep 2005
Posts: 437
Rep Power: 4 grimpirate is on a distinguished road
Send a message via Yahoo to grimpirate
Lightbulb Matrix Math in ActionScript 2.0

Don't know if this will be of any help to anyone but I've been working on creating a very simplistic 3d engine in Flash, and so it was necessary to figure out how to do some matrix operations. Hope these help anyone whose had a similar problem. Some functions build on the others so, be careful of using one without the others.
// Computes the Inverse of a matrix
function inverse(A):Array{
	var Ainv, E, i, j, k, n, swap;
	n = A.length;
	Ainv = identity(n);
	E = identity(n);
	for(j = 0; j < n; j++){
		for(i = j; i < n; i++){
			if(i == j){
				if(A[i][j] == 0){
					for(k = j + 1; k < n; k++){
						if(A[k][j] != 0){
							swap = A[i];
							A[i] = A[k];
							A[k] = swap;
							swap = Ainv[i];
							Ainv[i] = Ainv[k];
							Ainv[k] = swap;
							k = n;
						}
					}
				}
				E[i][j] = 1 / A[i][j];
			}else{
				E[i][j] = -A[i][j];
			}
			A = multiply(E, A);
			Ainv = multiply(E, Ainv);
			E = identity(n);
		}
	}
	for(j = n - 1; j > 0; j--){
		for(i = j - 1; i > -1; i--){
			E[i][j] = -A[i][j];
			A = multiply(E, A);
			Ainv = multiply(E, Ainv);
			E = identity(n);
		}
	}
	return Ainv;
}

// Multiplies two matrices together by making use of the Dot Product
function multiply(A, B):Array{
	var M, i, j;
	B = transpose(B);
	M = new Array(A.length);
	for(i = 0; i < M.length; i++){
		M[i] = new Array(B.length);
	}
	for(i = 0; i < A.length; i++){
		for(j = 0; j < B.length; j++){
			M[i][j] = dotProduct([A[i]],[B[j]]);
		}
	}
	return M;
}

// Calculate the Transpose of a matrix
// Takes matrices of any type so long as they're enclosed in double brackets
// I.E. [[]] This applies to 1 x 1 matrices as well as row and column vectors
function transpose(A):Array{
	var T, i, j;
	T = new Array(A[0].length);
	for(i = 0; i < T.length; i++){
		T[i] = new Array(A.length);
	}
	for(i = 0; i < A.length; i++){
		for(j = 0; j < A[0].length; j++){
			T[j][i] = A[i][j];
		}
	}
	return T;
}

// Calculate the Dot Product of two vectors
// Takes two row vectors of the form [[,,,...]]
// OR column vectors of the form [[],[],...]
function dotProduct(a, b):Number{
	var product = 0;
	var i, j;
	if(a.length == 1){
		for(j = 0; j < a[0].length; j++){
			product += a[0][j] * b[0][j];
		}
	}else{
		for(i = 0; i < a.length; i++){
			product += a[i][0] * b[i][0];
		}
	}
	return product;
}

// Returns an identity matrix of Nth size
function identity(n):Array{
	var A, i, j;
	A = new Array(n);
	for(i = 0; i < n; i++){
		A[i] = new Array(n);
	}
	for(i = 0; i < n; i++){
		for(j = 0; j < n; j++){
			if(i == j){
				A[i][j] = 1;
			}else{
				A[i][j] = 0;
			}
		}
	}
	return A;
}
If it so happens that the 3d engine works and I have any other useful matrix functions, I'll post them here. Need any explanations of the code just ask your questions and I'd be happy to answer.
__________________
Lo, there do I see my father. 'Lo, there do I see My mother, and my sisters, and my brothers. 'Lo, there do I see The line of my people... Back to the beginning. 'Lo, they do call to me. They bid me take my place among them. In the halls of Valhalla... Where the brave... May live... ...forever.. GrimBB | Mimesis
grimpirate is offline   Reply With Quote