Barnsley.java


This is the syntax highlighted version of Barnsley.java from 2.4 Input and Output of
Introduction to Computer Science by Robert Sedgewick and Kevin Wayne.

/*************************************************************************
 *  Compilation:  javac Barnsley.java
 *  Execution:    java Barnsley N
 *  Dependencies: StdDraw.java
 *
 *  Play chaos game to produce Barnsley's fern.
 *
 *  % java Barnsley 10000
 *
 *  Reference: http://physics.wm.edu/~shiwei/courses/Phys690F03/HW/hw1/prob2.pdf
 *
 *************************************************************************/

public class Barnsley {

   public static void main(String args[]) {
      int N = Integer.parseInt(args[0]);     // number of points to draw
      StdDraw.create(512, 512);
      StdDraw.setScale(0, 0, 1, 1);          // unit square
      StdDraw.setColorRGB(0, 114, 0);        // shade of green

      // apply the update rules
      double x = 0.5;
      double y = 0.0;
      for (int i = 0; i < N; i++) {
         double tempx, tempy;
         double r = Math.random();

         if (r <= 0.02)  {
             tempx = 0.5;
             tempy = 0.27 * y;
         }
         else if (r <= 0.17) {
             tempx = -0.139 * x + 0.263 * y + 0.5700;
             tempy =  0.246 * x + 0.224 * y - 0.0360;
         }
         else if (r <= 0.30) {
             tempx =  0.170 * x - 0.215 * y + 0.4080;
             tempy =  0.222 * x + 0.176 * y + 0.0893;
         }
         else {
             tempx =  0.781 * x + 0.034 * y + 0.1075;
             tempy = -0.032 * x + 0.739 * y + 0.2700;
         }

         x = tempx;
         y = tempy;
         StdDraw.go(x, y);
         StdDraw.spot(0);
      }
      StdDraw.show();
   }

   
}


Last updated: Thu Sep 16 11:36:57 EDT 2004 .
Copyright © 2004, Robert Sedgewick and Kevin Wayne.