Advertisement
calcpage

LACS07a_IFS.java

Jun 12th, 2012
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.31 KB | None | 0 0
  1. /*************************************************************************
  2.  *  Compilation:  javac IFS.java
  3.  *  Execution:    java IFS N < input.txt
  4.  *  Dependencies: StdDraw.java
  5.  *
  6.  *  Here are some sample data files:
  7.  *  
  8.  *  http://www.cs.princeton.edu/introcs/22library/barnsley.txt
  9.  *  http://www.cs.princeton.edu/introcs/22library/binary.txt
  10.  *  http://www.cs.princeton.edu/introcs/22library/culcita.txt
  11.  *  http://www.cs.princeton.edu/introcs/22library/cyclosorus.txt
  12.  *  http://www.cs.princeton.edu/introcs/22library/dragon.txt
  13.  *  http://www.cs.princeton.edu/introcs/22library/fern-sedgewick.txt
  14.  *  http://www.cs.princeton.edu/introcs/22library/fishbone.txt
  15.  *  http://www.cs.princeton.edu/introcs/22library/floor.txt
  16.  *  http://www.cs.princeton.edu/introcs/22library/koch.txt
  17.  *  http://www.cs.princeton.edu/introcs/22library/sierpinski.txt
  18.  *  http://www.cs.princeton.edu/introcs/22library/spiral.txt
  19.  *  http://www.cs.princeton.edu/introcs/22library/swirl.txt
  20.  *  http://www.cs.princeton.edu/introcs/22library/tree.txt
  21.  *  http://www.cs.princeton.edu/introcs/22library/zigzag.txt
  22.  *
  23.  *************************************************************************/
  24.  
  25. public class IFS {
  26.     public static void main(String[] args) {
  27.  
  28.         // number of iterations
  29.         int T = Integer.parseInt(args[0]);
  30.  
  31.         // probability distribution for choosing each rule
  32.         double[] dist = StdArrayIO.readDouble1D();
  33.  
  34.         // update matrices
  35.         double[][] cx = StdArrayIO.readDouble2D();
  36.         double[][] cy = StdArrayIO.readDouble2D();
  37.  
  38.         // current value of (x, y)
  39.         double x = 0.0, y = 0.0;
  40.  
  41.         // do T iterations of the chaos game
  42.         for (int t = 0; t < T; t++) {
  43.  
  44.             // pick a random rule according to the probability distribution
  45.             int r = StdRandom.discrete(dist);
  46.  
  47.             // do the update
  48.             double x0 = cx[r][0]*x + cx[r][1]*y + cx[r][2];
  49.             double y0 = cy[r][0]*x + cy[r][1]*y + cy[r][2];
  50.             x = x0;
  51.             y = y0;
  52.  
  53.             // draw the resulting point
  54.             StdDraw.point(x, y);
  55.  
  56.             // for efficiency, display only every 100 iterations
  57.             if (t % 100 == 0) StdDraw.show(10);
  58.         }
  59.  
  60.         // ensure everything gets drawn
  61.         StdDraw.show(0);
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement