Built with Processing

読んでても書いてみないことには上達しないです。

個人的に面白いと思った箇所をピックアップ。

(1)カメラの取り扱い

カメラからピクセルを取り出し、値を変換するアプリケーション

import processing.video.*;

Capture Camera;

void setup() {
  size(320, 240);
  //size(800,800);
  colorMode(HSB, 256);
  noFill();
  strokeWeight(2);

  //Camera = new Capture(this,320,240,12);
  Camera = new Capture(this, width, height, 12);
}

void draw() {
  //loadPixels();
  Camera.loadPixels();

  for (int y=0;y<height;y+=10) {
    for (int x=0;x<width;x+=10) {
      int pos = (y*width)+x;
      //pixels[pos] = Camera.pixels[pos];
      color c = Camera.pixels[pos];

      float h = hue(c);
      float s = saturation(c);
      float b = 20 * brightness(c) / 256;

      stroke(h, s, 256);
      ellipse(x, y, b, b);
    }
  }
  //updatePixels();
  //image(Camera,0,0);
}

void captureEvent(Capture camera) {
  camera.read();
}

差分を抽出して処理を施すアプリケーション

import processing.video.*;

Capture Camera;
PImage Pcamera;

void setup(){
  //size(320,240);
  size(800,800);
  frameRate(10);
  stroke(255);
  
  Camera = new Capture(this,width,height,15);
  Pcamera = new PImage(width,height);
  
}

void draw(){
  Pcamera.loadPixels();
  
  tint(255,64);
  image(Camera,0,0);
  
  for(int y=0;y<height;y+=5){
    for(int x=0;x<width;x+=5){
      int pos = (y*width)+x;
      float diff = abs(brightness(Pcamera.pixels[pos])-
        brightness(Camera.pixels[pos]));
      if(diff>50){
        fill(Camera.pixels[pos]);
        rect(x,y,diff/5,diff/5);
      }
    }
  }
  
  Pcamera.copy(Camera,0,0,width,height,0,0,width,height);
}
void captureEvent(Capture camera){
  camera.read();
}


(2)TwitterXMLをパースして表示(XML Elementクラス)

//Twitter public timeline URL
String PUBLIC_TIMELINE = "http://api.twitter.com/1/statuses/public_timeline.xml";

XMLElement Xml;
int TweetCount;
PImage[] UserIcon;
String[] Text;
float[] X,Y,Z;

void setup(){
  size(400,400,P3D);
  background(0);
  imageMode(CENTER);
  frameRate(30);
  
  Xml = new XMLElement(this,PUBLIC_TIMELINE);
  TweetCount = Xml.getChildCount();
  
  UserIcon = new PImage[TweetCount];
  Text = new String[TweetCount];
  X = new float[TweetCount];
  Y = new float[TweetCount];
  Z = new float[TweetCount];
  
  for(int i = 0;i < TweetCount ;i++){
    XMLElement status = Xml.getChild(i);
    String user_image_url = status.getChild("user/profile_image_url").getContent();
    UserIcon[i]=loadImage(user_image_url);
    Text[i] = status.getChild("text").getContent();
    X[i]=random(width);
    Y[i]=random(height);
    Z[i]=-100-i*150;
    
  }
}

void draw(){
  background(0);
  for(int i=0;i<TweetCount;i++){
    pushMatrix();
    
    Z[i] +=2;
    if(Z[i]>50){
      X[i]=random(width);
      Y[i]=random(height);
      Z[i]=-2000;
    }
    
    translate(X[i],Y[i],Z[i]);
    float opacity;
    if(Z[i]<0){
      opacity = min(100,map(Z[i],-1500,0,0,150))/100;
    } else {
      opacity = map(Z[i],0,50,100,0)/100;
    }
    
    tint(255,255*opacity);
    image(UserIcon[i],0,0,50,50);
    
    noStroke();
    fill(255,255*opacity);
    rect(-100,-110,200,75);
    triangle(-10,-35,0,-25,10,-35);
    
    fill(0,255*opacity);
    textSize(10);
    text(Text[i],-95,-105,190,65);
    
    popMatrix();
  }
}