use a list of cube positions + a single function to place multiple cubes—finally feeling the power of arrays.
Log in to post a comment.
// --------------------------------------------------------------------
// UNIT 5 – List Toy
// Title: Mohr Cube Grid (Data-Driven Placement)
//
// PSEUDOCODE
// define line(x1,y1,x2,y2) → draw a segment
// define drawCube(x, y) → draw all 12 cube edges using vertex offsets
//
// create a list of cube positions: [{x,y}, {x,y}, ... ]
//
// for each entry in the list:
// draw a cube at that position
//
// This demonstrates:
// - lists as data containers
// - iterating across lists
// - one function reused many times
// --------------------------------------------------------------------
Canvas.setpenopacity(1);
const t = new Turtle();
// --------------------------------------------------
// Helper: draw a single line segment between 2 points
function line(x1, y1, x2, y2) {
t.penup();
t.goto(x1, y1);
t.pendown();
t.goto(x2, y2);
}
// --------------------------------------------------
// Draw cube wireframe at (x, y)
// Geometry scaled and angled for visual clarity
function drawCube(x, y) {
const s = 30; // edge length
const dx = 18; // x shift for back face
const dy = -13; // y shift for back face
// FRONT
const A = {x:x, y:y};
const B = {x:x+s, y:y};
const C = {x:x+s, y:y-s};
const D = {x:x, y:y-s};
// BACK (offset)
const E = {x:A.x+dx, y:A.y+dy};
const F = {x:B.x+dx, y:B.y+dy};
const G = {x:C.x+dx, y:C.y+dy};
const H = {x:D.x+dx, y:D.y+dy};
// 12 edges
[
[A,B], [B,C], [C,D], [D,A], // front face
[E,F], [F,G], [G,H], [H,E], // back face
[A,E], [B,F], [C,G], [D,H] // connectors
].forEach(edge => line(edge[0].x, edge[0].y, edge[1].x, edge[1].y));
}
// --------------------------------------------------
// List of cube positions
let cubePositions = [
{x:-80, y:20},
{x:-20, y:20},
{x: 40, y:20},
{x:100, y:20}
];
// --------------------------------------------------
// Iterate once per frame
let idx = 0;
function walk() {
if (idx < cubePositions.length) {
let p = cubePositions[idx];
drawCube(p.x, p.y);
idx++;
return true;
}
return false;
}