PUHeatMap.java


Below is the syntax highlighted version of PUHeatMap.java.


/* Christopher Moretti
 *  cmoretti
 *  P01A/P06
 * 
 * takes resolution and map filename on CL
 * reads lat/lon coordinates from StdIn
 * prints total points and filled cells to StdOut
 * draws colored heatmap on StdDraw
 * 
 * Dependencies: StdIn, StdOut, StdDraw
 */

public class PUHeatMap {

    // sets the pen color according to the ratio of
    // points at this location (here) versus all points
    private static void setColor(int here, int all) {
        StdDraw.setPenColor(StdDraw.BLUE);
        if(here > .001*all)
            StdDraw.setPenColor(StdDraw.YELLOW);
        if(here > .003*all)
            StdDraw.setPenColor(StdDraw.ORANGE);
        if(here > .005*all)
            StdDraw.setPenColor(StdDraw.PINK);
        if(here > .007*all)
            StdDraw.setPenColor(StdDraw.MAGENTA);
        if(here >= .009*all)
            StdDraw.setPenColor(StdDraw.RED);
    }
    
    public static void main(String[] args) {

        //resolution
        int N = Integer.parseInt(args[0]);
        
        // map filename
        String PHOTO = args[1];
    
        // map extremes
        double minX = -74.66443;
        double minY = 40.33855;
        double maxX = -74.64564;
        double maxY = 40.35281;

        // set scale from 0->N in both dimensions
        // and draw picture centered and scaled.
        StdDraw.setXscale(0, N);
        StdDraw.setYscale(0, N);
        StdDraw.picture(N/2, N/2, PHOTO, N, N);

        // matrix of possible grid locations
        int[][] arr = new int[N][N];
        
        // counters for printed statistics
        int totalPoints = 0;
        int nonZeroCells = 0;
        
        // read in all lat/lons from StdIn
        while(!StdIn.isEmpty()) {
            double y = StdIn.readDouble();
            double x = StdIn.readDouble();

            // error-checking code, you don't need this, we guarantee it!
            if(x > maxX || x < minX || y > maxY || y < minY) {
                StdOut.println("Outside map: ("+x+","+y+")");
            }
            else {
                // scale lat/lon to 0-1
                double yCoord = (y - minY)*(1/(maxY-minY));
                double xCoord = (x - minX)*(1/(maxX-minX));
                
                // increment grid cell count and total count
                arr[(int)(xCoord*N)][(int)(yCoord*N)]++;
                totalPoints++;
            }
        }

        // for all filled grid cells, set the color based 
        // on the value then draw a circle at that cell
        StdDraw.show(0);
        for(int i = 0 ; i < N; i++) {
            for(int j = 0 ; j < N; j++) {
                if(arr[i][j] > 0) {
                    nonZeroCells++;
                    setColor(arr[i][j], totalPoints);
                    StdDraw.filledCircle(i,j,.01*N);
                }
            }
        }
        StdDraw.show();

        // print statistics
        StdOut.println("Total points: "+totalPoints);
        StdOut.println("Filled cells: "+nonZeroCells);
    }
}