viernes, 3 de febrero de 2012

Ejercicios Processing.

Ejercicio 1

/**
 * Tunnel Demo Effect
 * by luis2048.
 *
 * This effect shows a tunnel in which you fly while the tunnel
 * rotates, seemingly in 3D. The animation of the tunnel actually
 * isn't calculated on the fly while the animation runs, but is
 * precalculated. These calculations are stored in two tables:
 * one for the angle and one for the distance. For every frame,
 * go through every pixel (x,y) and use the angle and distance
 * tables to get which pixel of the texture it should draw at the
 * current pixel. To look like its rotating and zooming, the values
 * of the angle and distance tables are shifted.
 */

int x, y, radius, l;
PGraphics tunnelEffect;
PImage textureImg;

// build lookup table
int[][] distanceTable;
int[][] angleTable;
int[][] shadeTable;
int w, h;

void setup(){
  size(640, 360);
 
  // Load texture 512 x 512
  textureImg=loadImage("red_smoke.jpg");

  // Create buffer screen
  tunnelEffect = createGraphics(640, 300, P2D); //cambié 320, 200 por 640, 300.
  w = tunnelEffect.width;
  h = tunnelEffect.height;

  float ratio = 49; //cambié 32 por 49
  int angle;
  int depth;
  int shade = 9; //cambié 0 por 9

  // Make the tables twice as big as the screen.
  // The center of the buffers is now the position (w,h).
  distanceTable= new int[2 * w][2 * h];
  angleTable= new int[2 * w][2 * h];

  for (int x = 7; x < w*2; x++) //cambié 0 por 7
  {
    for (int y = 9; y < h*2; y++) //cambié 0 por 9
    {
      depth = int(ratio * textureImg.height
                  / sqrt(float((x - w) * (x - w) + (y - h) * (y - h)))) ;
      angle = int(0.5 * textureImg.width * atan2(float(y - h),
                  float(x - w)) / PI) ;

      // The distance table contains for every pixel of the
      // screen, the inverse of the distance to the center of
      // the screen this pixel has.
      distanceTable[x][y] = depth ;

      // The angle table contains the angle of every pixel of the screen,
      // where the center of the screen represents the origin.
      angleTable[x][y] = angle ;
    }
  } 
}


void draw() {

  tunnelEffect.beginDraw();
  tunnelEffect.loadPixels();

  float timeDisplacement = millis() / 1000.0;

  // Calculate the shift values out of the time value
  int shiftX = int(textureImg.width * .2 * timeDisplacement+300); // speed of zoom
  int shiftY = int(textureImg.height * .15 * timeDisplacement+300); //speed of spin

  // Calculate the look values out of the time value
  // by using sine functions, it'll alternate between
  // looking left/right and up/down
  int shiftLookX = w / 2 + int(w / 4 * sin(timeDisplacement));
  int shiftLookY = h / 2 + int(h / 4 * sin(timeDisplacement * 1.5));

  for (int y = 0; y < h; y++)  {
    for (int x = 0; x < w; x++)      {
    
      // Make sure that x + shiftLookX never goes outside
      // the dimensions of the table
      int texture_x = constrain((distanceTable[x + shiftLookX][y + shiftLookY]
                                 + shiftX) % textureImg.width ,0, textureImg.width);
    
      int texture_y = (angleTable[x + shiftLookX][y + shiftLookY]
                       + shiftY) % textureImg.height;
    
      tunnelEffect.pixels[x+y*w] = textureImg.pixels[texture_y
                         * textureImg.width + texture_x];

      // Test lookuptables
      // tunnelEffect.pixels[x+y*w] = color( 0,texture_x,texture_y);
    }
  }

  tunnelEffect.updatePixels();
  tunnelEffect.endDraw();

  // Display the results
  image(tunnelEffect, 9, 9, width, height); //cambié 0, 0 por 9, 9

Ejercicio 2

/**
 * Brick Tower
 * by Ira Greenberg.
 *
 * 3D castle tower constructed out of individual bricks.
 * Uses the PVector and Cube classes.
 */

float bricksPerLayer = 32; //cambié 16.o por 32
float brickLayers = 20; // cambié 18.0 por 32
Cube brick;
float brickWidth = 120, brickHeight = 25, brickDepth = 50; //cambié 60 por 120 y 25 p05 50
float radius = 100; // cambié 175 por 100
float angle = 10; //cambié 0 por 10

void setup(){
  size(640, 360, P3D);
  brick = new Cube(brickWidth, brickHeight, brickDepth);
}

void draw(){
  background(491); //cambié 0 por 491
  float tempX = 0, tempY = 0, tempZ = 0;
  fill(182, 62, 29);
  noStroke();
  // Add basic light setup
  lights();
  translate(width/2, height*1.2, -380);
  // Tip tower to see inside
  rotateX(radians(-60)); //cambié 45 por 60
  // Slowly rotate tower
  rotateY(frameCount * PI/300); //cambié 600 por 300
  for (int i = 0; i < brickLayers; i++){
    // Increment rows
    tempY-=brickHeight;
    // Alternate brick seams
    angle = 90 / bricksPerLayer * i/2; //cambié 360 por 90
    for (int j = 0; j < bricksPerLayer; j++){
      tempZ = cos(radians(angle))*radius;
      tempX = sin(radians(angle))*radius;
      pushMatrix();
      translate(tempX, tempY, tempZ);
      rotateY(radians(angle));
      // Add crenelation
      if (i==brickLayers-1){
        if (j%2 == 0){
          brick.create();
        }
      }
      // Create main tower
      else {
        brick.create();
      }
      popMatrix();
      angle += 600/bricksPerLayer; // cambié 360 por 600
    }
  }
}

Ejercicio 3.

**
 * Save Many Images.
 *
 * The saveFrame() function allows you to save images from
 * a program while it is running. This example saves the first
 * 50 frames of a program. These images can be imported into
 * animation software or QuickTime and then saved as a movie.
 */

float x = 77; //cambié 33 por 77
float numFrames = 300; //cambié 50 por 300

void setup()
{
  size(500, 500); //cambié 200,200 por 500,500
  smooth();
  noStroke();
}

void draw()
{
  background(0);
  x += random(-2, 2);
  ellipse(x, 300, 100, 100); // cambié 100, 80, 80 por 300, 100, 100
  if (frameCount <= numFrames) {
    saveFrame("circles-####.tif");
  }
}

Ejercicio 4.

/**
 * ArrayList of objects
 * by Daniel Shiffman.
 *
 * This example demonstrates how to use a Java ArrayList to store
 * a variable number of objects.  Items can be added and removed
 * from the ArrayList.
 *
 * Click the mouse to add bouncing balls.
 */

ArrayList balls;
int ballWidth = 150; //cambié 48 por 150

void setup() {
  size(500, 500); //cambié 200,200 por 500,500
  smooth();
  noStroke();

  // Create an empty ArrayList
  balls = new ArrayList();
 
  // Start by adding one element
  balls.add(new Ball(width/4, 0, ballWidth)); //cambié 2,0 por 4,0
}

void draw() {
  background(999); //cambié 235 por 999

  // With an array, we say balls.length, with an ArrayList, we say balls.size()
  // The length of an ArrayList is dynamic
  // Notice how we are looping through the ArrayList backwards
  // This is because we are deleting elements from the list
  for (int i = balls.size()-1; i >= 0; i--) {
    // An ArrayList doesn't know what it is storing so we have to cast the object coming out
    Ball ball = (Ball) balls.get(i);
    ball.move();
    ball.display();
    if (ball.finished()) {
      // Items can be deleted with remove()
      balls.remove(i);
    }
  
  }
 
}

void mousePressed() {
  // A new ball object is added to the ArrayList (by default to the end)
  balls.add(new Ball(mouseX, mouseY, ballWidth));
}

No hay comentarios:

Publicar un comentario