【互动媒体技术】 编程习作

代码本色零到四章内容
0噪声
1向量
2力
3震荡
4粒子爆炸

第零章
本习作是对Perlin噪声的拓展应用,通过控制噪声随时间的平滑变化规律,辅以色彩的亮度变化表现大小关系。

void setup(){
  size(600,600,P3D);
  background(0);
  fill(255,100);
  noStroke();
  //lights();
  //noiseSeed(1);
  noiseDetail(3);

}

float t=0;  
float theta = 0.0;
void draw(){
  t++;
  clear();
  pushMatrix();
  rotateX(PI/5);
  rotateZ(theta);
  drawbox();
  popMatrix();
  theta += 0.0025;
}

void drawbox(){ 
  for(int x=-width;x<width;x=x+50){
    for(int y=-height;y<height;y=y+50){
        float n=noise(0.02*x,0.02*y,0.02*(t));
        translate(x,y,0);
        fill(n*255,n*255,255);
        box(n*70); 

效果
在这里插入图片描述
第一章 向量

代码`Mover[] movers=new Mover[10];

void setup() {
size(600,600);
smooth();
background(0);
for (int i=0;i<movers.length;i++){
movers[i]=new Mover();
}
}

void draw() {
for (int i=0;i<movers.length;i++){
movers[i].update();
movers[i].checkEdges();
movers[i].display();
}
}

//Mover类
class Mover {
PVector location;
PVector velocity;
PVector acceleration;
float topspeed;
int a=int(random(255));
int b=int(random(255));
int c=int(random(255));

Mover() {
location = new PVector(random(width),random(height));
velocity =new PVector(random(1),random(1));
topspeed = 20;
}

void update() {
noStroke();
fill(0,10);
rect(0,0,width,height);
PVector mouse = new PVector(mouseX,mouseY);
PVector dir = PVector.sub(mouse,location);
dir.normalize();
dir.mult(0.2);
acceleration=dir;
velocity.add(acceleration);
velocity.limit(topspeed);
location.add(velocity);
}

void display() {
noStroke();
fill(a,b,c);
ellipse(location.x,location.y,20,20);
}

void checkEdges(){
if(location.x>width){
location.x=width;
velocity.x*=-1;
}else if (location.x<0){
location.x=0;
velocity.x*=-1;
}
if (location.y>height){
velocity.y*=-1;
location.y=height;
}else if (location.y<0){
location.y=0;
location.y*=-1;
}
}
}`
屏幕上随机位置生成圆,每个小球都具有一个朝向鼠标的加速度,当运动到图片边缘时被反弹,速度方向改变,但加速度方向仍朝向鼠标。给所有小球设置一个最大速度,如果这个速度设置的过小,那么很快这些小球就会全部达到这个速度并逐渐重合,
在这里插入图片描述
第二章 力

void setup(){
  size(600,400);
}
void draw(){
  colorMode(HSB,360,100,100);
  background(0,40,100);
  float w = 120;
  float l = w/3*5;
  DaBai db = new DaBai(w,l);
  db.leftArm.a=atan2(mouseY,mouseX);
  db.rightArm.a=PI-atan2(mouseY,width-mouseX);
  if((mouseX-pmouseX)>5){
    db.leftLeg.a=PI/4;db.rightLeg.a=PI/4;
  }else if((mouseX-pmouseX)<-5){
    db.leftLeg.a=-PI/4;db.rightLeg.a=-PI/4;
  }else{
    db.leftLeg.a=0;db.rightLeg.a=0;
  }

  stroke(30,75,75);strokeWeight(2);
  line(0,0,mouseX-db.body.w-db.leftArm.l*cos(db.leftArm.a),mouseY-db.body.l/2-db.leftArm.l*sin(db.leftArm.a));
  line(width,0,mouseX+db.body.w-db.rightArm.l*cos(db.rightArm.a),mouseY-db.body.l/2-db.rightArm.l*sin(db.rightArm.a));
  
  pushMatrix();
  translate(mouseX,mouseY);
  db.display();
  popMatrix();
}

class DaBai{
  float w,l;
  Head head;
  Body body;
  Arm leftArm;
  Arm rightArm;
  Leg leftLeg;
  Leg rightLeg;
  DaBai(float w,float l){
    this.w=w;this.l=l;
    head = new Head(w/3.5);
    body = new Body(l/3);
    leftArm = new Arm(l/5,0);
    rightArm = new Arm(l/5,0);
    leftLeg = new Leg(l/6,0);
    rightLeg = new Leg(l/6,0);
  }
  void display(){
    ellipseMode(RADIUS);
    // left arm
    pushMatrix();
    translate(-body.w,-body.l/2);
    rotate(leftArm.a);
    leftArm.display();
    popMatrix();
    // right arm
    pushMatrix();
    translate(body.w,-body.l/2);
    rotate(rightArm.a);
    rightArm.display();
    popMatrix();
    // left leg
    pushMatrix();
    translate(-body.w/3,body.l/1.25);
    rotate(leftLeg.a);
    leftLeg.display();
    popMatrix();
    // right leg
    pushMatrix();
    translate(body.w/3,body.l/1.25);
    rotate(rightLeg.a);
    rightLeg.display();
    popMatrix();
    // body
    body.display();
    // head
    pushMatrix();
    translate(0,-body.l);
    head.display();
    popMatrix();
  } 
}
class Head{
  float w,l;
  Head(float w){
    this.w=w;l=w*2/3;
  }
  void display(){
    ellipseMode(RADIUS);
    fill(0,0,100);stroke(0,0,0);strokeWeight(2);
    ellipse(0,0,w,l);
    fill(0,0,0);stroke(0,0,0);
    ellipse(-w/5*2,-l/10,l/10,l/10);
    ellipse(w/5*2,-l/10,l/10,l/10);
    noFill();stroke(0,0,0);strokeWeight(2);
    bezier(-w/5*2,-l/10,0,l/20,0,l/20,w/5*2,-l/10);
    fill(20,80,80);stroke(20,80,80);strokeWeight(2);
    ellipse(-w/5*3,l/5,l/10,l/15);
    ellipse(w/5*3,l/5,l/10,l/15);
  } 
}
class Body{
  float w,l;
  Body(float l){
    this.l=l;w=l*1/1.2;
  }
  void display(){
    ellipseMode(RADIUS);
    fill(0,0,100);stroke(0,0,0);strokeWeight(2);
    ellipse(0,0,w,l);
    fill(30,10,100);stroke(0,10,60);strokeWeight(1);

    ellipse(w/5*2,-l/5*2,w/8,w/8);
    beginShape();
    vertex(w/5*2-w/8,-l/5*2);
    vertex(w/5*2-w/16,-l/5*2);
    vertex(w/5*2-w/16,-l/5*2-w/16);
    vertex(w/5*2+w/16,-l/5*2-w/16);
    vertex(w/5*2+w/16,-l/5*2);
    vertex(w/5*2+w/8,-l/5*2);
    endShape();
  }
}
class Arm{
  float w,l;
  float a;
  Arm(float w,float a){
    this.w=w;l=w*1/3;this.a=a;
  }
  void display(){
    ellipseMode(RADIUS);
    fill(0,0,100);stroke(0,0,0);strokeWeight(2);
    ellipse(0,0,w,l);
  } 
}
class Leg{
  float w,l;
  float a;
  Leg(float l,float a){
    this.l=l;w=l*1/2;this.a=a;
  }
  void display(){
    ellipseMode(RADIUS);
    fill(0,0,100);stroke(0,0,0);strokeWeight(2);
    ellipse(0,0,w,l);
  } 
}

在这里插入图片描述
第三章 震荡


```cpp
Wave[] waves=new Wave[20];

void setup(){
  size(600,600);
  for (int i=0;i<waves.length;i++)
  {  
   waves[i]=new Wave(new PVector(0,300),600,int(random(300)));
  }
   background(0);
}

void draw(){
   background(0);
  for (int i=0;i<waves.length;i++){
    waves[i].calculate();
    waves[i].display();
  } 
}

//Wave类
class Wave { 
  int xspacing =int(random(2,10)); 
  int w;
  int r=int(random(100,255));
  int g=int(random(100,255));
  int b=int(random(100,255));
  PVector origin;         
  float theta = 0.0;       
  float amplitude;       
  float period;         
  float dx;            
  Particle[] particles;
  float a;
  float t=random(1);
  
  Wave(PVector o, int w_,float p) {
    origin = o;
    w = w_;
    period = p; 
    dx = (TWO_PI / period) * xspacing;
    particles = new Particle[w/xspacing];
     if(t<0.7){
    a=random(100);
  }
  else{
    a=random(200,300);
  }
    amplitude =a;
    for (int i = 0; i < particles.length; i++) {
      particles[i] = new Particle(); 
    }
  }
  
  void calculate() {
    theta += random(0.05);
    float x = theta;
    for (int i = 0; i < particles.length; i++) {
      particles[i].setposition(origin.x+i*xspacing,origin.y+sin(x)*amplitude);
      x+=dx;
    }
  }
  void display() {
    for (int i = 0; i < particles.length; i++) {
      particles[i].display(r,g,b);
    }
  }
}

//Particle类
class Particle {
  PVector position;
  
  Particle() {
    position = new PVector(); 
  }
  
  void setposition(float x, float y) {
    position.x = x;
    position.y = y; 
  }
  
  void display(int r,int g,int b) {    
    noStroke();
    fill(r,g,b);
    ellipse(position.x,position.y,2,2); 
  } 
}

效果图在这里插入图片描述

第四章  粒子爆炸
鼠标点击生成粒子爆炸效果,按住鼠标拖动,会生成一连串的粒子,之前鼠标点击出现的粒子会被清空,每次点击出现的爆炸粒子颜色都是随机的。
```cpp
ArrayList<ParticleSystem> systems;
boolean t;

void setup() {
  size(600,600);
  background(0);
  systems = new ArrayList<ParticleSystem>();
}

void draw() {
  fill(0,20);
  rect(0,0,width,height);
  for (ParticleSystem ps: systems) {
    if(t){
      ps.origin.set(mouseX,mouseY,0);
    }
    ps.run();
    ps.addParticle(); 
  }
}
void mousePressed() {
  systems.add(new ParticleSystem(1,new PVector(mouseX,mouseY)));
}
void mouseDragged(){
  t=true;
}
void mouseReleased(){
  t=false;
}

//ParticleSystem类
class ParticleSystem {
  ArrayList<Particle> particles;   
  PVector origin;       
  PVector  q;
  int r=int(random(255));
  int g=int(random(255));
  int b=int(random(255));
  ParticleSystem(int num, PVector v) {
    particles = new ArrayList<Particle>();   
    origin = v;  
    q=new PVector(mouseX,mouseY);  
    for (int i = 0; i < num; i++) {
     particles.add(new Particle(origin,r,g,b));   
    }  
  }
  void run() {
    for (int i = particles.size()-1; i >= 0; i--) {
      Particle p = particles.get(i);
      p.run();
      if (p.isDead()) {
        particles.remove(i);
      }
    }
  }
  void addParticle() {
   particles.add(new Particle(origin,r,g,b));
  }
  boolean dead() {
    if (particles.isEmpty()) {
      return true;
    } 
    else {
      return false;
    }
  }
}

//Particle类
class Particle {
  PVector position;
  PVector velocity;
  PVector acceleration;
  float lifespan;
  int r,g,b;
 
  Particle(PVector l,int R,int G,int B) {
    acceleration = new PVector(0,0.05);
    velocity = new PVector(random(-4,4),random(-4,1));
    position = l.get();
    lifespan = 255.0;
    r=R;
    g=G;
    b=B;
  }
  
  void run() {
    update();
    display();
  }
  
  void update() {
    velocity.add(acceleration);
    position.add(velocity);
    lifespan -= 2.0; 
  }
  
  void display() {
    noStroke();
    fill(r,g,b,lifespan);
    ellipse(position.x,position.y,3,3);
  }  
  
  boolean isDead() {
    if (lifespan < 0.0) {
      return true;
    } else {
      return false;
    }
  }

效果图
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值