nuran
26.09.2003, 08:44
insertSorting = function (a, lb, ub) {
for (i=lb+1; i<=ub; i++) {
t = a[i].z;
temp = a[i].i;
for (j=i-1; j>=lb && (a[j].z>t); j--) {
a[j+1].z = a[j].z;
a[j+1].i = a[j].i;
}
a[j+1].z = t;
a[j+1].i = temp;
}
return (a);
};
sort = function () {
sortedArr = new Array();
for (var i = 0; i<=faces; i++) {
sortedArr[i] = new Object();
sortedArr[i].z = f[i].zindex();
sortedArr[i].i = i;
}
sortedArr = insertSorting(sortedArr, 0, faces);
for (var i = 0; i<=faces; i++) {
f[sortedArr[faces-i].i].fill();
}
};
vertex = function (x, y, z) {
this.x = x;
this.y = y;
this.z = z;
};
vertex.prototype.rotateXY = function(sinX, cosX, sinY, cosY) {
//rotation around of axes X and Y
var yp = this.y*cosY-this.z*sinY;
var zp = this.y*sinY+this.z*cosY;
var xp = this.x*cosX+zp*sinX;
var zp = -this.x*sinX+zp*cosX;
this.x = xp;
this.y = yp;
this.z = zp;
};
vertex.prototype.perspective = function() {
//calculation 3d to 2d
var perRatio = 1/(this.z/dist+1);
this.rx = cnx+this.x*perRatio;
this.ry = cny+this.y*perRatio;
};
face = function (c, d, a) {
// dimension of the face
this.d = d;
this.c = c;
// array of the vertexs included in the face
this.a = a;
};
face.prototype.draw = function() {
shadow = 0;
for (var i = 0; i<=this.d; i++) {
shadow += (-v[this.a[i]].rx+v[this.a[i]].ry);
}
shadow = 140+shadow/(this.d+2);
_root.lineStyle(0, 0x000000, 100);
_root.beginFill(shadow << 16 | shadow << 8 | shadow << 0, 100);
_root.moveTo(v[this.a[0]].rx, v[this.a[0]].ry);
for (var i = 1; i<=(this.d); i++) {
_root.lineTo(v[this.a[i]].rx, v[this.a[i]].ry, v[this.a[i+1]].rx, v[this.a[i+1]].ry);
}
_root.endFill();
};
face.prototype.fill = function() {
var vis = ((v[this.a[1]].rx-v[this.a[0]].rx)*(v[this.a[2]].ry-v[this.a[0]].ry)-(v[this.a[1]].ry-v[this.a[0]].ry)*(v[this.a[2]].rx-v[this.a[0]].rx));
if ((this.c+2)%2) {
vis *= -1;
}
if (vis>=0) {
this.draw();
}
delete vis;
};
face.prototype.zindex = function() {
var zindex = 0;
for (var i = 0; i<=this.d; i++) {
zindex += v[this.a[i]].z;
}
return (Math.round(zindex));
delete zindex;
};
_root.onLoad = function() {
cnx = 250;
cny = 250;
dist = 200;
var radius = 100;
var width = 50;
rad = Math.PI/180;
preRad = rad/25;
// initialization of the vertexs & build star
v = new Array();
var ang = (360/10)*rad;
for (var i = 0; i<=19; i += 2) {
var angle = ang*i;
var width = (i == 10) ? -width : width;
v[i+1] = new vertex(Math.cos(angle)*radius*0.5, Math.sin(angle)*radius*0.5, width);
v[i] = new vertex(Math.cos(angle)*radius, Math.sin(angle)*radius, width);
}
// initialization of the faces
f = new Array();
f[0] = new face(0, 9, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
f[1] = new face(1, 9, [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]);
f[2] = new face(0, 3, [0, 1, 11, 10]);
f[3] = new face(0, 3, [1, 2, 12, 11]);
f[4] = new face(0, 3, [2, 3, 13, 12]);
f[5] = new face(0, 3, [3, 4, 14, 13]);
f[6] = new face(0, 3, [4, 5, 15, 14]);
f[7] = new face(0, 3, [5, 6, 16, 15]);
f[8] = new face(0, 3, [6, 7, 17, 16]);
f[9] = new face(0, 3, [7, 8, 18, 17]);
f[10] = new face(0, 3, [8, 9, 19, 18]);
f[11] = new face(0, 3, [9, 0, 10, 19]);
// ----------------------------------
faces = f.length-1;
amount = v.length-1;
// ---------------
delete radius, width, ang, angle;
};
// ####################################################
// -- 3D STAR + REAL Z SORTING
// -- © 2002-2003 Grigory Ryabov.
// -- http://www.flash.plux.ru
// ####################################################
_root.onEnterFrame = function() {
var xa = -(cnx-_ymouse)*preRad;
var ya = (cny-_xmouse)*preRad;
var sinY = Math.sin(ya);
var cosY = Math.cos(ya);
var sinX = Math.sin(xa);
var cosX = Math.cos(xa);
for (var i = 0; i<=amount; i++) {
v[i].rotateXY(sinY, cosY, sinX, cosX);
v[i].perspective();
}
_root.clear();
sort();
};
stop();
for (i=lb+1; i<=ub; i++) {
t = a[i].z;
temp = a[i].i;
for (j=i-1; j>=lb && (a[j].z>t); j--) {
a[j+1].z = a[j].z;
a[j+1].i = a[j].i;
}
a[j+1].z = t;
a[j+1].i = temp;
}
return (a);
};
sort = function () {
sortedArr = new Array();
for (var i = 0; i<=faces; i++) {
sortedArr[i] = new Object();
sortedArr[i].z = f[i].zindex();
sortedArr[i].i = i;
}
sortedArr = insertSorting(sortedArr, 0, faces);
for (var i = 0; i<=faces; i++) {
f[sortedArr[faces-i].i].fill();
}
};
vertex = function (x, y, z) {
this.x = x;
this.y = y;
this.z = z;
};
vertex.prototype.rotateXY = function(sinX, cosX, sinY, cosY) {
//rotation around of axes X and Y
var yp = this.y*cosY-this.z*sinY;
var zp = this.y*sinY+this.z*cosY;
var xp = this.x*cosX+zp*sinX;
var zp = -this.x*sinX+zp*cosX;
this.x = xp;
this.y = yp;
this.z = zp;
};
vertex.prototype.perspective = function() {
//calculation 3d to 2d
var perRatio = 1/(this.z/dist+1);
this.rx = cnx+this.x*perRatio;
this.ry = cny+this.y*perRatio;
};
face = function (c, d, a) {
// dimension of the face
this.d = d;
this.c = c;
// array of the vertexs included in the face
this.a = a;
};
face.prototype.draw = function() {
shadow = 0;
for (var i = 0; i<=this.d; i++) {
shadow += (-v[this.a[i]].rx+v[this.a[i]].ry);
}
shadow = 140+shadow/(this.d+2);
_root.lineStyle(0, 0x000000, 100);
_root.beginFill(shadow << 16 | shadow << 8 | shadow << 0, 100);
_root.moveTo(v[this.a[0]].rx, v[this.a[0]].ry);
for (var i = 1; i<=(this.d); i++) {
_root.lineTo(v[this.a[i]].rx, v[this.a[i]].ry, v[this.a[i+1]].rx, v[this.a[i+1]].ry);
}
_root.endFill();
};
face.prototype.fill = function() {
var vis = ((v[this.a[1]].rx-v[this.a[0]].rx)*(v[this.a[2]].ry-v[this.a[0]].ry)-(v[this.a[1]].ry-v[this.a[0]].ry)*(v[this.a[2]].rx-v[this.a[0]].rx));
if ((this.c+2)%2) {
vis *= -1;
}
if (vis>=0) {
this.draw();
}
delete vis;
};
face.prototype.zindex = function() {
var zindex = 0;
for (var i = 0; i<=this.d; i++) {
zindex += v[this.a[i]].z;
}
return (Math.round(zindex));
delete zindex;
};
_root.onLoad = function() {
cnx = 250;
cny = 250;
dist = 200;
var radius = 100;
var width = 50;
rad = Math.PI/180;
preRad = rad/25;
// initialization of the vertexs & build star
v = new Array();
var ang = (360/10)*rad;
for (var i = 0; i<=19; i += 2) {
var angle = ang*i;
var width = (i == 10) ? -width : width;
v[i+1] = new vertex(Math.cos(angle)*radius*0.5, Math.sin(angle)*radius*0.5, width);
v[i] = new vertex(Math.cos(angle)*radius, Math.sin(angle)*radius, width);
}
// initialization of the faces
f = new Array();
f[0] = new face(0, 9, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
f[1] = new face(1, 9, [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]);
f[2] = new face(0, 3, [0, 1, 11, 10]);
f[3] = new face(0, 3, [1, 2, 12, 11]);
f[4] = new face(0, 3, [2, 3, 13, 12]);
f[5] = new face(0, 3, [3, 4, 14, 13]);
f[6] = new face(0, 3, [4, 5, 15, 14]);
f[7] = new face(0, 3, [5, 6, 16, 15]);
f[8] = new face(0, 3, [6, 7, 17, 16]);
f[9] = new face(0, 3, [7, 8, 18, 17]);
f[10] = new face(0, 3, [8, 9, 19, 18]);
f[11] = new face(0, 3, [9, 0, 10, 19]);
// ----------------------------------
faces = f.length-1;
amount = v.length-1;
// ---------------
delete radius, width, ang, angle;
};
// ####################################################
// -- 3D STAR + REAL Z SORTING
// -- © 2002-2003 Grigory Ryabov.
// -- http://www.flash.plux.ru
// ####################################################
_root.onEnterFrame = function() {
var xa = -(cnx-_ymouse)*preRad;
var ya = (cny-_xmouse)*preRad;
var sinY = Math.sin(ya);
var cosY = Math.cos(ya);
var sinX = Math.sin(xa);
var cosX = Math.cos(xa);
for (var i = 0; i<=amount; i++) {
v[i].rotateXY(sinY, cosY, sinX, cosX);
v[i].perspective();
}
_root.clear();
sort();
};
stop();