/* This java applet program generates a fractal image of a fern. * The algorithm is based on the Iterated Function System (IFS). * It is generated by four affine maps of the plane. * They are of the form x'=Ax+(e,f), where A=(a,b|c,d) is a 2x2 matrix. * There is a slight subtlety in the drawing of the picture. The four * mappings are not used with equal probability. The probabilities p are * given in the table. This does not affect the actual IFS Attractor, * but does affect the computergraphical appearance. You could use for * example the table below. * # a b c d e f p * 1 0 0 0 0.16 0 0 0.01 * 2 0.85 0.04 -0.04 0.85 0 1.6 0.85 * 3 0.2 -0.26 0.23 0.22 0 1.6 0.07 * 4 -0.15 0.28 0.26 0.24 0 0.44 0.07 * * Write the HTML code as: * * C.A. Bertulani, 08/15/2000 */ import java.awt.*; import java.applet.*; public class Fern extends Applet { double mxx,myy,bxx,byy; public void Fernplot(Graphics g) { int i; double x, y, xn, yn, r; int pex,pey; int max, seed; max=15000; seed=68111; x=0.5; /* starting point */ y=0.0; convert(0.0,1.0,1.0,-0.5); setBackground(Color.black); g.setColor(Color.green); for(i=1; i<=max; i++) /* iterations */ { r=Math.random(); /* generate a random number */ if (r <= 0.02) /* case 1 */ { xn = 0.5; yn = 0.27*y; } else if((r>0.02) && (r<=0.17)) /* case 2 */ { xn = -0.139*x + 0.263*y + 0.57; yn = 0.246*x + 0.224*y - 0.036; } else if ((r>0.17) && (r<=0.3)) /* case 3 */ { xn = 0.17*x - 0.215*y + 0.408; yn = 0.222*x + 0.176*y + 0.0893; } else /* case 4 */ { xn = 0.781*x + 0.034*y + 0.1075; yn = -0.032*x + 0.739*y + 0.27; } x=xn; y=yn; pex=(int)(mxx*x+bxx); pey=(int)(myy*y+byy); g.drawLine(pex,pey,pex,pey); /* output to screen */ } } void convert(double xiz,double ysu,double xde,double yinf) {//to convert to screen coordinates. Gives mxx, bxx for //x_screen=mxx*x_world + byy same for y. double maxx,maxy,xxfin,xxcom,yyin,yysu; maxx=600; maxy=450; xxcom=0.15*maxx; xxfin=0.75*maxx; yyin=0.8*maxy; yysu=0.2*maxy; mxx=(xxfin-xxcom)/(xde-xiz); bxx=0.5*(xxcom+xxfin-mxx*(xiz+xde)); myy=(yyin-yysu)/(yinf-ysu); byy=0.5*(yysu+yyin-myy*(yinf+ysu)); } public void paint(Graphics g) { Fernplot(g); showStatus("Done"); } }