float increment =0.01;float zoff =0.0;float zincrement =0.02;voidsetup(){size(200,200);}voiddraw(){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;voidsetup(){size(200,200);
mover =newMover();}voiddraw(){background(255);
mover.update();
mover.checkEdges();
mover.display();}classPVector{float x;float y;PVector(float x_,float y_){
x = x_;
y = y_;}voidadd(PVector v){
x = x + v.x;
y = y + v.y;}voidsub(PVector v){
x = x - v.x;
y = y - v.y;}voidmult(float n){
x = x * n;
y = y * n;}voiddiv(float n){
x = x / n;
y = y / n;}floatmag(){returnsqrt(x * x + y * y);}voidnormalize(){float m =mag();if(m !=0){div(m);}}voidlimit(float max){if(mag()> max){normalize();mult(max);}}}classMover{PVector location;PVector velocity;PVector acceleration;float topspeed;Mover(){
location =newPVector(random(width),random(height));
velocity =newPVector(0,0);
acceleration =newPVector(-0.001,0.01);
topspeed =10;}voidupdate(){
velocity.add(acceleration);
velocity.limit(topspeed);
location.add(velocity);println(velocity.mag());}voiddisplay(){stroke(0);fill(175);ellipse(location.x,location.y,16,16);}voidcheckEdges(){if(location.x > width ){
location.x =0;}elseif(location.x <0){
location.x = width;}if(location.y > height){
location.y =0;}elseif(location.y <0){
location.y = height;}}}
力
Mover[] movers =newMover[20];voidsetup(){size(383,200);randomSeed(1);for(int i =0; i < movers.length;i++){
movers[i]=newMover(random(1,4),0,0);}}voiddraw(){background(255);for(int i =0; i < movers.length;i++){PVector wind =newPVector(0.01,0);PVector gravity =newPVector(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();}}classPVector{float x;float y;PVector(float x_,float y_){
x = x_;
y = y_;}PVectorget(){PVector newVector =newPVector(x,y);return newVector;}voidadd(PVector v){
x = x + v.x;
y = y + v.y;}voidsub(PVector v){
x = x - v.x;
y = y - v.y;}voidmult(float n){
x = x * n;
y = y * n;}voiddiv(float n){
x = x / n;
y = y / n;}floatmag(){returnsqrt(x * x + y * y);}voidnormalize(){float m =mag();if(m !=0){div(m);}}voidlimit(float max){if(mag()> max){normalize();mult(max);println(mag());}}}classMover{PVector location;PVector velocity;PVector acceleration;float mass;
color c;Mover(float m,float x,float y){
location =newPVector(x,y);
velocity =newPVector(0,0);
acceleration =newPVector(0,0);
mass = m;
c =color(random(255),random(255),random(255));}voidapplyForce(PVector force){PVector newVector = force.get();
newVector.div(mass);
acceleration.add(newVector);}voidupdate(){
velocity.add(acceleration);
location.add(velocity);
acceleration.mult(0);}voiddisplay(){stroke(0);strokeWeight(2);fill(c);//fill(random(255),random(255),random(255));ellipse(location.x,location.y,mass *16,mass *16);}voidcheckEdges(){if(location.x > width){
location.x = width;
velocity.x *=-1;}elseif(location.x <0){
velocity.x *=-1;
location.x =0;}if(location.y > height){
velocity.y *=-1;
location.y = height;}}}
震荡
Oscillator[] oscillators =newOscillator[200];voidsetup(){size(800,200);background(255);for(int i =0; i < oscillators.length; i++){
oscillators[i]=newOscillator();}}voiddraw(){background(255);for(int i =0; i < oscillators.length; i++){
oscillators[i].oscillate();
oscillators[i].display();}}classOscillator{PVector angle;PVector velocity;PVector amplitude;
color c;Oscillator(){
angle =newPVector();
velocity =newPVector(random(-0.05,0.05),random(-0.05,0.05));
amplitude =newPVector(random(width /2),random(height /2));
c =color(random(255),random(255),random(255));}voidoscillate(){
angle.add(velocity);}voiddisplay(){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();}}classPVector{float x;float y;PVector(){
x =0;
y =0;}PVector(float x_,float y_){
x = x_;
y = y_;}PVectorget(){PVector newVector =newPVector(x,y);return newVector;}voidadd(PVector v){
x = x + v.x;
y = y + v.y;}voidsub(PVector v){
x = x - v.x;
y = y - v.y;}voidmult(float n){
x = x * n;
y = y * n;}voiddiv(float n){
x = x / n;
y = y / n;}floatmag(){returnsqrt(x * x + y * y);}voidnormalize(){float m =mag();if(m !=0){div(m);}}voidlimit(float max){if(mag()> max){normalize();mult(max);}}floatheading2D(){returnatan2(y,x);}}
粒子系统
import java.util.Iterator;ParticleSystem ps;voidsetup(){size(800,200);
ps =newParticleSystem(newPVector(width /2,50));}voiddraw(){background(255);
ps.addParticle();
ps.run();}classParticleSystem{
ArrayList<Particle> particles;PVector origin;ParticleSystem(PVector location){
origin = location.get();
particles =newArrayList<Particle>();}voidaddParticle(){float r =random(1);if(r <0.5){
particles.add(newParticle(origin));}else{
particles.add(newConfetti(origin));}}voidrun(){
Iterator<Particle> it = particles.iterator();while(it.hasNext()){Particle p = it.next();
p.run();if(p.isDead()){
it.remove();}}}}classParticle{PVector location;PVector velocity;PVector acceleration;float lifespan;Particle(PVector l){
acceleration =newPVector(0,0.05);
velocity =newPVector(random(-1,1),random(-2,0));
location = l.get();
lifespan =255.0;}voidrun(){update();display();}voidupdate(){
velocity.add(acceleration);
location.add(velocity);
lifespan -=2.0;}voiddisplay(){stroke(0,lifespan);strokeWeight(2);fill(127,lifespan);ellipse(location.x,location.y,12,12);}
boolean isDead(){if(lifespan <0.0){returntrue;}else{returnfalse;}}}classConfetti extends Particle {Confetti(PVector l){super(l);}voiddisplay(){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();}}classPVector{float x;float y;PVector(float x_,float y_){
x = x_;
y = y_;}PVectorget(){PVector newVector =newPVector(x,y);return newVector;}voidadd(PVector v){
x = x + v.x;
y = y + v.y;}voidsub(PVector v){
x = x - v.x;
y = y - v.y;}voidmult(float n){
x = x * n;
y = y * n;}voiddiv(float n){
x = x / n;
y = y / n;}floatmag(){returnsqrt(x * x + y * y);}voidnormalize(){float m =mag();if(m !=0){div(m);}}voidlimit(float max){if(mag()> max){normalize();mult(max);}}floatheading2D(){returnatan2(y,x);}}