function draw() {
let zBuffer = [];
for (let i = 0; i < canv.height; ++i) {
zBuffer[i] = new Int32Array(canv.width + 1);
}
zBuffer = zBuffer.map(e => e.map(l => l - 100));
// console.log(zBuffer);
ctx.clearRect(0, 0, canv.width, canv.height);
for (let i = 0; i < vertex.length; ++i) {
let x = vertex[i].x;
let y = vertex[i].y;
let xx = parseInt(x);
let yy = parseInt(y);
try {
if (vertex[i].z <= parseFloat(zBuffer[yy][xx])) continue;
zBuffer[yy][xx] = vertex[i].z;
} catch (e) {}
ctx.beginPath();
ctx.arc(x, y, 1, 0, Math.PI * 2, false);
ctx.closePath();
ctx.fill();
}
}
// Тут по нажатию левой стрелки, объект вращается.
// MultiplyMatrix - произведение матриц
window.onkeydown = e => {
let grad = (5 * Math.PI) / 180;
// let grad = 360;
let p = 2;
let mat1 = [
[Math.cos(grad), -Math.sin(grad), 0],
[Math.sin(grad), Math.cos(grad), 0],
[0, 0, 1]
];
if (e.keyCode === 37) {
for (let i = 0; i < vertex.length; ++i) {
let mat1 = [
[Math.cos(grad), 0, Math.sin(grad)],
[0, 1, 0],
[-Math.sin(grad), 0, Math.cos(grad)]
];
let mat2 = [[vertex[i].x - 400], [vertex[i].y - 400], [vertex[i].z]];
let ans = MultiplyMatrix(mat1, mat2);
vertex[i].x = ans[0][0] + 400;
vertex[i].y = ans[1][0] + 400;
vertex[i].z = ans[2][0];
}
draw();
}
};