3d Cubes ⚙️

#3d weirdo cubes build from some planes

Created by markknol on 2019/2/5
40
0

Log in to post a comment.

Canvas.setpenopacity(1);

const turtle = new Turtle();

const camera = {
    pos: [230, -120, 0],
    pan: [160, -25, 0],
    f: 120,
}
const randomish = 3.5;

function walk(i) {
    for(let j =0;j<5; j++) {
        const size = 15.9 + (i*1.5);
        const sizeInner = 14 + (i*1.5);
        const sizeDiff = size-sizeInner;
        const h = j * (size*4.5)
        for (let c=0; c<2; c++) {
            const z = (-size + c) * size;
            const plane = [
                [-size,-size+h, z],
                [-size, size+h, z],
                [ size, size+h, z],
                [ size,-size+h, z],
            ];
            drawPlane(plane);
        }
        for (let c=0; c<2; c++) {
            const x = c == 0 ? - size : size;
            const zfront = (-size) * size + sizeDiff;
            const zback = (-size + 1) * size - sizeDiff;
            const plane = [
                [x,  size+h, zfront],
                [x,  size+h, zback ],
                [x, -size+h, zback ],
                [x, -size+h, zfront],
            ];
            drawPlane(plane);
        }
        for (let c=0; c<2; c++) {
            const y = c == 0 ? - size : size;
            const zfront = (-size) * size + sizeDiff;
            const zback = (-size + 1) * size - sizeDiff;
            const plane = [
                [sizeInner , y+h, zfront],
                [sizeInner , y+h, zback ],
                [-sizeInner, y+h, zback ],
                [-sizeInner, y+h, zfront],
            ];
            drawPlane(plane);
        }
    }
    return i < 4;
}

function drawPlane(segments) {
    turtle.penup();
    for(let s=0, l=segments.length; s<=l; s++) {
        if (s==1) turtle.pendown();
        let segment = segments[s % l];
        segment = randomize(segment, randomish);
        segment = add(segment, camera.pos);
        segment = project(segment);
        segment = add(segment, camera.pan);
        turtle.goto(segment);
    }
}

/**************************************************/

// some functions from https://turtletoy.net/turtle/2dc4806767
function project(p) {
    p[2] += camera.f;
    return [p[0]/p[2]*camera.f, p[1]/p[2]*camera.f, p[2]];
}
const randomize = (p, v) => {
    p[0] += -v * 0.5 + Math.random() * v;
    p[1] += -v * 0.5 + Math.random() * v;
    p[2] += -v * 0.5 + Math.random() * v;
    return p;
};
const cos = Math.cos;
const sin = Math.sin;
const scale = (p,scale) => [p[0]*scale, p[1]*scale, p[2]*scale];
const add = (a,b) => [a[0]+b[0], a[1]+b[1], a[2]+b[2]];
const sub = (a,b) => [a[0]-b[0], a[1]-b[1], a[2]-b[2]];
const cross = (a,b) => [
    a[1]*b[2]-b[1]*a[2],
    a[2]*b[0]-b[2]*a[0],
    a[0]*b[1]-b[0]*a[1]
];