
// PlotFilter.java
// Robert Sedgewick and Kevin Wayne
// Slightly modified by Andrew Davison, Nov. 2022

// https://introcs.cs.princeton.edu/java/15inout/PlotFilter.java.html

/* Plot points read from a text file.

   Usage:
     > compile PlotFilter.java
     > run PlotFilter <text-file>
   e.g.
     > run PlotFilter USA.txt

  Data files: https://introcs.cs.princeton.edu/java/15inout/USA.txt

*/

import java.io.*;
import java.util.*;


public class PlotFilter
{

  public static void main(String[] args) throws IOException
  {
    if (args.length != 1) {
      System.out.println("Usage: run PlotFilter <text-file>");
      return;
    }

    Scanner sc = new Scanner(new FileReader(args[0]));

    // read in bounding box and rescale
    double x0 = sc.nextDouble();
    double y0 = sc.nextDouble();
    double x1 = sc.nextDouble();
    double y1 = sc.nextDouble();
    StdDraw.setXscale(x0, x1);
    StdDraw.setYscale(y0, y1);

    // for bigger points
    StdDraw.setPenRadius(0.005);

    // to speed up performance, defer displaying points
    StdDraw.enableDoubleBuffering();

    // plot points, one at a time
    while (sc.hasNext()) {
      double x = sc.nextDouble();
      double y = sc.nextDouble();
      StdDraw.point(x, y);
    }

    // display all points now
    StdDraw.show();
    System.out.println("Plot finished");

    sc.close();
  } /* main */


}
