Class matrix3d.as - basic, pour aider a comprendre

Contenu du snippet

j'ai trouvé un tutorial sur les matrices sur flash-france:
http://www.flash-france.com/sections.php?op=viewarticle&secid=1&artid=73
comme le code est plutot moche (non, c vrai, c cool mais mal codé), j'ai decidé de convertir en as2.
Voici donc la class.Je ne vais pas expliquer comment ça fonctionne, j'ai compris a peine ce qu'il faut pour pouvoir coder cette class.
Amusez vous, cette class est basic, mais ej compte en developer une plus complete pour des besoins personnels

Source / Exemple :


dans matrix3d.as:
----------------------------------
class matrix3d {
	var polygons:Array;
	var points:Array;
	//
	// Constructor
	function matrix3d() {
		polygons = new Array();
		points = new Array();
	}
	//
	// register a new point
	function registerPoint(x:Number, y:Number, z:Number) {
		var temp:Array = new Array();
		var i:Number;
		for (i=1; i<4; i++) {
			temp[i] = new Array();
		}
		temp[1][1] = x;
		temp[2][1] = y;
		temp[3][1] = z;
		return temp;
	}
	//
	// register a new polygon -> 3 points
	function registerPolygon(pt1:Array, pt2:Array, pt3:Array) {
		polygons.push({pt1:pt1, pt2:pt2, pt3:pt3});
	}
	//
	//
	function mtxMutiply(matriceA, matriceB) {
		var i, j, k:Number;
		var temp:Array = new Array();
		for (i=1; i<4; i++) {
			temp[i] = new Array();
		}
		for (i=1; i<=3; i++) {
			temp[i][1] = 0;
			for (j=1; j<=3; j++) {
				for (k=1; k<=3; k++) {
					temp[i][j] += matriceA[i][k]*matriceB[k][j];
				}
			}
		}
		return temp;
	}
	//
	//
	function getRotationMatrix_x(radian:Number) {
		var rx:Array = new Array();
		var i:Number;
		for (i=1; i<4; i++) {
			rx[i] = new Array();
		}
		rx[1][1] = 1;
		rx[1][2] = 0;
		rx[1][3] = 0;
		rx[2][1] = 0;
		rx[2][2] = Math.cos(radian);
		rx[2][3] = -Math.sin(radian);
		rx[3][1] = 0;
		rx[3][2] = Math.sin(radian);
		rx[3][3] = Math.cos(radian);
		return rx;
	}
	//
	//
	function rotate_x(radian:Number) {
		var i:Number;
		for (i=0; i<=polygons.length-1; i++) {
			polygons[i].pt1 = mtxMutiply(getRotationMatrix_x(radian), polygons[i].pt1);
			polygons[i].pt2 = mtxMutiply(getRotationMatrix_x(radian), polygons[i].pt2);
			polygons[i].pt3 = mtxMutiply(getRotationMatrix_x(radian), polygons[i].pt3);
		}
		return true;
	}
	//
	//
	function drawMatrix(cible:MovieClip, cOption:Object) {
		var i:Number;
		cible.lineStyle(cOption.lineStyle.thickness, cOption.lineStyle.color, cOption.lineStyle.alpha);
		for (i=0; i<=polygons.length-1; i++) {
			cible.beginFill(cOption.fillStyle.color, cOption.fillStyle.alpha);
			cible.moveTo(polygons[i].pt1[1][1], polygons[i].pt1[2][1]);
			cible.lineTo(polygons[i].pt2[1][1], polygons[i].pt2[2][1]);
			cible.lineTo(polygons[i].pt3[1][1], polygons[i].pt3[2][1]);
			cible.lineTo(polygons[i].pt1[1][1], polygons[i].pt1[2][1]);
			cible.endFill();
		}
	}
}

Conclusion :


utilisation:
----------------------------------
var cOption:Object = new Object({lineStyle:new Object(), fillStyle:new Object()});
cOption.lineStyle.thickness = -1;
cOption.lineStyle.color = 0x0;
cOption.lineStyle.alpha = 100;
cOption.fillStyle.color = 0x336699;
cOption.fillStyle.alpha = 20;

var m3d:matrix3d = new matrix3d();
m3d.registerPolygon(m3d.registerPoint(60, -40, -20), m3d.registerPoint(140, 80, -20), m3d.registerPoint(110, 20, 70));
m3d.registerPolygon(m3d.registerPoint(140, 80, -20), m3d.registerPoint(110, 20, 70), m3d.registerPoint(180, -20, -20));
m3d.registerPolygon(m3d.registerPoint(110, 20, 70), m3d.registerPoint(180, -20, -20), m3d.registerPoint(60, -40, -20));
var itv = setInterval(function () {
_root.clear();
m3d.rotate_x(0.1);
m3d.drawMatrix(_root,cOption);
}, 50);

------------------------------------------------------------
comme d'hab, je fournis le code, mais pas le service apres vente.

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.