Hola a todos:
Acabo de terminar una clase re-usable que pienso que podr�a 
interesaros. Si ten�is dudas en el uso, o quer�is a�adir o sugerir algo 
por favor no dud�is en poneros en contacto conmigo. Muchas gracias.

Espero que lo disfrut�is tanto como yo haci�ndolo.

M.

PD.- Est� escrito en ingl�s pero no creo que sea ning�n problema.

---------------8< ------ C�digo ------------------

// -------------------
// Basic Matrix Class
// -------------------
// Description: It will help us in many different ways: 3D Engine, 
Particles movement, etc.
// Author: Manuel de la Higuera ([EMAIL PROTECTED])
// Date: 21th August 2001
// Flash Version: 5+

// Matrix constructor
// ------------------
//
// usage:
//  matrixName = new Matrix (rows, columns);
//

function Matrix (rows, columns) {
        if (typeof (rows) == "number" && typeof (columns) == "number")
                this.rows = rows;
                this.columns = columns;
}


// matrix.clear()
// --------------
// Matrix.clear will make the matrix full of 0 like:
//        
//         0   0   0
//         0   0   0
//         0   0   0
//

Matrix.prototype.clear = function () { 
        for (var iColumns = 1; iColumns<=this.columns; ++iColumns) 
                for (var iRows = 1; iRows<=this.rows; ++iRows) 
                        this["element" add iRows add "-" add iColumns] 
= 0;
}


// matrix.isSquared()
// ------------------
// This method will check whether matrix is squared or not.

Matrix.prototype.isSquared = function () { 
        return (this.rows == this.columns);
}

// matrix.makeIdentity()
// ---------------------
// This will make the matrix take values like the Identity. Matrix 
_must_ be an Squared one.
//
// Identity Matrix:
//                          1   0   0
//                M (r,r)=  0   1   0
//                          0   0   1
//

Matrix.prototype.makeIdentity = function () {
        if (matrix.isSquared())
                for (var iColumns = 1; iColumns<=this.columns; 
++iColumns) 
                for (var iRows = 1; iRows<=this.rows; ++iRows) 
                        if (iRows == iColumns) this["element" add iRows 
add "-" add iColumns] = 1;
                        else {
                                this["element" add iRows add "-" add 
iColumns] = 0;
                        }
}

// matrix.checkFilled()
// --------------------
// Will check whether our matrix is correctly filled (all its elements) 
or not.

Matrix.prototype.checkFilled = function () {
        var countElementsCreated = 0;
        for (name in this) 
                if (name.indexOf("element")==0) countElementsCreated++;
                return (countElementsCreated == this.columns*this.rows);
        delete countElementsCreated;
}


// matrix.setRow(arguments)
// ------------------------
// This will set the row defined by the first argument.

Matrix.prototype.setRow = function () {
        if (arguments.length - 1 <= this.columns && arguments[0] <= 
this.columns)
                for (var iArgs=1; iArgs < arguments.length; iArgs++)
                        if (typeof(arguments[iArgs]) == "number") 
accept = true;
                        else{
                                accept = false;
                                break;
                        }
        if (accept)
                for (var iColumns = 1; iColumns <= arguments.length-1; 
iColumns++) {
                                this["element" add arguments[0] add "-" 
add iColumns] = arguments[iColumns];
                }
                for (var iColumns = arguments.length; iColumns <= 
this.columns; iColumns++) {
                                this["element" add arguments[0] add "-" 
add iColumns] = 0;
                }
}


// matrix.setColumn(arguments)
// ---------------------------
// This will set the column defined by the first argument.

Matrix.prototype.setColumn = function () {
        if (arguments.length - 1 <= this.rows && arguments[0] <= 
this.rows)
                for (var iArgs=1; iArgs < arguments.length; iArgs++)
                        if (typeof(arguments[iArgs]) == "number") 
accept = true;
                        else{
                                accept = false;
                                break;
                        }
        if (accept) 
                for (var iRows = 1; iRows <= arguments.length-1; 
iRows++) {
                        this["element" add iRows add "-" add arguments
[0]] = arguments[iRows];
                }
                for (var iRows = arguments.length; iRows <= this.rows; 
iRows++) {
                        this["element" add iRows add "-" add arguments
[0]] = 0;
                }       
}

// matrix.makeTranspose()
// ----------------------
// It constructs an object called "transpose" into the matrix object 
with the Transposed Matrix.

Matrix.prototype.makeTranspose = function () {
        if (this.checkFilled()) 
                this.transpose = new Matrix(this.columns, this.rows);
                for (var iColumns = 1; 
iColumns<=this.transpose.columns; ++iColumns) 
                        for (var iRows = 1; iRows<=this.transpose.rows; 
++iRows) 
                                this.transpose["element" add iRows 
add "-" add iColumns] = this["element" add (iColumns) add "-" add 
(iRows)];
}


// matrix.plusNumber(scalar)
// -------------------------
// This method will add an scalar to the matrix

Matrix.prototype.plusNumber = function (scalar) {
        if (typeof(scalar) == "number")
                for (var iColumns = 1; iColumns<=this.columns; 
++iColumns) 
                        for (var iRows = 1; iRows<=this.rows; ++iRows) 
                                this["element" add iRows add "-" add 
iColumns] += scalar;
}


// matrix.multNumber(scalar)
// -------------------------
// This method will multiplicate an scalar to the matrix

Matrix.prototype.multNumber = function (scalar) {
        if (typeof(scalar) == "number")
                for (var iColumns = 1; iColumns<=this.columns; 
++iColumns) 
                        for (var iRows = 1; iRows<=this.rows; ++iRows) 
                                this["element" add iRows add "-" add 
iColumns] *= scalar;
}


// matrix.plusMatrix(matrix, resultant)
// -------------------
// Sum between Matrices

Matrix.prototype.plusMatrix = function (matrix, resultant) {
        if (this.columns == matrix.columns && this.rows == matrix.rows 
&& this.checkFilled() && matrix.checkFilled() && typeof(resultant) 
== "string" && typeof(_root[resultant]) != "object") 
                _root[resultant] = new _root.Matrix
(this.columns,this.rows);
                for (var iColumns = 1; iColumns<=this.columns; 
++iColumns) 
                        for (var iRows = 1; iRows<=this.rows; ++iRows) 
                                _root[resultant]["element" add iRows 
add "-" add iColumns] = this["element" add iRows add "-" add iColumns]
+matrix["element" add iRows add "-" add iColumns];
}

// matrix.multMatrix(matrix, resultant)
// ------------------------------------
// Matrices multiplication

Matrix.prototype.multMatrix = function (matrix, resultant) {
        if (this.rows == matrix.columns && this.checkFilled() && 
matrix.checkFilled() && typeof(resultant) == "string") 
                _root[resultant] = new _root.Matrix(this.rows, 
matrix.columns);
                for (var iRows = 1; iRows<=matrix.columns; ++iRows) {
                        for (var iColumns = 1; iColumns<=this.rows; 
++iColumns) {
                                for (var iElements = 1; iElements <= 
this.columns; iElements++) {
                                        sum += Number(this["element" 
add iRows add "-" add iElements] * matrix["element" add iElements add "-
" add iColumns]);
                                        trace(sum);
                                }
                                _root[resultant]["element" add iRows 
add "-" add iColumns] = sum;                    
                                delete sum;
                        }
                }       
}


Responder a