用processing制作整齐排列的圆圈呈水波状动画效果

运行结果

代码部分

void setup(){
  size(600,600);
  radius = 0.5*width/float(num_side+1);
}
int num_side = 10;
float t = 0.0;
float dt = 0.01;
float x,y,osc,radius;
float rateRatio=0.75;

void draw(){
  t = t + dt;
  background(255);
  for(int i = 0; i<num_side; i++){
    for(int j = 0; j<num_side; j++){
      fill(0);
      x=float(j)/float(num_side);
      y=float(i)/float(num_side);
      osc = radius*(sin(TWO_PI * (y+t))+cos(TWO_PI * (x+t)));
      ellipse(2*(radius+i*radius),2*(radius+j*radius),osc,osc);
    }
  }
  for(int i = 0;i<num_side;i++){
    for(int j = 0;j<num_side;j++){
      x = float(j)/float(num_side);
      y = float(i)/float(num_side);
      fill(255);
      osc = 25*(sin(TWO_PI*(y-rateRatio*t))+cos(TWO_PI*(x-rateRatio*t)));
      ellipse(2*(radius+i*radius),2*(radius+j*radius),osc,osc);
    }
  }
}
制作粒子效果,使用Processing可以使用两种方式:使用现有的粒子库或自己编写代码。 1. 使用现有的粒子库:Processing有许多粒子库可供使用,如Toxiclibs、Traer等。这些库提供了许多现成的函数和类,可以轻松地创建和控制粒子效果。 例如,使用Toxiclibs库可以轻松地创建一个简单的粒子效果: ``` import toxi.geom.*; import toxi.physics.*; import toxi.physics.behaviors.*; VerletPhysics physics; VerletParticle[] particles; void setup() { size(400, 400); physics = new VerletPhysics(); particles = new VerletParticle[100]; for (int i = 0; i < particles.length; i++) { particles[i] = new VerletParticle(new Vec2D(random(width), random(height))); physics.addParticle(particles[i]); physics.addBehavior(new AttractionBehavior(particles[i], 20, -1)); } } void draw() { background(255); physics.update(); stroke(0); for (int i = 0; i < particles.length; i++) { point(particles[i].x, particles[i].y); } } ``` 2. 自己编写代码:如果你想更深入地了解粒子效果的原理和实现方法,可以自己编写代码。通常需要使用向量、物理引擎等概念和技术。 例如,可以使用向量来表示粒子的位置和速度,使用物理引擎来模拟粒子的运动和碰撞: ``` Particle[] particles; void setup() { size(400, 400); particles = new Particle[100]; for (int i = 0; i < particles.length; i++) { particles[i] = new Particle(new PVector(random(width), random(height)), new PVector(random(-1, 1), random(-1, 1))); } } void draw() { background(255); for (int i = 0; i < particles.length; i++) { particles[i].update(); particles[i].display(); } } class Particle { PVector pos, vel, acc; Particle(PVector pos, PVector vel) { this.pos = pos; this.vel = vel; this.acc = new PVector(0, 0.1); } void update() { vel.add(acc); pos.add(vel); if (pos.x < 0 || pos.x > width) vel.x *= -1; if (pos.y < 0 || pos.y > height) vel.y *= -1; } void display() { stroke(0); ellipse(pos.x, pos.y, 10, 10); } } ``` 以上是两种制作粒子效果的方法,你可以根据自己的需求和技能选择适合自己的方式。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值