Science and Computers -- PHY307/607

Lab 8. - Modifying Simple2 for 2d Fractal Plots

  1. Login to SUnix as per normal and go to your PHY307 directory (what is the unix command for changing directory ?). Create a new directory called Simple3 (again, look back at previous labs to figure out the unix command for creating a directory). Change its permissions so that it can be loaded using Netscape (remember there are 2 types of permissions - chmod 755 for directories and chmod 644 for files - which should I use for Simple3 ?)
  2. Copy the .java files in Simple2 to this new directory Simple3 (cp Simple2/*.java Simple3).
  3. Similarly, copy the file Simple2.html (which is in the directory Simple2) to the new directory Simple3 and name it Simple3.html there. This is done with the unix command cp Simple2/Simple2.html Simple3/Simple3.html.
  4. Change to the Simple3 directory and edit the file Simple3.html using pico so that the applet tag now loads not Simple2.class but Simple3.class.
  5. Within the Simple3 directory rename the file Simple2.java to Simple3.java using the mv command i.e mv Simple2.java Simple3.java
  6. To plot a fractal which is embedded in two dimensions like the Sierpinski triangle we need to modify the Map object/class so that two variables x and y are involved. Thus within the Simple3 directory edit the file Map.java. Replace the line
    private double x;
    by
    private double x,y;
    Also, delete the line private double a;. We don't need the parameter a anymore.
  7. Delete the code enclosed between the beginning and ending braces of the method iterate(). In its place add the lines
    
    int dum;
    dum=(int)(Math.random()*K);
    x=a[dum]*x+b[dum]*y+e[dum];
    y=c[dum]*x+d[dum]*y+f[dum];
    return;
    
    This code should be read as follows: define a integer variable dum. This is set to a random integer between 0 and K-1 each time the method is called. This value of dum is then used to select a set of values of the numbers a,b,c,d,e,f from which to compute the new (iterated) values of x and y. Thus the variable a is an array of numbers - which one is used depends on the value of this randomly chosen integer dum.
  8. We already have a method getX() which returns the value of the variable x. For our present purposes this may be simplified. Replace the code for the getX() method with the following code
    
    public double getX(){
    return(x);
    }
    
    We also need a similar method for y. So add a new method (say after the closing curly brace of getX()) which reads
    
    public double getY(){
    return(y);
    }
    
  9. The only remaining thing to do is to create the arrays a[],b[],c[],d[], e[],f[] and set their values. This will determine which fractal is drawn to the screen. To do this we use a constructor method which is called when the object is first created. It has a name which is the same as the name of the class i.e here Map(). So before the iterate method add the code
    
    Map(){
    x=0.0;
    y=0.0;
    a = new double[K];
    b = new double[K];
    c = new double[K];
    d = new double[K];
    e = new double[K];
    f = new double[K];
    a[0]=0.5;b[0]=0.0;c[0]=0.0;d[0]=0.5;e[0]=0.0;f[0]=0.0;
    a[1]=0.5;b[1]=0.0;c[1]=0.0;d[1]=0.5;e[1]=0.5;f[1]=0.0;
    a[2]=0.5;b[2]=0.0;c[2]=0.0;d[2]=0.5;e[2]=0.25;f[2]=0.5;
    }
    
  10. Finally add the following lines after the line private double x,y;
    
    private double a[],b[],c[],d[],e[],f[];
    private int K=3;
    Also, delete the three methods setA(), getA(), setX() since a no longer exists (i.e delete both the line public void setA(double z){ and all succeeding lines till the next closing curly brace)
  11. OK, you've finished with editing Map.java. Exit pico saving your changes.
  12. Now type pico Simple3.java to start editing Simple3.java. First change the word Simple2 to Simple3 in the second line.
  13. Next, inside the init() method delete the lines containing the method calls mymap.setX(); and mymap.setA();. Also in init() set the size of the x and y arrays to 1000. That is, whereever you see the number 100 replace it with 1000 in this method.
  14. Next, go to the paint() method. Change the line g.drawLine(xp[i],yp[i],xp[i+1],yp[i+1]); to g.fillOval(x[i]-2,y[i]-2,4,4);. This piece of code now means that circles of size 4 pixels are drawn at each x,y coordinate point. Also, change the arguments of the fillRect() method to 50,50,200,200.
  15. Consider the method addPoint(). Since the total applet size is 300 by 300 (as defined in the applet tag) and we have a border of 50 pixels around the drawing area with our fractal lying between x=0..1 and y=0..1 we then have to set
    double xscale=200.0; double yscale=200.0;
    . Make this change.
  16. Final things: delete the lines ix=xoffset+(int)(xscale.... and iy=yoffset-(int)(yscale... and replace them by the lines
    
    ix=xoffset+(int)(xscale*mymap.getX());
    iy=yoffset-(int)(yscale*mymap.getY());
    
    Also, change the line if(counter==25) counter=0; to if(counter < 999) counter++;. Delete the line above which contains the code fragment counter++;.
  17. Now, you're all finished. Compile the new code with javac *.java. If there are no errors, change the file permissions with chmod 644 *.class. Also, remember to change the file permissions on Simple3.html. Point Netscape at web.syr.edu/~yourSUnixuserid/PHY307/Simple3/Simple3.html.
  18. Hit iterate many times. You should see a by now familiar fractal start to appear on the screen. At each iteration you are jumping from one point of the fractal to another. The choice of which neighboring point you jump to is random but the final shape that is drawn is far from that - it is a beautiful regular fractal! Thus the set of values of x,y that you generate via this funny 2d map are just the points of a regular fractal. The same is quite generally true of nonlinear maps in a chaotic regime although the fractal (strange attractor) that they are associated with is not necesarily such a simple, regular one.

Self-similarity in the SnowFlake Fractal


To get credit for this lab you need to
  1. Email me the url to your Web page so that I can take a look at your working Simple3 applet.
The deadline to get these things done is next Thursday.

Back to the PHY307 Homepage

This page maintained by Simon Catterall, last updated 19 October, 1999.