代码本色示例学习

随机

在这里插入图片描述

float increment = 0.01;
float zoff = 0.0;  
float zincrement = 0.02; 

void setup() {
  size(200,200);
}

void draw() {
  background(0);
  
  loadPixels();

  float xoff = 0.0; 
  
  for (int x = 0; x < width; x++) {
    xoff += increment;   
    float yoff = 0.0;   
    for (int y = 0; y < height; y++) {
      yoff += increment; 
      
      float bright = noise(xoff,yoff,zoff)*255;

      pixels[x+y*width] = color(bright,bright,bright);
    }
  }
  updatePixels();
  
  zoff += zincrement; // Increment zoff
}

向量

在这里插入图片描述

Mover mover;

void setup() {
  size(200,200);
  
  mover = new Mover();
}

void draw() {
  background(255);
  
  mover.update();
  mover.checkEdges();
  mover.display();
}

class PVector {
  float x;
  float y;
  
  PVector(float x_,float y_) {
    x = x_;
    y = y_;
  }
  
  void add(PVector v) {
    x = x + v.x; 
    y = y + v.y;
  }
  
  void sub(PVector v) {
    x = x - v.x;
    y = y - v.y;
  }
  
  void mult(float n) {
    x = x * n;
    y = y * n;
  }
  
  void div(float n) {
    x = x / n;
    y = y / n;
  }
  
  float mag() {
     return sqrt(x * x + y * y); 
  }
  
  void normalize() {
    float m = mag();
    if(m != 0) {
      div(m);  
    }
  }
  
  void limit(float max) {
    if(mag() > max) {
      normalize();
      mult(max);
    }
  }
}

class Mover {
  PVector location;
  PVector velocity;
  PVector acceleration;
  float topspeed;
  
  Mover() {
    location = new PVector(random(width),random(height));
    velocity = new PVector(0,0);
    acceleration = new PVector(-0.001,0.01);
    topspeed = 10;
  }
  
  void update() {
    velocity.add(acceleration);
    velocity.limit(topspeed);
    location.add(velocity); 

    println(velocity.mag());
  }
  
  void display() {
    stroke(0);
    fill(175);
    ellipse(location.x,location.y,16,16);
  }
  
  void checkEdges() {
    if(location.x > width ) {
      location.x = 0; 
    } else if(location.x < 0) {
      location.x = width; 
    }
    
    if(location.y > height) {
      location.y = 0; 
    } else if(location.y < 0) {
      location.y = height; 
    }
  }
}

在这里插入图片描述

Mover[] movers = new Mover[20];

void setup() {
  size(383,200);
  randomSeed(1);
  for(int i = 0; i < movers.length;i++) {
    movers[i] = new Mover(random(1,4),0,0); 
  }
}

void draw() {
  background(255);
  
  for(int i = 0; i < movers.length;i++) {
    PVector wind = new PVector(0.01,0);
    PVector gravity = new PVector(0,0.1 * movers[i].mass);
    
    // 摩擦戏数
    float c = 0.05;
    // 正向力
    float normal = 1;
    
    PVector friction = movers[i].velocity.get();
    friction.mult(-1);  // 乘以 -1
    friction.normalize(); // 归一化    
    friction.mult(c);  // 乘以摩擦戏数
    friction.mult(normal);  // 乘以正向力
       
    
    if(movers[i].location.x > width / 2) {
      movers[i].applyForce(friction);  
    }
    
    movers[i].applyForce(wind);
    movers[i].applyForce(gravity);
  
    movers[i].update();
    movers[i].display();
    movers[i].checkEdges();
  }
  

}

class PVector {
  float x;
  float y;
  
  PVector(float x_,float y_) {
    x = x_;
    y = y_;
  }
  
  PVector get() {
    PVector newVector = new PVector(x,y);
    return newVector;
  }
  
  void add(PVector v) {
    x = x + v.x; 
    y = y + v.y;
  }
  
  void sub(PVector v) {
    x = x - v.x;
    y = y - v.y;
  }
  
  void mult(float n) {
    x = x * n;
    y = y * n;
  }
  
  void div(float n) {
    x = x / n;
    y = y / n;
  }
  
  float mag() {
     return sqrt(x * x + y * y); 
  }
  
  void normalize() {
    float m = mag();
    if(m != 0) {
      div(m);  
    }
  }
  
  void limit(float max) {
    if(mag() > max) {
      normalize();
      mult(max);
      
      println(mag());
    }
  }
}

class Mover {
  PVector location;
  PVector velocity;
  PVector acceleration;
  float mass;
  
  color c;
  
  Mover(float m,float x,float y) {
    location = new PVector(x,y);
    velocity = new PVector(0,0);
    acceleration = new PVector(0,0);
    mass = m;
    c = color(random(255),random(255),random(255));
  }
  
  void applyForce(PVector force) {
    PVector newVector = force.get();
    newVector.div(mass);
    acceleration.add(newVector);
  }
  
  void update() {
    velocity.add(acceleration);
    location.add(velocity);
    acceleration.mult(0);
  }
  
  void display() {
    stroke(0);
    strokeWeight(2);
    fill(c);
    //fill(random(255),random(255),random(255));
    ellipse(location.x,location.y,mass * 16,mass * 16);
  }
  
  void checkEdges() {
    if(location.x > width) {
      location.x = width;
      velocity.x *= -1;
    } else if(location.x < 0) {
      velocity.x *= -1;
      location.x = 0;
    }
    
    if(location.y > height) {
      velocity.y *= -1;
      location.y = height;
    }
  }
}

震荡

在这里插入图片描述

Oscillator[] oscillators = new Oscillator[200];

void setup() {
  size(800,200);
  background(255);
  for(int i = 0; i < oscillators.length; i++) {
    oscillators[i] = new Oscillator(); 
  }  
}

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

class Oscillator {
  PVector angle;
  PVector velocity;
  PVector amplitude;
  
  color c;
  
  Oscillator() {
    angle = new PVector();
    velocity = new PVector(random(-0.05,0.05),random(-0.05,0.05));
    amplitude = new PVector(random(width / 2),random(height / 2));
    
    c = color(random(255),random(255),random(255));
  }
  
  void oscillate() {
    angle.add(velocity); 
  }
  
  void display() {
    float x = sin(angle.x) * amplitude.x;
    float y = sin(angle.y) * amplitude.y;
    
    pushMatrix();
    translate(width / 2,height / 2);
    stroke(0);
    fill(c);
    line(0,0,x,y);
    ellipse(x,y,16,16);
    popMatrix();
  }
}

class PVector {
  float x;
  float y;
  
  PVector() {
    x = 0;
    y = 0;
  }
  
  PVector(float x_,float y_) {
    x = x_;
    y = y_;
  }
  
  PVector get() {
    PVector newVector = new PVector(x,y);
    return newVector;
  }
  
  void add(PVector v) {
    x = x + v.x; 
    y = y + v.y;
  }
  
  void sub(PVector v) {
    x = x - v.x;
    y = y - v.y;
  }
  
  void mult(float n) {
    x = x * n;
    y = y * n;
  }
  
  void div(float n) {
    x = x / n;
    y = y / n;
  }
  
  float mag() {
     return sqrt(x * x + y * y); 
  }
  
  void normalize() {
    float m = mag();
    if(m != 0) {
      div(m);  
    }
  }
  
  void limit(float max) {
    if(mag() > max) {
      normalize();
      mult(max);
    }
  }
  
  float heading2D() {
    return atan2(y,x); 
  }
}

粒子系统

在这里插入图片描述

import java.util.Iterator;

ParticleSystem ps;

void setup() {
  size(800,200);
  ps = new ParticleSystem(new PVector(width / 2,50));
}

void draw() {
  background(255);
  ps.addParticle();
  ps.run();
}

class ParticleSystem {
  ArrayList<Particle> particles;
  PVector origin;
  
  ParticleSystem(PVector location) {
    origin = location.get();
    particles = new ArrayList<Particle>();
  }
  
  void addParticle() {
    float r = random(1);
    if(r < 0.5) {
      particles.add(new Particle(origin)); 
    } else {
      particles.add(new Confetti(origin));
    }
  }
  
  void run() {
    Iterator<Particle> it = particles.iterator();
    while(it.hasNext()) {
      Particle p = it.next();
      p.run();
      if(p.isDead()) {
        it.remove(); 
      }
    }
  }
}

class Particle {
  PVector location;
  PVector velocity;
  PVector acceleration;
  
  float lifespan;
  
  Particle(PVector l) {
    acceleration = new PVector(0,0.05);
    velocity = new PVector(random(-1,1),random(-2,0));
    location = l.get();
    lifespan = 255.0;
  }
  
  void run() {
    update();
    display();
  }
  
  void update() {
    velocity.add(acceleration);
    location.add(velocity);
    lifespan -= 2.0;
  }
  
  void display() {
    stroke(0,lifespan);
    strokeWeight(2);
    fill(127,lifespan);
    ellipse(location.x,location.y,12,12);
  }
  
  boolean isDead() {
    if(lifespan < 0.0) {
      return true; 
    } else {
      return false; 
    }
  }
}

class Confetti extends Particle {
  Confetti(PVector l) {
    super(l); 
  }
  
  void display() {
    rectMode(CENTER);
    fill(127,lifespan);
    stroke(0,lifespan);
    strokeWeight(2);
    pushMatrix();
    translate(location.x,location.y);
    float theta = map(location.x,0,width,0,TWO_PI * 2);
    rotate(theta);
    rect(0,0,12,12);
    popMatrix();
  }
}

class PVector {
  float x;
  float y;
  
  PVector(float x_,float y_) {
    x = x_;
    y = y_;
  }
  
  PVector get() {
    PVector newVector = new PVector(x,y);
    return newVector;
  }
  
  void add(PVector v) {
    x = x + v.x; 
    y = y + v.y;
  }
  
  void sub(PVector v) {
    x = x - v.x;
    y = y - v.y;
  }
  
  void mult(float n) {
    x = x * n;
    y = y * n;
  }
  
  void div(float n) {
    x = x / n;
    y = y / n;
  }
  
  float mag() {
     return sqrt(x * x + y * y); 
  }
  
  void normalize() {
    float m = mag();
    if(m != 0) {
      div(m);  
    }
  }
  
  void limit(float max) {
    if(mag() > max) {
      normalize();
      mult(max);
    }
  }
  
  float heading2D() {
    return atan2(y,x); 
  }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值