// function to create Lego bricks of different sizes // define the basic units of length width = 7.8; height = 9.5; // main function - asks for size(x, y), and color(not being used right now) function brick(row, column) { // create the variables used inside function var main, hole, pin, pin_copy, bottom, bottom_cut; // create the main brick shape - rectangular-prism main = cube(width * row, height, width * column); // create the cut-out from the bottom of the brick - rectangular-prism hole = cube((width * row)-1.5, 8.0, (width * column)-1.5); // create the "pins", or bumps, on the brick - cylinder pin = cylinder(2.5, 1.5); // length from center to edge in different axis // 0 - x, 1 - y from_center = [(width * row) / 2, (width * column) / 2]; // move the cut-out to the correct position hole.translateY(-0.75); // now produce the cut-out by "differencing" from the brick main = main.difference(hole); // move the FIRST pin to the correct position pin.translate(from_center[0]-4, 5, from_center[1]-4); // loop for creating other pins // y-axis for(j = 0; j < column; j ++) { // x-axis for (i = 0; i < row; i ++) { // make a copy of the pin to display - will get reused every time pin_copy = pin.clone(); // move the copy to correct pos - based on loop iteration pin_copy.translate(-(0.83 * height) * i, 0,-(0.83 * height) * j); // union the pin to the main brick main = main.union(pin_copy); } } // create the cylinder for the clips at the bottom - cylinder bottom = cylinder(3.267,8.0); // create the hole that it needs = cylinder bottom_cut = cylinder(2.467,8.0); // "difference" the cut to create the hole bottom = bottom.difference(bottom_cut); // move it to the correct position bottom.translateX(8.014 * (row / 2-1)); // union the first one to the brick main = main.union(bottom); for (k = 1; k < row-1; k++) { // make a copy of the bottom to create multiple - will be reused each time bottom_copy = bottom.clone(); // move... bottom_copy.translateX(-8.014 * k); // union the copy to the main brick main = main.union(bottom_copy); } // return the main brick return main; } brick(4,2).display();