算法_气体分子模型_Processing

算法_气体分子模型_Processing_第1张图片
气体分子小球模型Processing代码运行界面

参考文章:Experiments in statistical mechanics

Main function
float ball_l=80;
int N = 3;
Particle [] ps= new Particle[N];
Bar bar = new Bar (int(ball_l)*5);
float heigh = ball_l*4;
float widt = ball_l*8;
//parameters for statistics
float timeBoxWidth=ball_l*4; //the width of time box
float timeBoxHeight=ball_l*2;
float timeBoxOriginX=ball_l*8; //leftupper corner x of time box
float timeBoxOriginY=ball_l*0; //leftupper corner y of time box
int counter = 0; //time step counter
float [] ts = {}; //time steps
float [] ys = {}; //bar locations
float histBoxWidth = ball_l*4; //the width of histogram box
float histBoxHeight = ball_l*2; //the height of histogram box
float histBoxOriginX=ball_l*8; //leftupper corner x of hist box
float histBoxOriginY=ball_l*2; //leftupper corner y of hist box
 
void setup() {
  size(int(ball_l)*12,int(ball_l)*4);
  /*
  for (int i=0; i 0){normalX = 1;}
      else {normalX=-1;}
      float midpointX = (ps[i].location.x+ps[i].r*normalX+bar.x)/2;
      ps[i].location.x = midpointX - normalX*ps[i].r;
      bar.x = midpointX;
      float dvx = ps[i].velocity.x - bar.velocity;
      ps[i].velocity.x -= dvx;
      bar.velocity  += dvx;
    }  
  }
  
  //calculate total energy
  float E=0;
  for (int i=0; i1){
    for (int i=0;i
Particle Class
class Particle {
  
  PVector location;
  PVector velocity;
  //PVector acceleration;
  float mass;
  float r;
 
  Particle(PVector l, float radius) {
    mass = 1;
    //acceleration = new PVector(0,0);
    velocity = new PVector(5,5);
    location = l.get();
    r = radius;
  }
 
  void run() {
    update();
    display();
    checkEdges();
  }
 
  void update() {
    //velocity.add(acceleration);
    location.add(velocity);
  }
 
  void display() {
    stroke(0);
    fill(175);
    ellipse(location.x,location.y,r*2,r*2);
  }
  
    void checkEdges() {
      if (location.x < r){
        location.x = r;
        velocity.x *= -1;
      }
      if (location.x > widt-r){
        location.x = widt-r;
        velocity.x *= -1;
      }      
      if (location.y < r){
        location.y = r;
        velocity.y *= -1;
      }
      if (location.y > heigh-r){
        location.y = heigh-r;
        velocity.y *= -1;
      }
  }
}
Bar class
class Bar {
  
  float x;
  float velocity;
  //float acceleration;
  float mass;
 
  Bar(float l_) {
    mass = 1;
    //acceleration = 0;
    velocity = 0;
    x = l_;
  }
 
  void run() {
    update();
    display();
    checkEdges();
  }
 
  void update() {
    //velocity+=acceleration;
    x+=velocity;
  }
 
  void display() {
    stroke(0);
    fill(100);
    strokeWeight(2);
    line(x,0,x,heigh);
    strokeWeight(1);
  }
  
  void checkEdges() {
    if (x < 0){
      x = 0;
      velocity *= -1;
    }
    if (x > widt){
      x = widt;
      velocity *= -1;
    }
  }
  
}



}

你可能感兴趣的:(算法_气体分子模型_Processing)