// the unit lego is 8mm (wide) x 9.8 mm (tall) // we're using four-wide legos. // 90 x 28 pixels float w = 32.0 / 25.4 * 72; float h = 9.8 / 25.4 * 72; int rows; int cols; float s; class Point { float x, y; Point(float x, float y) { this.x = x; this.y = y; } } Point[][] shapes = new Point[1][0]; void setup() { size(900, 225); s = 3; rows = int(height / h / s); cols = int(width / w / s); } void draw() { scale(s); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols - i % 2; j++) { pushMatrix(); translate(j * w + (i % 2) * w / 2, i * h); fill((i == 0 & j == 0) ? 255 : 127); noStroke(); rect(0, 0, w, h); stroke(0); noFill(); for (int k = 0; k < shapes.length; k++) { for (int l = 0; l < shapes[k].length - 1; l++) { line(shapes[k][l].x, shapes[k][l].y, shapes[k][l + 1].x, shapes[k][l + 1].y); } } Point[] shp = shapes[shapes.length - 1]; if (shp.length > 0) ellipse(shp[0].x, shp[0].y, 1, 1); noFill(); stroke(0); rect(0, 0, w, h); popMatrix(); } } } void mouseClicked() { Point[] shp = shapes[shapes.length - 1]; if (shp.length > 0 && dist(mouseX / s, mouseY / s, shp[0].x, shp[0].y) < 2) { shapes[shapes.length - 1] = (Point[]) append(shp, new Point(shp[0].x, shp[0].y)); shapes = (Point[][]) append(shapes, new Point[0]); } else { shapes[shapes.length - 1] = (Point[]) append(shapes[shapes.length - 1], new Point(mouseX / s, mouseY / s)); } } void keyPressed() { if (key == 'x') { if (shapes[shapes.length - 1].length > 0) { shapes[shapes.length - 1] = (Point[]) shorten(shapes[shapes.length - 1]); } else if (shapes.length > 1) { shapes = (Point[][]) append(shorten(shorten(shapes)), new Point[0]); } } }