Star.java


Below is the syntax highlighted version of Star.java from §1.5 Input and Output.


/******************************************************************************
 *  Compilation:  javac Star.java
 *  Execution:    java Star
 *  Dependencies: StdDraw.java
 *
 *  Draw the spirograph star on the cover of the book.
 *
 ******************************************************************************/

public class Star {

    public static void main(String[] args) {
        int n = Integer.parseInt(args[0]);
        double R =  Math.toRadians(35.0);
        double r =  Math.toRadians(-14.0);
        double a =  Math.toRadians(31.0);

        StdDraw.setXscale(-1, +1);
        StdDraw.setYscale(-1, +1);


        for (int i = 0; i <= n; i++) {
            double t = 4 * Math.PI * i / n;
            double x = (R+r) * Math.cos(t) - (r+a) * Math.cos(((R+r)/r)*t);
            double y = (R+r) * Math.sin(t) - (r+a) * Math.sin(((R+r)/r)*t);
            StdDraw.point(x, y);
        }
    }
}


Copyright © 2000–2022, Robert Sedgewick and Kevin Wayne.
Last updated: Thu Aug 11 10:14:17 EDT 2022.