/****************************** * Jonathan Seguin * Problem Set 3 * Question 3 * Last Modified March 23rd 2010 *******************************/ //Arranges dots randomly and then allows the user //to neatly arrange them into the shape of a cube //depending on cursor position relative to the x-axis class DotBox { int boxSize; // dimensions of the cube float spacing; // spacing between dots (should be represented as a decimal percentage between 0.0 and 1.0) PVector[][][] cubePlots; // points arranged into a cube PVector[][][] scramblePlots; // points scattered across the canvas float spacingConst; //variable to spread dots evenly across the float originX, originY; DotBox (int boxSize, float spacing) { this.boxSize = boxSize; this.spacing = constrain(spacing, 0.0,1.0); this.spacingConst = 1/spacing; this.cubePlots = new PVector[6][round(boxSize*spacing)][round(boxSize*spacing)]; this.scramblePlots = new PVector[6][round(boxSize*spacing)][round(boxSize*spacing)]; originX = width/2-boxSize/2; originY = height/2-boxSize/2; } //Initialized a set of co-ordinates/points into a cube. void setCube () { //front-back float spacerA=originX; float spacerB=originY; float spacerC=0; for (int side = 0; side < 6; side++){ spacerA = width/2-boxSize/2; for (int a = 0; a < round(boxSize*spacing); a++) { spacerB = height/2-boxSize/2; spacerC = 0; for (int b = 0; b < round(boxSize*spacing); b++) { if (side == 0) { cubePlots[side][a][b] = new PVector(spacerA,spacerB,0); //back side } else if (side == 1) { cubePlots[side][a][b] = new PVector(spacerA,spacerB,boxSize); //front side } else if (side == 2) { cubePlots[side][a][b] = new PVector(originX,spacerA,spacerC); //left side } else if (side == 3){ cubePlots[side][a][b] = new PVector(originX+boxSize,spacerA,spacerC);//right side } else if (side == 4) { cubePlots[side][a][b] = new PVector(spacerA,originY,spacerC);//top side } else if (side == 5) { cubePlots[side][a][b] = new PVector(spacerA,originY+boxSize,spacerC);//bottom side } spacerB+=spacingConst; spacerC+=spacingConst; } spacerA+=spacingConst; } } } // end setCube //initializes a series of co-ordinates of who's positions //are randomly generated in a constrainted 3d space. void setScramble () { for (int side = 0; side < 6; side++){ for (int a = 0; a < round(boxSize*spacing); a++) { for (int b = 0; b < round(boxSize*spacing); b++) { scramblePlots[side][a][b] = new PVector (random(0,width), random(0,height), random(-width,width)); } } } }//end setScramble //Draws points at the cube co-ordinates //only used to test whether or not my variable initialization was working. void drawCube() { for (int side = 0; side < 6; side++){ for (int a = 0; a < round(boxSize*spacing); a++) { for (int b = 0; b < round(boxSize*spacing); b++) { point (cubePlots[side][a][b].x,cubePlots[side][a][b].y,cubePlots[side][a][b].z); } } } }//end drawCube //Enables scrambled points to re-arrange and form into a cube. //Looks at both the cube co-ordinates and the scrambled co-ordinates. //The closer the mouse cursor is to the left, the closer the points will //be drawn to the scrambled set of co-ordinates. The closer the cursor is //to the right, the closer the points will be drawn to the cube set of //co-ordinates. void formCube () { for (int side = 0; side < 6; side++){ for (int a = 0; a < round(boxSize*spacing); a++) { for (int b = 0; b < round(boxSize*spacing); b++) { if (side == 0) { stroke (255,0,0); // back colour } else if (side == 1){ stroke (0,255,0); // front colour } else if (side == 2){ stroke (0,0,255); // left colour } else if (side == 3){ stroke (255,0,255); // right colour } else if (side == 4){ stroke (255,255,0); // top colour } else if (side == 5){ stroke (0,255,255); // bottom colour } point (map (mouseX, 0, width, scramblePlots[side][a][b].x, cubePlots[side][a][b].x), map (mouseX, 0, width, scramblePlots[side][a][b].y, cubePlots[side][a][b].y), map (mouseX, 0, width, scramblePlots[side][a][b].z, cubePlots[side][a][b].z)); } } } } }//end DotBox //Output //------------------------------------------------------- DotBox boxy; void setup () { size (400,400,P3D); boxy = new DotBox (100,0.4); boxy.setCube(); boxy.setScramble(); } void draw() { background(0); camera(width/2+100, height/2-200, 300, width/2-40, height/2+40, 0, 0.0, 1.0, 0.0); boxy.formCube(); }