第2章 力——《processing》学习,自己完成实验的代码

第三个实验实现结果:
在这里插入图片描述
实现代码如下:

//主函数为:
Mover[] movers = new Mover[20];
Mover[] mover2s = new Mover[10];

void setup() {
  size(640,360);
  for (int i = 0; i < movers.length; i++) {
    movers[i] = new Mover(random(0.8,6),0,0);
    //mass=random(0.1,8)质量随机 
  }
   for (int i = 0; i < mover2s.length; i++) {
   mover2s[i] = new Mover(random(0.8,4),0,0);
   }
}

void draw() {
  background(255);
  float t = 1;
  
  for (int i = 0; i < movers.length; i++) {
    //PVector wind = new PVector(noise(t)*0.1,0);
    if (mousePressed) {
       PVector wind = new PVector(0.01,noise(t));
       movers[i].applyForce(wind);
         t+=0.1;
    }
    PVector gravity = new PVector(0,0.1); 
    movers[i].applyForce(gravity);
    
    movers[i].update();
    movers[i].display();
    movers[i].checkEdges();
  }
  
  for (int i = 0; i < mover2s.length; i++) {
    if (mousePressed) {
       PVector wind = new PVector(0.01,noise(t));
       mover2s[i].applyForce(wind);
         t+=0.1;
    }
    PVector gravity = new PVector(0,0.1);
    
    mover2s[i].applyForce(gravity);
    mover2s[i].update();
    mover2s[i].display2();
    mover2s[i].checkEdges2();
    stroke(122,103,238,230);
    strokeWeight(2);
    line(width/2,0,width/2,height/2);
    line(0,height/2,width/2,height/2);
  }
}

//引用的Mover类
class Mover {

  PVector position;
  PVector velocity;
  PVector acceleration;
  float mass;

  Mover(float m, float x , float y) {
    mass = m;
    position = new PVector(x,y);
    velocity = new PVector(0,0);
    acceleration = new PVector(0,0);
  }
  
  void applyForce(PVector force) {
    PVector f = PVector.div(force,mass);
    acceleration.add(f);
  }
  
  void update() {
    velocity.add(acceleration);
    position.add(velocity);
    acceleration.mult(0);
  }

  void display() {
    stroke(255);
    strokeWeight(1);
    float b = randomGaussian();
    b = constrain(b,100,255);
    fill(30,b,255,b);
    rectMode(CENTER);
    ellipse(position.x,position.y,mass*16,mass*16);
  }
  void display2() {
    stroke(255);
    strokeWeight(1);
    float g = randomGaussian();
    g = constrain(g,60,180);
    float a = randomGaussian();
    a = constrain(a,200,255);
    fill(255,g,180,a);
    ellipse(position.x,position.y,mass*16,mass*16);
  }

  void checkEdges() {
    if (position.x > width) {
      position.x = width;
      velocity.x *= -1;
    } else if (position.x < 0) {
      velocity.x *= -1;
      position.x = 0;
    }
    if (position.y > height) {
      velocity.y *= -1;
      position.y = height;
    }
  }
  
  void checkEdges2() {
    if (position.x > width/2) {
      position.x = width/2;
      velocity.x *= -1;
    } else if (position.x < 0) {
      velocity.x *= -1;
      position.x = 0;
    }
    if (position.y > height/2) {
      velocity.y *= -1;
      position.y = height/2;
    }
  }

}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值