#!/usr/bin/perl
#

  
  ###############################################################
  #  Make a plot showing motion of a ball as it falls
  #
  # get the arguments
  $output_file = "falling_ball.ps";
  $term_options = "postscript enhanced color 'Helvetica,18' ";
  $cmdfile = "gnuplot.in";
  
  open (CMDFILE, ">$cmdfile") || die("can't open $cmdfile for writing");
  printf CMDFILE "set output '$output_file' \n";
  printf CMDFILE "set term $term_options \n";
  #printf CMDFILE "set grid \n";
  printf CMDFILE "set key 1.2,30.0\n";
  printf CMDFILE "set xlabel 'Time (seconds) ' \n";
  printf CMDFILE "set ylabel 'Distance (meters) ' \n";
  printf CMDFILE "set title 'A ball dropped from a high window \n";
  
  $cmd = "plot [0:2][-20:55] (50-0.5*9.8*x*x) t 'height above ground' ";
  $cmd .= "  with lines lc -1 lw 2  ";
  $cmd .= " , ";
  $cmd .= " 0.0-0.5*9.8*x*x t 'displacement from window' ";
  $cmd .= "  with lines lc -1 lt 2  lw 2  ";

  printf CMDFILE "$cmd \n";
  printf CMDFILE "set output \n";
  printf CMDFILE "quit \n";
  close(CMDFILE) ;
  
  $retval = `gnuplot < $cmdfile`;
  printf "retval is ..%s..\n", $retval;
  
  if ($term_options =~ /postscript/) {
    $psfile = $output_file;
    $giffile = $psfile;
    $giffile =~ s/.ps/.png/;
    $cmd = "convert -rotate 90 $psfile $giffile";
    printf "cmd is ..$cmd.. \n";
    $ret = `$cmd`;
  }



exit 0;
