Sierpinski.java


Below is the syntax highlighted version of Sierpinski.java from §2.2 Libraries.


/******************************************************************************
 *  Compilation:  javac Sierpinski.java
 *  Execution:    java Sierpinski n size
 *  Dependencies: StdDraw.java
 *
 *  Play chaos game on triangle to produce Sierpinski triangle.
 *  Plots n points.
 *
 *  % java Sierpinski 10000
 *
 ******************************************************************************/

public class Sierpinski {

    public static void main(String[] args) {
        int n = Integer.parseInt(args[0]);
        double[] cx   = { 0.000, 1.000, 0.500 }; 
        double[] cy   = { 0.000, 0.000, 0.866 }; 

        double x = 0.0, y = 0.0; 
        for (int i = 0; i < n; i++) { 
            int r = StdRandom.uniform(3);
            x = (x + cx[r]) / 2.0; 
            y = (y + cy[r]) / 2.0; 
            StdDraw.point(x, y); 
        } 
    }   
}


Copyright © 2000–2017, Robert Sedgewick and Kevin Wayne.
Last updated: Fri Oct 20 14:12:12 EDT 2017.