<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
</body>
</html>
// 3D points array.
var points=[];
var Vector=function(x,y,z) {
this.x=x;
this.y=y;
this.z=z;
};
Vector.prototype.toString= function() {
return (' x: ' + this.x + ' y: ' + this.y + ' z : ' + this.z);
};
// returns a new vector translatedfrom ampVec * random
Vector.prototype.createRandomShift= function(ampVec) {
var thisRandom = Math.random()*0.9+0.1;
return new Vector(
thisRandom*ampVec.x+this.x,
thisRandom*ampVec.y+this.y,
thisRandom*ampVec.z+this.z );
};
var pointCount=20;
var pt = new Vector(Math.random()*100|0, Math.random()*100|0, Math.random()*100|0 );
var ampVec = new Vector(Math.random()*4-2, Math.random()*4-2, Math.random()*4 - 2);
var ampVecAmp = new Vector(Math.random()*2-1, Math.random()*2-1, Math.random()*2-1) ;
// fill the point array.
// based on the current point shifted randomly by ampVec
// ampVec itself is shifted randomly by ampVecAmp
for(var i=0; i<pointCount; i++) {
points.push(pt );
pt=pt.createRandomShift(ampVec);
ampVec=ampVec.createRandomShift(ampVecAmp);
}
// array of face. a face is an array of 3 point indexes.
var facelist=[];
var faceCount=10, i=faceCount;
// fill the face array
while(i--) {
var thisFacePoints=[];
var ip1 = 0;
// 3 point indexes
thisFacePoints.push(ip1=Math.random()*pointCount|0);
thisFacePoints.push(Math.random()*pointCount|0);
thisFacePoints.push(Math.random()*pointCount|0);
facelist.push(thisFacePoints);
}
// returns the min z for a face
var minZ=function(f) {
return Math.min(f[0].z, Math.min(f[1].z, f[2].z));
};
// returns the max z for a face
var maxZ=function(f) {
return Math.max(f[0].z, Math.max(f[1].z, f[2].z));
};
// !!! sort the faces on Z !!!
facelist.sort(function(a,b) {
if (maxZ(a) < minZ (b)) return 1; // a fully below b
if (minZ(b) < minZ(a) ) return -1; // b fully below a
// !!!!!!!!!!!!!!!!!!!!!!!!
// in fact here is the tricky part :
// here a and b overlap in Z.
return trickyFaceZSort(a,b);
});
// !!!!!! returns wether face a is < to face b when looking at Z (0,0,1).
//Required : a and b overlaps by Z.
var trickyFaceZSort= function(a,b) {
// do some checks on projected(xi), projected(yi) -> return 0 if no overlap
// then you have the hardest part ahead : x,y and z overlap.
return 0;
};
// Print sorted faces
for(var i=0; i<facelist.length; i++) {
var fp=facelist[i];
console.log(' *** face ' + i + ' *** ');
console.log(' ** point 0 ' + points[fp[0]] + ' *** ');
console.log(' ** point 1 ' + points[fp[1]] + ' *** ');
console.log(' ** point 2 ' + points[fp[2]] + ' *** ');
}
Output
300px
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |