参考《代码本色》所进行的编程习作

  • 目录
    • 序章 随机游走
    • 第一章 向量
    • 第二章 力
    • 第三章 震荡
    • 第四章 粒子系统

目录

序章 随机游走

实验结果

应用的技术
①概率和非均匀分布 绘制了一个小球使他有50%的概率向着鼠标所在的方向移动。
②perlin噪声 如果小球没有向着鼠标运动(另外的50%概率),那么他就使用噪声noise()产生随机数值使小球随机移动一步。

实验代码

Walker w;
float tx=0;
float ty=10000;void setup() {size(640,360);w = new Walker();background(255);
}void draw() {w.step();w.display();
}class Walker {float x;float y;Walker() {x = width / 2;y = height / 2;}void display() {stroke(100,200,200);ellipse(x,y,16,16);}void step() {float r = random(1);if(r<0.5){                        //使得小球向鼠标运动的概率是0.5//小球向着鼠标方向移动if((x<mouseX) && (y<mouseY)){ x++;y++;}else if((x<mouseX) && (y>mouseY)){x++;y--;}else if((x>mouseX) && (y>mouseY)){x--;y--;}else if((x>mouseX) && (y<mouseY)){x--;y++;}}else if(r>0.5){x = x + noise(tx);//perlin噪声y = y + noise(ty);tx+=0.01;ty+=0.01;}}
}

第一章 向量

实验结果

应用的技术
做了一个类似弹球的程序,当小球碰到鼠标小球就会被弹起,如果把鼠标移走,小球就会自然下落,当碰到窗口的边缘时也会被弹起。

①向量的加法 小球即时的位置=(原来的位置.x+速度,原来的位置.y+速度)

②向量运动的速度和加速度 小球运动时设置了大小方向恒定的加速度使得小球下落运动的速度越来越快。

实验代码

Mover mover;void setup() {size(300,300);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(width/2,0);velocity = new PVector(random(-2,2),random(-2,2));//初始速度随机acceleration = new PVector(-0.001,0.1);//加速度大小方向恒定topspeed = 10;}void update() {    velocity.add(acceleration);//更新速度velocity.limit(topspeed);location.add(velocity); //更新位置}void display() {stroke(100,200,200);fill(100,200,200);ellipse(location.x,location.y,20,20);}void checkEdges() {//小球碰到鼠标后反弹if((location.x > mouseX) || (location.x < 0)) {velocity.x = velocity.x * -1; }if((location.y > mouseY) || (location.y < 0)) {velocity.y = velocity.y * -1; }}
}

第二章 力

实验结果

应用的技术
①给小球施加外力(风力) 如果点击鼠标就可以给小球施加一个随机大小方向向右的风力。
②空气和流体阻力 模拟了不同质量的物体在空气中和在流体中收到不同阻力的情况。

实验代码

Mover[] movers = new Mover[11];
Liquid liquid;
float flag=0;void mousePressed() {flag=random(0,0.5);//如果点击鼠标就随机给flag赋值
}void setup() {size(800,500);reset();liquid = new Liquid(0,height / 2,width, height / 2,0.1);
}void draw() {background(255);liquid.display();for(int i = 0; i < movers.length; i++) {if(liquid.contains(movers[i])) {PVector dragForce = liquid.drag(movers[i]);movers[i].applyForce(dragForce);//判断如果小球进入下面灰色的区域就施加流体阻力}PVector gravity = new PVector(0,0.1 * movers[i].mass);PVector wind = new PVector(flag,0);movers[i].applyForce(gravity);movers[i].applyForce(wind);movers[i].update();movers[i].display();movers[i].checkEdges();}}void reset() {for(int i = 0; i < movers.length; i++) {movers[i] = new Mover(random(0.5,3),40 + i * 70,0); }
}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);ellipse(location.x,location.y,mass * 16,mass * 16);}void checkEdges() {if(location.y > height) {velocity.y *= -0.9;  location.y = height;}if(location.x > width) {velocity.x *= -0.5;  location.x = width;}}
}class Liquid {float x,y,w,h;float c;Liquid(float x_,float y_,float w_,float h_,float c_) {x = x_;y = y_;w = w_;h = h_;c = c_;}// is the mover in the liquid?boolean contains(Mover m) {PVector l = m.location;if(l.x > x && l.x < x + w && l.y > y && l.y < y + h) {return true; } else {return false;}}PVector drag(Mover m) {float speed = m.velocity.mag();float dragMagnitude = c * speed * speed;PVector dragForce = m.velocity.get();dragForce.mult(-1);dragForce.normalize();dragForce.mult(dragMagnitude);return dragForce;}void display() {noStroke();fill(50);rect(x,y,w,h);}
}

第三章 震荡

实验结果

应用的技术
①指向运动的方向 屏幕中有一个长方形小物体会跟随鼠标移动,它运动的方向始终指向鼠标的方向。
②弹力 屏幕上有一个模拟的弹球装置,当长方形小物体靠近它时会给他施加一个外力使得弹力小球朝小物体相反的方向弹走。

实验代码

Bob bob;
Spring spring;
float flag=0;
float one=0;
Mover mover;void setup() {size(800,500);spring = new Spring(width/2,10,100); bob = new Bob(width/2,100); mover = new Mover();
}void draw()  {background(255); mover.update();mover.display();PVector gravity = new PVector(0,2);PVector zhuang = new PVector(one,0);//在x方向上撞击的力和在y方向上的拉力if(mover.location.x>400){one=random(-10,0);   }else{one=random(0,10);}if(mover.location.y>bob.location.y){flag=random(-1,0);}else{flag=random(0,10);}bob.applyForce(zhuang);bob.applyForce(gravity);spring.connect(bob);spring.display(); spring.displayLine(bob);bob.update();bob.display(); }class Spring {PVector anchor;float len;float k = 0.1;Spring(float x,float y,int l) {anchor = new PVector(x,y);len = l;}void connect(Bob b) {PVector force = b.location.get();force.sub(anchor);float d = force.mag();float stretch = d - len;force.normalize();force.mult(-1 * k * stretch);b.applyForce(force);}void display() { stroke(0);fill(175);strokeWeight(2);rectMode(CENTER);rect(anchor.x, 150, 10, 10);}void displayLine(Bob b) {strokeWeight(2);stroke(0);line(b.location.x, b.location.y, anchor.x, 150);}
}class Bob { PVector location;PVector velocity;PVector acceleration;float mass = 24;float damping = 0.98;Bob(float x, float y) {location = new PVector(x,y);velocity = new PVector(0,0);acceleration = new PVector(0,0);} void update() { velocity.add(acceleration);velocity.mult(damping);location.add(velocity);acceleration.mult(0);}void applyForce(PVector force) {PVector f = force.get();f.div(mass);acceleration.add(f);}void display() { stroke(0);strokeWeight(2);fill(175);ellipse(location.x,location.y,mass * 2,mass * 2);}
}class Mover {PVector location;PVector velocity;PVector acceleration;float topspeed;float xoff,yoff;float r = 16;Mover() {location = new PVector(width / 2,height / 2);velocity = new PVector(0,0);topspeed = 4;xoff = 1000;yoff = 0;}void update() {PVector mouse = new PVector(mouseX,mouseY);PVector dir = mouse.get();dir.sub(location);dir.normalize();dir.mult(0.5);acceleration = dir;velocity.add(acceleration);velocity.limit(topspeed);location.add(velocity);}void display() {float theta = velocity.heading2D();stroke(0);strokeWeight(2);fill(127);pushMatrix();rectMode(CENTER);translate(location.x,location.y);rotate(theta);rect(0,0,30,10);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); }
}

第四章 粒子系统

实验结果

应用的技术
①由粒子系统组成的系统 会在鼠标单击的地方产生一个粒子系统,整个程序会有很多个粒子系统,由他们再组成了一个系统,对这个系统进行的一些操作。

②受力作用的粒子系统 给产生的这些粒子施加一个重力(图中是因为我把重力的数值设置的有点小,如果调大重力的话就可以看见这些像素小点直直下落无法飞起来形成喷射效果)。

实验代码

import java.util.Iterator;ArrayList<ParticleSystem> systems;void setup() {size(800,500);systems = new ArrayList<ParticleSystem>();
}void draw() {background(255);PVector g = new PVector(0,0.1);for(ParticleSystem ps : systems) {  //遍历每一个粒子系统ps.applyForce(g);                //给粒子系统施加重力就是给上面的每一个粒子施加重力ps.run();ps.addParticle();}
}void mousePressed() {//在鼠标点击处制造新的粒子系统systems.add(new ParticleSystem(1,new PVector(mouseX,mouseY)));
}class ParticleSystem {ArrayList<Particle> particles;PVector origin;ParticleSystem(int num,PVector v) {particles = new ArrayList<Particle>();origin = v.get();for(int i = 0; i < num; i++) {particles.add(new Particle(origin)); }}void addParticle() {particles.add(new Particle(origin)); }void addParticle(Particle p) {particles.add(p); }void applyForce(PVector f){for(Particle p : particles){p.applyForce(f);}}void run() {Iterator<Particle> it = particles.iterator();  while(it.hasNext()) {Particle p = it.next();p.run();if(p.isDead()) {it.remove(); }}    }boolean dead() {if(particles.isEmpty()) {return true; } else {return false; }}
}class Particle {PVector location;PVector velocity;PVector acceleration;float lifespan;float mass=1;color c;Particle(PVector l) {acceleration = new PVector(0,0);velocity = new PVector(random(-1,1),random(-2,0));location = l.get();        lifespan = 255;c = color(random(255),random(255),random(255));}void run() {update();display();}void applyForce(PVector force) {PVector f=force.get();f.div(mass);acceleration.add(f);}void update() {velocity.add(acceleration);location.add(velocity);acceleration.mult(0);lifespan -= 2.0;}void display() {stroke(c,lifespan);strokeWeight(5);fill(c,lifespan);point(location.x,location.y);}boolean isDead() {if(lifespan < 0.0) {return true; } else {return false; }}
}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); }}

代码本色 processing编程练习相关推荐

  1. 《代码本色》作者Daniel Shiffman:艺术家也编程

    非商业转载请注明作译者.出处,并保留本文的原始链接:http://www.ituring.com.cn/article/179855 Daniel Shiffman是纽约大学Tisch艺术学院助理艺术 ...

  2. 《代码本色:用编程模拟自然系统》作者Daniel Shiffman访谈问题有奖征集

    Daniel Shiffman是纽约大学Tisch艺术学院助理艺术教授,"代码本色"便是其主讲课程之一.他在耶鲁大学获得了数学与哲学学士学位和交互通讯的硕士学位.多年来,他一直用P ...

  3. 代码本色——雪梨的Processing探索·Chapter 0:随机游走

    概述 Chapter 0为我们介绍了随机数.概率和噪声在运动当中起到的变化作用,下面就让我们来好好的了解下这些数学名词,究竟可以起到怎样的公用,最后再让我们来发挥想象进行这些知识的运用创作. 原理介绍 ...

  4. processing创意图形代码_代码之上,诗意之中 | Processing编程造个梦

    今天介绍算法艺术实验室二位优秀学员的一个合作作品. 以<梦游天姥吟留别>这首诗为概念设计,用Processing编程在一个沉浸空间里制造光影,码出诗中那仙府名山. 传统美学结合了编程,被打 ...

  5. 基于《代码本色》的processing学习与拓展

    目录 1. 第0章 引言 2. 第1章 向量 3. 第2章 力 4. 第3章 振荡 5. 第4章 粒子系统 1. 第0章 引言 <代码本色>在这一章节的主要内容是模拟随机游走. 讲了利用r ...

  6. Processing编程学习指南导读

    前 言 Learning Processing:A Beginner抯 Guide to Programming Images, Animation, and Interaction, Second ...

  7. 代码本色0章——Perlin噪声生成起伏地形

    代码本色0章--Perlin噪声生成起伏地形 此博文依据代码本色第0章中的随机数与Perlin噪声运用写成,并观看了丹尼尔希夫曼关于perlin噪声的讲解,https://www.youtube.co ...

  8. C语言代码规范(编程规范)

    首页 > 编程笔记 > C语言笔记 阅读:7,165 C语言代码规范(编程规范) C语言中文网推出辅导班啦,包括「C语言辅导班.C++辅导班.算法/数据结构辅导班」,全部都是一对一教学:一 ...

  9. [.NET] 怎样使用 async await 一步步将同步代码转换为异步编程

    怎样使用 async & await 一步步将同步代码转换为异步编程 [博主]反骨仔 [出处]http://www.cnblogs.com/liqingwen/p/6079707.html  ...

  10. 敏捷开发“松结对编程”系列之十五:L型代码结构(编程篇之一)

    本文是"松结对编程"系列的第十五篇.(松结对编程栏目目录) 之前的L型代码结构的前三篇提到过,L型代码结构的微观计划和估算过程会与一般的编程方法不同,今天正好要编写一些新代码,边写 ...

最新文章

  1. TinyML-TVM是如何驯服Tiny的(下)
  2. MySql JDBC
  3. SAP HR 报表开发
  4. 【转】基于XML-RPC的BloggerAPI学习
  5. APK反编译之APKTOOL的使用
  6. Web浏览器调试工具firebug
  7. 第7章--基本统计分析
  8. PSP3000破解原理——缓冲区溢出漏洞随谈
  9. Git 使用源代码包编译、配置部署和使用 使用包管理工具安装
  10. Python之身份证号码的校验
  11. 【跨境电商】EDM邮件营销完整指南(一):概念,区别与优势
  12. 如何把苍白的一年写成耀眼的年终报告?写完当场加薪的那种
  13. 2路继电器控制直流电机正反转问题
  14. java 往数组增加一个元素
  15. uniapp ios 真机调试
  16. 朋友圈大数据:你的朋友圈出卖了你,大数据就是这么给力!!!
  17. 支付宝二维码支付文档,找了好久,记在这里
  18. 如何降低自动化维护成本?
  19. C语言绕过杀毒软件,易语言插件规避杀毒软件方法
  20. mysql查询学科_查询出各个学科的前3名的同学信息的Sql

热门文章

  1. C#中的Obsolete
  2. 网易实探瑞幸门店:消费者1.8折买到手软,店员累到流汗
  3. 摘抄(SAP所有模块用户出口(User Exits) )
  4. Fabric CA的部署与使用
  5. 【pyqt5学习】——菜单栏(QMenu())、工具栏QToolBar学习
  6. 文献阅读 - Expressive Expression Mapping with Ratio Images
  7. 解决ffmpeg合并视频后播放条拖不动,画面出错的问题
  8. 斑马Zebra 170Xi4 打印机驱动
  9. 论文:轨迹路线生成算法
  10. 26.时空跳跃者的魔法