topical media & game development

talk show tell print

#javascript-processing-example-topic-motion-brownian.htm / htm



  <!DOCTYPE html>
  <html><head>
  <script src="javascript-processing-example-processing.js"></script>
  <script src="javascript-processing-example-init.js"></script>
  <link rel="stylesheet" href="javascript-processing-example-style.css">
  </head><body><h1><a href="http://ejohn.org/blog/processingjs/">Processing.js</a></h1>
  <h2>Brownian</h2>
  
  <p>Recording random movement as a continuous line.</p>
  
  <p><a href="http://processing.org/learning/topics/brownian.html"><b>Original Processing.org Example:</b> Brownian</a><br>
  <script type="application/processing">
  int num = 2000;
  int range = 4;
  
  float[] ax = new float[num];
  float[] ay = new float[num]; 
  
  void setup() 
  {
    size(200, 200);
    for(int i=0; i<num; i++) {
      ax[i] = 50;
      ay[i] = height/2;
    }
    frameRate(30);
  }
  
  void draw() 
  {
    background(51);
    
    // Shift all elements 1 place to the left
    for(int i=1; i<num; i++) {
      ax[i-1] = ax[i];
      ay[i-1] = ay[i];
    }
  
    // Put a new value at the end of the array
    ax[num-1] += random(-range, range);
    ay[num-1] += random(-range, range);
  
    // Constrain all points to the screen
    ax[num-1] = constrain(ax[num-1], 0, width);
    ay[num-1] = constrain(ay[num-1], 0, height);
    
    // Draw a line connecting the points
    for(int i=1; i<num; i++) {    
      float val = float(i)/num * 204.0 + 51;
      stroke(val);
      line(ax[i-1], ay[i-1], ax[i], ay[i]);
    }
  }
  </script><canvas width="200" height="200"></canvas></p>
  <div style="overflow: hidden; height: 0px; width: 0px;"></div>
  
  <pre><b>// All Examples Written by <a href="http://reas.com/">Casey Reas</a> and <a href="http://benfry.com/">Ben Fry</a>
  // unless otherwise stated.</b>
  int num = 2000;
  int range = 4;
  
  float[] ax = new float[num];
  float[] ay = new float[num]; 
  
  void setup() 
  {
    size(200, 200);
    for(int i=0; i&lt;num; i++) {
      ax[i] = 50;
      ay[i] = height/2;
    }
    frameRate(30);
  }
  
  void draw() 
  {
    background(51);
    
    // Shift all elements 1 place to the left
    for(int i=1; i&lt;num; i++) {
      ax[i-1] = ax[i];
      ay[i-1] = ay[i];
    }
  
    // Put a new value at the end of the array
    ax[num-1] += random(-range, range);
    ay[num-1] += random(-range, range);
  
    // Constrain all points to the screen
    ax[num-1] = constrain(ax[num-1], 0, width);
    ay[num-1] = constrain(ay[num-1], 0, height);
    
    // Draw a line connecting the points
    for(int i=1; i&lt;num; i++) {    
      float val = float(i)/num * 204.0 + 51;
      stroke(val);
      line(ax[i-1], ay[i-1], ax[i], ay[i]);
    }
  }</pre>
  </body></html>
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.