• fullscreen
  • Cube.pde
  • Star.pde
  • cubeception.pde
  • class Cube {
      int cubeNum;
      int startSize;
      int strokeColor;
      //  int r;
      //  int g;
      //  int b;
    
    
    
      Cube(int place, int startSize, int sColor) {
        this.cubeNum = place;
        this.startSize = startSize;
        this.strokeColor = sColor;
      }
    
      void createCube() {
        /*
        this.r = int(random(70, 255));
         this.g = int(random(70, 255));
         this.b = int(random(70, 255));
         */
        pushMatrix();
        translate(width/2, height/2, -(width/2));
    
        if ((cubeNum%2) != 0) {
    
          rotateX(map(mouseY, 0, height, -PI, PI));
          rotateY(map(mouseX, 0, width, -PI, PI));
          rotateZ(map(mouseX, mouseY, height, -PI, PI));
          //stroke(r, g, b);
          stroke(255 - strokeColor/cubeNum);
          box(startSize/(pow(2, cubeNum-1)));
        }
    
        else if ((cubeNum%2) == 0) {
    
          rotateX(map(mouseY, 0, height, -PI, PI));
          rotateY(map(mouseX, 0, width, -PI, PI));
          rotateZ(map(-mouseX, -mouseY, height, -PI, PI));
          //stroke(r, g, b);
          stroke(255 - strokeColor/cubeNum);
          box(startSize/(pow(2, cubeNum-1)));
        }
        popMatrix();
      }
    
      void update() {
        pushMatrix();
        translate(width/2, height/2, -(width/2));
        if ((cubeNum%2) != 0) {
          rotateX(map(mouseY, 0, height, -PI, PI));
          rotateY(map(mouseX, 0, width, -PI, PI));
          rotateZ(map(mouseX, mouseY, height, -PI, PI));
          //stroke(r, g, b);
          stroke(255 - strokeColor/cubeNum);
          box(startSize/(pow(2, cubeNum-1)));
        }
    
        else if ((cubeNum%2) == 0) {
    
          rotateX(map(mouseY, 0, height, -PI, PI));
          rotateY(map(mouseX, 0, width, -PI, PI));
          rotateZ(map(-mouseX, -mouseY, height, -PI, PI));
          //stroke(r, g, b);
          stroke(255 - strokeColor/cubeNum);
          box(startSize/(pow(2, cubeNum-1)));
        }
        popMatrix();
      }
    
      int getVolume () {
        int volume = int(pow(startSize/pow(2, cubeNum-1), 3));
        return(volume);
      }
    }
    
    
    class Star {
      int x;
      int y;
      int w;
      int h;
      
      Star() {
        this.x = int(random(0, width));
        this.y = int(random(0, height));
        this.w = int(random(1,4));
        this.h = int(random(1,4));
      }
      
      void update() {
        fill(255);
        noStroke();
        ellipse(x, y, w, h);
      }
    }
      
    
    int numBoxes = 10;
    int startSize;
    int strokeColor = 175;
    int totalVolume = 0;
    ArrayList cubes;
    boolean showVolume = true;
    PFont f;
    Star [] stars;
    int numStars = 500;
    
    
    void setup() {
      size(600,600, P3D);
      f = loadFont("f.vlw");
      noFill();
      smooth();
      
      startSize = int((.75)*float(width));
      println(startSize);
      
      stars = new Star[numStars];    // Initializes stars
      for(int j=0;j < numStars;j++) {
        stars[j] = new Star();
      }
    
      cubes = new ArrayList();
      Cube aCube = new Cube(1, startSize, strokeColor);
      aCube.createCube();
      cubes.add(aCube);
    }
    
    void draw() {
      background(0);
      
      for(int j=0;j < numStars;j++) {
        stars[j].update();
      }
    
      noFill();
    
      textFont(f);
      textAlign(LEFT);
      text("Cubes: " + (cubes.size()), 10, 30);
      text("Volume: " + totalVolume + " pixels^3", 150, 30);
    
      totalVolume = 0;
    
      for (int i = 0; i < cubes.size(); i++) {
        Cube tempCube = (Cube) cubes.get(i);
        tempCube.update();
        cubes.set(i, tempCube);
        totalVolume = totalVolume + tempCube.getVolume();
      }
      if (showVolume) {
        println(totalVolume);
        showVolume = false;
      }
    }
    
    
    void mousePressed() {
      Cube aCube = new Cube(cubes.size()+1, startSize, strokeColor);
      aCube.createCube();
      cubes.add(aCube);
      showVolume = true;
    }
    
    void keyPressed() {
      try {
        cubes.remove(cubes.size()-1);
      }
    
      catch(Exception e) {
      }
    
      showVolume = true;
    }
    
    

    code

    tweaks (0)

    about this sketch

    This sketch is running as Java applet, exported from Processing.

    license

    advertisement

    Mandos

    Cubeception

    Add to Faves Me Likey@!
    You must login/register to add this sketch to your favorites.

    A fun little sketch I made. You can create an infinite number of imbedded cubes and see the combined volume of every cube. Each cube is half the size of the previous cube. Click to add a cube, and press any key to remove one.

    You need to login/register to comment.