
// Rose.java
// Robert Sedgewick and Kevin Wayne
// Slightly reformatted by Andrew Davison, Nov. 2022

// https://introcs.cs.princeton.edu/java/15inout/Rose.java.html

/* Plots an n petal rose (if n is odd) and a 2n-petal rose if n is
   even.

   Usage:
     > compile Rose.java
     > run Rose n
   e.g.
     > run Rose 6
*/


public class Rose
{

  public static void main(String[] args)
  {
    if (args.length != 1) {
      System.out.println("Usage: run Rose <no. of petals>");
      return;
    }

    int n = Integer.parseInt(args[0]);
    StdDraw.setXscale(-1, +1);
    StdDraw.setYscale(-1, +1);
    StdDraw.setPenColor(StdDraw.PINK);

    double x0 = 0, y0 = 0;
    for (double t = 0.0; t <= 360.0; t += 0.1) {
      double theta = Math.toRadians(t);
      double r = Math.sin(n * theta);
      double x1 = r * Math.cos(theta);
      double y1 = r * Math.sin(theta);
      StdDraw.line(x0, y0, x1, y1);
      x0 = x1;
      y0 = y1;
    }
  } /* main */

}  // end of Rose class
