A more complex primitive

I’ve been working on a slightly more complex primitive box. Not entirely sure why. Nonetheless I’ve been having some fun with the challenge of wrapping my head around a 3D object, and I’ve managed this little piece of code:

*hint, hold the mouse to see how different the box actually is than the simple primitive.


int boxSize = 40;
int boxCells = 4;
int cellSize = boxSize / boxCells;
int offset, rotateOffset = 0;

void setup() {
size(400, 400, P3D);
background(0);
stroke(255);
strokeWeight(0);
}

void draw() {
if (mousePressed) {
offset++;
} else {
if (offset > 0) { offset--; }
}
// Set stroke and background to 0 to reset page and redraw.
background(0);
// Translate to center of the page so midpoint is x=0, y=0
translate(width/2, height/2, boxSize/2);
stroke(0);
// Introduce rotateOffset that will help to rotate object on the page
rotateX(radians(rotateOffset));
rotateY(radians(rotateOffset));
// Move page over so to draw the boxes at center
translate(-(boxSize/2), -(boxSize/2), -boxSize/2);
fill(255);
for ( int i = 1; i <= 6; i++) { /* Coloring code to help debug switch (i) { case 1: fill(0, 0, 255); break; case 2: fill(255, 0, 0); break; case 3: fill(0, 255, 0); break; case 4: fill(255); stroke(0,0,255); break; case 5: stroke(255,0,0); break; case 6: stroke(0,255,0); break; default: fill(255); break; } */ for ( int j = 1; j <= boxCells; j++) { for ( int k = 1; k <= boxCells ; k++) { // Draw small 'cell' rectangle rect(0, 0, cellSize, cellSize); // Move drawing surface over translate(cellSize+offset, 0); } // Next row, move back to the first column, down one row translate(-((cellSize+offset)*boxCells), (cellSize+offset)); } // After side is finished rendering, move back to top corner translate(0, -((cellSize+offset)*boxCells)); // Move back to the center of page translate(boxSize/2, boxSize/2); if (i % 2 == 0 ) { rotateY(radians(90)); translate(-boxSize/2, 0, -boxSize/2); } else { rotateX(radians(-90)); translate(0, -boxSize/2, -boxSize/2); } translate(-boxSize/2, -boxSize/2); } rotateOffset++; }

Author: Ali

Developer and dreamer. I like to solve problems and make things come to life.

2 thoughts on “A more complex primitive”

  1. Hey Matt!

    Thanks for the comments šŸ™‚ In this particular example I basically draw a cube by cell by cell, methodically. I work on one surface, then rotate the plane and work on another surface, and again until the entire thing is drawn out. Then when you hit the mouse button it adds a offset in between the cells making the box explode. Simple really, but the difficulty was in figuring out the geometry of a cube.

    I’ll find you on the floor on Wednesday and we can talk more about it!

Leave a Reply

Your email address will not be published. Required fields are marked *