
// Flower.java
// Robert Sedgewick and Kevin Wayne
// Slightly reformatted by Andrew Davison, Nov. 2022

/* Draws a fractal flower

   Usage:
     > compile Flower.java
     > run Flower n
   e.g.
     > run Flower 4
*/


import java.awt.Color;


public class Flower
{
  private static Color[] palette = {
    StdDraw.BLACK, StdDraw.BLUE, StdDraw.CYAN, StdDraw.DARK_GRAY,
    StdDraw.GRAY, StdDraw.GREEN, StdDraw.LIGHT_GRAY, 
    StdDraw.MAGENTA, StdDraw.ORANGE, StdDraw.PINK, 
    StdDraw.RED, StdDraw.WHITE, StdDraw.YELLOW
  };


  public static void main(String args[])
  {
    if (args.length != 1) {
      System.out.println("Usage: run Flower <depth>");
      return;
    }
    int n = Integer.parseInt(args[0]);     // depth

    for (int i = 0; i < palette.length; i++)
      palette[i] = makeTransparent(palette[i], 70);

    // Center is at (0.5, 0.5), half-width 0.3, half-height 0.5, depth is n
    flower(0.5, 0.5, 0.35, 0.5, n);

    System.out.println("Drawing finished");
  }  // end of main()


  private static Color makeTransparent(Color c, int alpha)
  {  return new Color(c.getRed(), c.getGreen(), c.getBlue(), alpha);  }



  private static void flower(double x, double y,
                             double w, double h, int depth)
  /* (x,y): the center of this ellipse
     w: half the width of this ellipse
     h: half the height of this ellipse
     depth: depth of the recursion
  */
  {
    if (depth == 0)
      return;

    int idx = (int)(Math.random() * palette.length);
    StdDraw.setPenColor(palette[idx]);

    StdDraw.filledEllipse(x, y, w, h);

    flower(x+w/2, y, w/2, h/2, depth-1);
    flower(x - w/2, y, w/2, h/2, depth-1);

    flower(x, y - (w/1.21), w/2, h/2, depth-1);
    flower(x, y+(w/1.21), w/2, h/2, depth-1);
  }  // end of flower()


}  // end of Flower class
