
// Histogram.java
// Robert Sedgewick and Kevin Wayne
// Slightly reformatted by Andrew Davison, Nov. 2022

/*  Create a histogram of the frequency of occurrences of values 
    in the range [0, max value) after so many trials.

   Usage:
     > compile Histogram.java
     > run Histogram <max value> <no. of trials>
   e.g.
     > run Histogram 50 1000000

 */

public class Histogram
{
  public static final int BARS = 0;    // three ways to draw the histogram
  public static final int PTS = 1;
  public static final int LINE = 2;


  private final double[] freqs;     // freqs[i] = # occurences of value i
  private double max;  // largest value in array


  public Histogram(int maxVal)
  {
    freqs = new double[maxVal+1];  // so space for 0
    max = 0;
  }


  public void addPt(int i)
  // Add one occurrence of value i
  { freqs[i]++;
    if (freqs[i] > max)
      max = freqs[i];
  }


  public void draw()
  {  draw(Histogram.BARS);  }


  public void draw(int drawType)
  // draw (and scale) the h
  {
    StdDraw.setYscale(-10, max*1.2);      // to leave space at the top

    if (drawType == Histogram.BARS)
      plotBars(freqs);
    else if (drawType == Histogram.PTS)
      plotPoints(freqs);
    else if (drawType == Histogram.LINE)
      plotLine(freqs);
    else {
      System.out.println("Did not recognise type (BARS, PTS, LINE)");
      plotBars(freqs);
    }
  }  // end of draw()


  private void plotBars(double[] a)
  // Plots bars from (0, a[i]) to (i, a[i])
  {
    StdDraw.setXscale(0, a.length-1);
    for (int i = 0; i < a.length; i++)
      StdDraw.filledRectangle(i, a[i]/2, .25, a[i]/2);
  } /* plotBars */


  private void plotPoints(double[] a)
  // Plots the points (i, a[i])
  {
    StdDraw.setXscale(0, a.length-1);
    StdDraw.setPenRadius(1.0 / (3.0 * a.length));
    for (int i = 0; i < a.length; i++)
      StdDraw.point(i, a[i]);
  } /* plotPoints */


  private void plotLine(double[] a)
  // Plots line segments connecting points (i, a[i])
  {
    StdDraw.setXscale(0, a.length-1);
    StdDraw.setPenRadius();
    for (int i = 1; i < a.length; i++)
      StdDraw.line(i-1, a[i-1], i, a[i]);
  } /* plotLine */


  // ------------------------------------------------------

  public static void main(String[] args)
  {
    if (args.length != 2) {
      System.out.println("Usage: run Histogram <max value> <no. of trials>");
      return;
    }
    int maxVal = Integer.parseInt(args[0]);      // max value
    int trials = Integer.parseInt(args[1]);      // number of trials

    // create a random histogram
    Histogram h = new Histogram(maxVal);
    for (int t = 0; t < trials; t++)
      h.addPt(randomInt(0,maxVal));

    // display using standard draw
    StdDraw.setCanvasSize(500, 100);
    h.draw();   // Histogram.LINE, Histogram.PTS
  } /* main */


  private static int randomInt(int min, int max)
  {  return (int)(Math.random()*((max-min)))+min;  }



}  // end of Histogram class
