2013年5月31日 星期五

[對照]蝦爸的p5範例 8-2 繼承應用

讓音樂的音量,影響球體上節點離中心點的距離。

[Processing code] ↓

float r;
float theta;
float phi;
ArrayList group;

import ddf.minim.*;

Minim minim;
AudioPlayer player;

void setup() {
  size(600, 300, OPENGL);
  group=new ArrayList();
  r=200;
  theta=0;
  phi=0;

  minim = new Minim(this);
  player = minim.loadFile("topas.wav"); 
  player.loop();
}


void draw() {
  background(0);
  //camera(mouseX, -mouseY, 400, 0, 0, 0, 0, 1, 0);
  camera(500, -300, 400, 0, 0, 0, 0, 1, 0);
  for (int i=0;i<group.size();i++) {

    ((Music)group.get(i)).update();
    if ( ((Music)group.get(i)).life==0) {
      group.remove(i);
    }
  }

  for (int i=0;i<group.size();i++) {
    for (int j=i+1;j<group.size();j++) {
      ((Music)group.get(i)).MusicGraphic((player.left.level()+player.right.level())/2);

      Music aa=((Music)group.get(i));
      Music bb=((Music)group.get(j));

      float t_x1=aa.get_x();
      float t_y1=aa.get_y();
      float t_z1=aa.get_z();

      float t_x2=bb.get_x();
      float t_y2=bb.get_y();
      float t_z2=bb.get_z();

      if (Distance( t_x1, t_x2, t_y1, t_y2, t_z1, t_z2) < 50) {
        stroke(150);
        strokeWeight(1);
        line(t_x1, t_y1, t_z1, t_x2, t_y2, t_z2);
      }
    }
  }
}

void mousePressed() {
  for (int i=0;i<5;i++) {
    group.add(new Music(r, 0, 0, color(0, 0, 200, 100)));
  }
}


class Music extends e_point {
  float add_r;
  float T_r;
  Music(float e_r, float e_theta, float e_phi, color e_c ) {
    r=e_r;
    theta=e_theta;
    phi=e_phi;
    cc=e_c;
    v_t=random(0, 1);
    v_p=random(0, 1);
    life=(int)random(500, 1000);
    size=random(5, 20);
    add_r=random(1,r);
  }

  void MusicGraphic(float vol) {
    float px=(r+vol*add_r)*cos(radians(theta))*cos(radians(phi));
    float py=(r+vol*add_r)*sin(radians(phi));
    float pz=(r+vol*add_r)*sin(radians(theta))*cos(radians(phi));
    T_r = (r+vol*add_r);
    stroke(cc);
    strokeWeight(size*0.5);
    point(px, py, pz);
  }
  float get_x() {
    return (T_r)*cos(radians(theta))*cos(radians(phi));
  }
  float get_y() {
    return (T_r)*sin(radians(phi));
  }
  float get_z() {
    return (T_r)*sin(radians(theta))*cos(radians(phi));
  }
  
}

class e_point {

  float r, theta, phi;
  color cc;
  float v_t, v_p;
  int life;
  float size;

  e_point() {
  }
  e_point(float e_r, float e_theta, float e_phi, color e_c ) {
    r=e_r;
    theta=e_theta;
    phi=e_phi;
    cc=e_c;
    v_t=random(0, 1);
    v_p=random(0, 1);
    life=(int)random(500, 1000);
    size=random(5, 20);
  }

  void graphic() {
    float px=r*cos(radians(theta))*cos(radians(phi));
    float py=r*sin(radians(phi));
    float pz=r*sin(radians(theta))*cos(radians(phi));
    stroke(cc);
    strokeWeight(1);
    point(px, py, pz);
  }

  void update() {
    theta=theta+v_t;
    phi=phi+v_p;
    life--;
  }

  float get_x() {
    return r*cos(radians(theta))*cos(radians(phi));
  }
  float get_y() {
    return r*sin(radians(phi));
  }
  float get_z() {
    return r*sin(radians(theta))*cos(radians(phi));
  }
}


float Distance(float x1, float x2, float y1, float y2, float z1, float z2) {
  float Temp;
  Temp= sqrt(sq(x1-x2)+sq(y1-y2)+sq(z1-z2));
  return Temp;
}