HadamardPlot.java


This is the syntax highlighted version of HadamardPlot.java from 2.5 Arrays of
Introduction to Computer Science by Robert Sedgewick and Kevin Wayne.


/*************************************************************************
 *  Compilation:  javac HadamardPlot.java
 *  Execution:    java HadamardPlot 64
 *  Dependencies: StdDraw.java
 *  
 *  Plots the Hadamard matrix of order N. Assumes N is a power of 2.
 *
 *  Todo:  simplify with StdDraw.setScale.
 *
 *************************************************************************/

import java.awt.Color;

public class HadamardPlot { 
    public static void main(String[] args) { 
        int N = Integer.parseInt(args[0]);
        boolean[][] H = new boolean[N][N];

        // initialize Hadamard matrix of order N
        H[0][0] = true;
        for (int n = 1; n < N; n += n) {
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    H[i+n][j]   =  H[i][j];
                    H[i][j+n]   =  H[i][j];
                    H[i+n][j+n] = !H[i][j];
                }
            }
        }


        // plot it
        int SIZE = 512;
        StdDraw.create(SIZE, SIZE);
        StdDraw.clear(Color.black);
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                StdDraw.go((i + 0.5) * SIZE / N, SIZE - (j + 0.5) * SIZE / N);
                if (H[i][j]) StdDraw.setColor(Color.black);
                else         StdDraw.setColor(Color.white);
                StdDraw.spot(1.0 * SIZE / N - 2, 1.0 * SIZE / N - 2);
            }
        }
        StdDraw.show();
    }

}


Last updated: Sat Aug 7 07:50:01 EDT 2004 .
Copyright © 2004, Robert Sedgewick and Kevin Wayne.