2016年11月26日土曜日

Processingでブラウン運動

実験の授業の参考のためにProcessingでブラウン運動 (というか酔歩) を再現するプログラムを書いたところ、やたらとアーティスティックな感じになって愉快。
こちらを参考にしました。
Processing.jsでweb上で動かすとこんな感じこんな感じ。

int r = 3;  
int v = 5;  
int x = 400;  
int y = 400; 
int c=0;
void setup() {
  size(1000, 1000);
  background(255);     
  noStroke();   
  fill(c);      
  x=width/2;
  y=height/2;
}
 
void draw() {
 
  fill(c);    
  
  float th=random(0,2*PI);
  
  x += int(v*cos(th));
  y += int(v*sin(th));
  if(x>width) x=0;
  if(x<0 if="" x="width;" y="">height) y=0;
  if(y<0 c="" if="" y="height;">255) c=0;
}