import java.awt.Color;

/* Generate Barnsley's Fern
 */
public class Fern {
  
  public static void main(String[] args) {
    
    MyFrame f = new MyFrame("Fern");
    f.setRange(-3, 3, -1, 11);
    
    double x = 0.4, y = 0.8;
    double newx, newy;
    
    for (int i=0; i<200000; i++) {
      
      int n = biasedFour();
      
      if (n == 1) {
        newx = 0.85*x + 0.04*y;
        newy = -0.04*x + 0.85*y + 1.6;
        x = newx; y = newy;
      } else if (n == 2) {
        newx = -0.15*x + 0.28*y;
        newy = 0.26*x + 0.24*y + 0.44;
        x = newx; y = newy;
      } else if (n == 3) {
        newx = 0.2*x - 0.26*y;
        newy = 0.23*x + 0.22*y + 1.6;
        x = newx; y = newy;
      } else if (n == 4) {
        newx = 0;
        newy = 0.16*y;
        x = newx; y = newy;
      }
      
      f.drawPoint(x, y, Color.black);
    }
  }
  
  /* Returns 1 with probability (p) = 0.85,
   *         2 with p=0.07,
   *         3 with p=0.07, 
   *         4 with p=0.01
   */
  public static int biasedFour()
  {
    double r = Math.random();
    if (r<=0.85) return 1;
    else if (r<=0.92) return 2;
    else if (r<=0.99) return 3;
    else return 4;
  }
}
