热点新闻
由系统组成的系统
2023-07-16 22:19  浏览:498  搜索引擎搜索“错改B2B”
温馨提示:信息一旦丢失不一定找得到,请务必收藏信息以备急用!本站所有信息均是注册会员发布如遇到侵权请联系文章中的联系方式或客服删除!
联系我时,请说明是在错改B2B看到的信息,谢谢。
展会发布 展会网站大全 报名观展合作 软文发布

书名:代码本色:用编程模拟自然系统
作者:Daniel Shiffman
译者:周晗彬
ISBN:978-7-115-36947-5
目录

4.5 由系统组成的系统

1、由系统组成的系统

  • 我们已经知道如何实现粒子对象,
  • 也学会了如何实现粒子对象组成的系统,这个系统称为“粒子系统”,粒子系统就是由一系列独立对象组成的集合。
  • 但粒子系统本身不也是一个对象?
    如果粒子系统也是个对象,我们可以让这些粒子系统对象组成一个集合,产生一个由系统组成的系统。

2、示例

示例代码4-4 由系统组成的系统

ArrayList<ParticleSystem> systems; void setup() { size(640,360); systems = new ArrayList<ParticleSystem>(); } void draw() { background(255); for (ParticleSystem ps: systems) { ps.run(); ps.addParticle(); } fill(0); text("click mouse to add particle systems",10,height-30); } void mousePressed() { systems.add(new ParticleSystem(1,new PVector(mouseX,mouseY))); }

一旦鼠标被点击,一个新的粒子系统对象将会被创建并放入ArrayList中.

ParticleSystem .pde

class ParticleSystem { ArrayList<Particle> particles; // An arraylist for all the particles PVector origin; // An origin point for where particles are birthed ParticleSystem(int num, PVector v) { particles = new ArrayList<Particle>(); // Initialize the arraylist origin = v.get(); // Store the origin point for (int i = 0; i < num; i++) { particles.add(new Particle(origin)); // Add "num" amount of particles to the arraylist } } 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)); } // A method to test if the particle system still has particles boolean dead() { if (particles.isEmpty()) { return true; } else { return false; } } }

3、运行结果






发布人:55d3****    IP:117.173.23.***     举报/删稿
展会推荐
让朕来说2句
评论
收藏
点赞
转发