CoveragePicture.java


Below is the syntax highlighted version of CoveragePicture.java.


/*************************************************************************
  *  Name:    Donna Gabai
  *  Login:   dgabai 
  *  Precept: P01
  *  Fall 2011 exam 2
  * 
  *  Compilation:  javac CoveragePicture.java
  *  Execution:    java CoveragePicture < input.txt
  * 
  *  Dependencies: Tower, Queue, Picture, StdIn
  *  Description: Reads data for a set of cell towers from standard input.
  *               Visualizes the coverage for the set of towers.
  ************************************************************************/
import java.awt.Color;

public class CoveragePicture {
    public static void main(String[] args) {
        // set up a Queue of Towers
        Queue<Tower> qt = new Queue<Tower>();
        
        // read from standard input and construct each Tower
        while (!StdIn.isEmpty()) {
            double x0 = StdIn.readDouble();
            double y0 = StdIn.readDouble();
            double r  = StdIn.readDouble();
            Tower t1 = new Tower(x0, y0, r);
            qt.enqueue(t1);
        }
       
        // set up picture
        int width = 512;
        int ht = 512;
        Picture pic = new Picture(width, ht);
        
        // for each pixel, check if in range of any tower
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < ht; j++) {
                // for each tower in queue
                boolean inRange = false;
                for (Tower t : qt) {
                    if (t.inRange((double) i/width, 1.0 - (double) j/ht)) {
                        inRange = true;
                        // no need to keep checking this pixel
                        break;         
                    }
                }
                if (inRange) pic.set(i, j, Color.BLACK);
                else         pic.set(i, j, Color.WHITE);
            }
        }
        
        // display the coverage
        pic.show();
    }
}