Even AS1 Matrix Class

xiaoxiao2021-03-06  115

This is a class written by the 3D engine that is applying in May last year. Unfortunately, those things are lost in the school studio when they leave school. I saw it before the Empire Forum. I don't know which version. Go home I suddenly looked at the graphics book, I found that the line generation has been thrown to the teacher (should not be said to be a teacher, I have never been overlined.), Huh. However, it is still adapted behind. After all, the line of transcripts can still have time to work again, and have collected a lot of information recently.

Pre {Font-Family: "Courier New", Courier, Arial; font-size: 12px;}

. galan {color: # 000000;

. readyword {color: # 993300;

.IDENTIFIER {color: # 000087;

.properties {color: # 000087;

{Color: # 000087;

.LINECMMMENT, .BLOCKCOMMENT {Color: # 808080;

. String {color: # 0000ff;}

Function Matrix () {

/ / Construct a two-dimensional array

Function constructor () {

Var i;

T = new array ();

For (i = 0; i <4; i ) {

T [I] = new array ();

}

Return T;

}

// Public data member

THIS.MAT = Constructor ();

// Private member function, calculating algebraic surplus of matrices

Function Valuedim (a) {

VAR Num1, Num2;

Num1 = a [0] [0] * a [1] [1] * a [2] [2] a [0] [1] * a [1] [2] * a [2] [0] a [0] [2] * a [1] [0] * a [2] [1];

Num2 = a [0] [2] * a [1] [1] * a [2] [0] a [0] [1] * a [1] [0] * a [2] [2] a [0] [0] * a [1] [2] * a [2] [1];

Return (NUM1-NUM2);

}

}

// Clear

Matrix.prototype.zeromatrix = function () {

Var i, j;

For (i = 0; i <4; i ) {

Mat [I] = new array ();

For (j = 0; j <4; j ) {

Mat [I] [j] = 0;

}

}

}

//Unitization

Matrix.prototype.LoadindenDetity = function () {

Var i;

Zeromatrix ();

For (i = 0; i <4; i ) {

MAT [I] [i] = 1;

}

}

// When rotating M = 1, when the X axis; m = 2, around the Y-axis; M = 3 Wound Z-axis

Matrix.Prototype.Rotate3D = function (m, theeta) {

Var m1, m2;

Var C, S;

();

MAT [M-1] [M-1] = 1;

Mat [3] [3] = 1;

M1 = (m% 3) 1;

M2 = (m1% 3) 1;

M1 - = 1;

C = Math.cos (Theta);

S = Math.sin (Theta);

Mat [m1] [m1] = C;

Mat [m1] [m2] = -s;

MAT [M2] [M2] = C;

MAT [M2] [M1] = S;

}

// translation transform matrix

// Parameter TX, TY, TZ represent x, y, z displacement, respectively

// 1 0 0 0 // 0 1 0 0

// 0 0 1 0

// TX TY TZ 1

Matrix.Prototype.Translate3D = function (TX, TY, TZ) {

();

Mat [0] [3] = TX;

Mat [1] [3] = TY;

Mat [2] [3] = Tz;

}

/ / Zoom transformation matrix

// SX, SY, SZ represent the scale of the X, Y, Z direction, respectively.

// SX 0 0 0

// 0 SY 0 0

// 0 0 SZ 0

// 0 0 0 1

Matrix.prototype.scaled3d = function (SX, SY, SZ) {

();

Mat [0] [0] = SX;

Mat [1] [1] = SY;

Mat [2] [2] = SZ;

}

/ / Remove the matrix flat amount, and perspective transformation (you have to define some perspective functions)

// 0

// 0

// 0

// 0 0 0

Matrix.Prototype.Rotcomponet = function () {

Mat [0] [3] = 0;

MAT [1] [3] = 0;

Mat [2] [3] = 0;

MAT [3] [0] = 0;

Mat [3] [1] = 0;

Mat [3] [2] = 0;

}

// Reversible inverse matrix, because here is a quarter coordinate matrix is ​​four-dimensional,

// So the remaining sub-type Valuedim () is better

// If you ask if you ask other N-dimensional words, then other methods.

Matrix.prototype.vertdim = function (b) {

VAR I, J, LIN, COL, I1, J1;

VAR D, DETA1;

Var c = new array ();

For (i = 0; i <4; i ) {

For (j = 0; j <4; j ) {

LIN = 0;

COL = 0;

For (i1 = 0; i1 <4; i1 ) {

IF (i1! = i) {

C [Lin] = New Array ();

For (J1 = 0; J1 <4; J1 ) {

IF (j1! = j) {

C [lin] [col] = mat [i] [i];

COL = 1;

}

}

LIN = 1;

COL = 0;

}

}

Deta1 = Valuedim (c);

IF ((i j)% 2 == 0) {

B.MAT [J] [i] = DETA1;

} else {

B.MAT [J] [i] = -deta1;

}

}

}

D = 0;

For (i = 0; i <4; i ) {

D = MAT [0] [i] * b.mat [i] [0];

}

IF (d == 0) {

Return;

}

For (i = 0; i <4; i ) {

For (j = 0; j <4; j ) {

B.MAT [i] [j] / = d;

}

}

}

/ / Request the product of two matrices

Matrix.Prototype.matrix4x4 = function (v1, v2) {

VAR I, J, K;

For (i = 0; i <4; i ) {

For (j = 0; j <4; j ) {

Mat [I] [j] = 0;

FOR (k = 0; k <4; k ) {

Mat [i] [j] = v1.mat [i] [k] * v2.mat [k] [j];

}

}

}

}

// Copy a matrix

Matrix.prototype.copymatrix = function (v1) {

Var i, j;

For (i = 0; i <4; i ) {

For (j = 0; j <4; j ) {

MAT [I] [J] = v1.mat [i] [j];

}

}

}

// Place the transformation matrix around any axis

// This uses a class CVector I have not defined yet, it is a vector class

// Inherited from the cpointer class, this evening (the order in the counter is?)

// pbeg represents the starting point of any axis

// Pend indicates the end of any axis or the direction of the axis, which is to see the value of Key.

// key = 0 PEND indicates the end point

// Key = 1 PEND indicates the axial direction vector (the starting point is the default is the origin)

Matrix.prototype.makerotateaxis = function (PBEG, Pend, Angle, Key) {

Var r = 0, SPSI, CPSI;

Var i;

P = new cvector ();

MAT1 = new matrix ();

MA = new matrix ();

Rx = new matrix ();

Ry = new matrix ();

Rz = new matrix ();

RX1 = new matrix ();

RY1 = new matrix ();

Mt1 = new matrix ();

IF (Key! = 1) {

P.VectorPointminus (Pend, PBEG);

p.NORVEC ();

} else {

p.copy;

p.NORVEC ();

}

// translation matrix

// 1 0 0 0

// 0 1 0 0

// 0 0 1 0

// - PBEG [0] -pbeg [1] -pbeg [2] 1

For (i = 0; i <4; i ) {

m.mat [I] [i] = 1;

}

MA.MAT [0] [3] = -pbeg [0];

MA.MAT [1] [3] = -pbeg [1]; ma.mat [2] [3] = -pbeg [2];

// reverse flat matrix

// 1 0 0 0

// 0 1 0 0

// 0 0 1 0

// pbeg [0] PBEG [1] PBEG [2] 1

For (i = 0; i <4; i ) {

Mt1.mat [i] [i] = 1;

}

Mt1.mat [0] [3] = PBEG [0];

Mt1.mat [1] [3] = PBEG [1];

Mt1.mat [2] [3] = -pbeg [2];

// Rotating matrix around X-axis

// 1 0 0 0

// 0 cos θ sinθ 0

// 0 -sin θ cos θ 0

// 0 0 0 1

SPSI = 0;

CPSI = 1;

r = math.sqrt (p.y * py p.z * p.z);

IF (r> = 1.e-5) {

SPSI = P.Y / R;

CPSI = P.Z / R;

} else {

R = 0;

}

For (i = 0; i <4; i ) {

Rx.mat [i] [i] = 1;

}

Rx.mat [1] [1] = CPSI;

Rx.mat [1] [2] = -SPSI;

Rx.mat [2] [1] = SPSI;

Rx.mat [2] [2] = CPSI;

// Waves the X-axis reverse rotation matrix

// 1 0 0 0

// 0 cos θ -sin θ 0

// 0 sin θ cos θ 0

// 0 0 0 1

For (i = 0; i <4; i ) {

Rx1.mat [i] [i] = 1;

}

RX1.MAT [1] [1] = CPSI;

Rx1.mat [1] [2] = SPSI;

Rx1.mat [2] [1] = -SPSI;

Rx1.mat [2] [2] = CPSI;

// Rotating matrix around Y-axis

// cos θ 0 -sin θ 0

// 0 1 0 0

// sin θ 0 cos θ 0

// 0 0 0 1

For (i = 0; i <4; i ) {ry.mat [i] [i] = 1;

}

SPSI = -p.x;

CPSI = R;

Ry.mat [0] [0] = CPSI;

Ry.mat [0] [2] = -SPSI;

Ry.mat [2] [0] = SPSI;

Ry.mat [2] [2] = CPSI;

// Waves the y-axis reverse rotation matrix

// cos θ 0 sinθ 0

// 0 1 0 0

// -sin θ 0 cos θ 0

// 0 0 0 1

For (i = 0; i <4; i ) {

Ry1.mat [i] [i] = 1;

}

Ry1.mat [0] [0] = CPSI;

Ry1.mat [0] [2] = SPSI;

Ry1.mat [2] [0] = -SPSI;

Ry1.mat [2] [2] = CPSI;

// Rotate the matrix around the Z-axis

// cos θ sinθ 0 0

// -sin θ cos θ 0 0

// 0 0 1 0

// 0 0 0 1

For (i = 0; i <4; i ) {

Rz.mat [i] [i] = 1;

}

SPSI = Math.sin (Angle);

CPSI = Math.cos (ANGLE);

Rz.mat [0] [0] = CPSI;

Rz.mat [0] [1] = -SPSI;

Rz.mat [1] [0] = SPSI;

Rz.mat [1] [1] = CPSI;

// mt1 = ma · RX · RY · RZ · RX1 · RY1 · MT1

// The final result matrix exists in MAT

Mat1.matrix4x4 (MT1, RX1);

Mt1.matrix4x4 (MAT1, RY1);

Mat1.matrix4x4 (MT1, RZ);

Mt1.matrix4x4 (MAT1, RY);

Mat1.matrix4x4 (MT1, RX);

Mt1.matrix4x4 (MAT1, MA);

Copymatrix (MT1);

}

// Symmetrical transform matrix with a flat axis

// This is a two-dimensional matrix, mainly with the textbook I want to do, or it will not define this function.

// PBEG, PEND, KEY representative meaning the same as the above rotation function

/ / The result is placed in MAT. Nothing explains

Matrix.prototype.makereflectaxis = function (PBEG, Pend, Key) {

Var r = 0, SPSI, CPSI;

Var i, j;

P = new cvector ();

Mat = new matrix ();

MA = new matrix ();

Rx = new matrix ();

RX1 = new matrix ();

Mt1 = new matrix ();

IF (Key! = 1) {

P.VectorPointminus (Pend, PBEG);

p.NORVEC ();

} else {p.copy;

p.NORVEC ();

}

For (i = 0; i <4; i ) {

m.mat [I] [i] = 1;

}

MA.MAT [0] [3] = -pbeg [0];

MA.MAT [1] [3] = -pbeg [1];

MA.MAT [2] [3] = -pbeg [2];

For (i = 0; i <4; i ) {

Mt1.mat [i] [i] = 1;

}

Mt1.mat [0] [3] = PBEG [0];

Mt1.mat [1] [3] = PBEG [1];

Mt1.mat [2] [3] = PBEG [2];

SPSI = 0;

CPSI = 1;

r = math.sqrt (p.x * p.x p.y * p.y);

IF (r> 1.e-5) {

SPSI = P.Y / R;

CPSI = P.X / R;

} else {

R = 0;

}

For (i = 0; i <4; i ) {

Rx.mat [i] [i] = 1;

}

Rx.mat [0] [0] = CPSI;

Rx.mat [0] [1] = SPSI;

Rx.mat [1] [0] = -SPSI;

Rx.mat [1] [1] = CPSI;

For (i = 0; i <4; i ) {

Rx1.mat [i] [i] = 1;

}

Rx1.mat [0] [0] = CPSI;

Rx1.mat [0] [1] = -SPSI;

Rx1.mat [1] [0] = SPSI;

RX1.MAT [1] [1] = CPSI;

For (i = 0; i <4; i ) {

Rs.mat [i] [i] = 1;

}

Rs.mat [0] [0] = 1;

rs.mat [0] [1] = 0;

Rs.mat [1] [0] = 0;

rs.mat [1] [1] = -1;

Mat.Matrix4x4 (MT1, RX1);

Mt1.matrix4x4 (MAT, RS);

Mat.matrix4x4 (MT1, RX);

Mt1.matrix4x4 (MAT, MA);

Copymatrix (MT1);

}

转载请注明原文地址:https://www.9cbs.com/read-100897.html

New Post(0)