## 锥

2011-11-01 13:57  【当耐特】  阅读(3464)  评论(8编辑  收藏

# 二.圆锥模拟

        var pointPositions = [];
for (var i = -100; i < 200; i += 10) {
var xTemp = getRandomNumber(-i, i);
var zTemp = Math.sqrt(Math.abs(i * i - xTemp * xTemp));
pointPositions.push(new Vector3(xTemp, i, zTemp));
} 

i 既是h,又是半径r。

# 三.旋转与投影

        //旋转
function rotate(angle) {

for (var i = 0; i < pointPositions.length; i++) {
var tempX = pointPositions[i].x;
var tempZ = pointPositions[i].z;
pointPositions[i].x = pointPositions[i].x * Math.cos(angle) - pointPositions[i].z * Math.sin(angle);
pointPositions[i].z = pointPositions[i].z * Math.cos(angle) + tempX * Math.sin(angle);
}
}
//投影
function projection(v) {
var v1 = new Vector3();
v1.x = v.x * distance / Math.abs(cameraPosition.z - v.z);
v1.y = v.y * distance / Math.abs(cameraPosition.z - v.z);
v1.z = v.z;
return v1;
}

# 四.动画

        var currentAngle = 0;
var roundAsync = eval(Jscex.compile("async", function () {
while (true) {
pointPositionsForShow.length = 0;
currentAngle += 0.1;
PositionsProjection();
for (var i = 0; i < pointPositionsForShow.length; i++) {
cxt.fillStyle = randomColor();
cxt.beginPath();
if (pointPositionsForShow[i].z > 0) cxt.globalAlpha = 1;
if (pointPositionsForShow[i].z < 0) cxt.globalAlpha = 0.1;
cxt.arc(centreOfCirclePosition.x + pointPositionsForShow[i].x, centreOfCirclePosition.x + pointPositionsForShow[i].y, distance / Math.abs(cameraPosition.z - pointPositionsForShow[i].z), 0, Math.PI * 2, true);
cxt.closePath();
cxt.fill();
}
\$await(Jscex.Async.sleep(50));
}
}))

# 五.在线演示

javascript异步编程系列目录：http://www.cnblogs.com/iamzhanglei/archive/2011/09/20/2182038.html