#!/usr/bin/perl
#

$xmin = 0;
$xmax = 65;
$ymin = 0;
$ymax = 65;


$v = 0.9;
$gamma = sqrt( 1.0 / (1.0 - $v*$v) );


if (0 == 1) {
 # make several plots, each showing the line of Earth-simultaneity
 #    as it moves forward in time
 #    2500 - 2518
 

  
  my($output_file, $term_options);

  for ($time = 0.0; $time < 20.0; $time += 2.0) {

    # create a file for plotting
    $datafile = "make_plot_series.dat";
    open(DATAFILE, ">$datafile") || die("can't open file $datafile");
    printf DATAFILE " %lf %lf \n", $xmin, $time;
    printf DATAFILE " %lf %lf \n", $xmax, $time;
    close(DATAFILE);
    

    # get the arguments
    $output_file = sprintf "simul_earth_%04.1f.ps", $time;
    $term_options = "postscript color enhanced ";

    $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 size square \n";
    printf CMDFILE "set grid \n";
    printf CMDFILE "set key top right \n";
    printf CMDFILE "set xlabel 'Position (years) ' \n";
    printf CMDFILE "set ylabel 'Time on Earth since 2500 (years) ' \n";
    printf CMDFILE "set title 'Twin paradox '\n";
    $label_text = sprintf "Earth time %.0f ", 2500 + $time;
    printf CMDFILE "set label 1 '$label_text' at 21,55 font 'Helvetica,24' \n";
  
    $cmd = "plot [$xmin:$xmax][$ymin:$ymax] ";
    $cmd .= " '$datafile' using 1:2 with lines lt 3 lw 10  t ''  ";
  
   
  
    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/.eps/.gif/;
      $giffile =~ s/.ps/.gif/;
      $cmd = "convert -flatten -background white -rotate 90 $psfile $giffile";
      printf "cmd is ..$cmd.. \n";
      $ret = `$cmd`;
    }
  
  }

}


if (0 == 1) {
 # make several plots, each showing the line of Earth-simultaneity
 #    as it moves forward in time
 #    2530 - 2548
 

  
  my($output_file, $term_options);

  for ($time = 30.0; $time < 50.0; $time += 2.0) {

    # create a file for plotting
    $datafile = "make_plot_series.dat";
    open(DATAFILE, ">$datafile") || die("can't open file $datafile");
    printf DATAFILE " %lf %lf \n", $xmin, $time;
    printf DATAFILE " %lf %lf \n", $xmax, $time;
    close(DATAFILE);
    

    # get the arguments
    $output_file = sprintf "simul_earth_%04.1f.ps", $time;
    $term_options = "postscript color enhanced ";

    $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 size square \n";
    printf CMDFILE "set grid \n";
    printf CMDFILE "set key top right \n";
    printf CMDFILE "set xlabel 'Position (years) ' \n";
    printf CMDFILE "set ylabel 'Time on Earth since 2500 (years) ' \n";
    printf CMDFILE "set title 'Twin paradox '\n";
    $label_text = sprintf "Earth time %.0f ", 2500 + $time;
    printf CMDFILE "set label 1 '$label_text' at 21,55 font 'Helvetica,24' \n";
  
    $cmd = "plot [$xmin:$xmax][$ymin:$ymax] ";
    $cmd .= " '$datafile' using 1:2 with lines lt 3 lw 10  t ''  ";
  
   
  
    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/.eps/.gif/;
      $giffile =~ s/.ps/.gif/;
      $cmd = "convert -flatten -background white -rotate 90 $psfile $giffile";
      printf "cmd is ..$cmd.. \n";
      $ret = `$cmd`;
    }
  
  }

}


if (0 == 1) {
 # make several plots, each showing the line of Earth-simultaneity
 #    as it moves forward in time
 #    2560 - 2566
 

  
  my($output_file, $term_options);

  for ($time = 60.0; $time < 66.0; $time += 2.0) {

    # create a file for plotting
    $datafile = "make_plot_series.dat";
    open(DATAFILE, ">$datafile") || die("can't open file $datafile");
    printf DATAFILE " %lf %lf \n", $xmin, $time;
    printf DATAFILE " %lf %lf \n", $xmax, $time;
    close(DATAFILE);
    

    # get the arguments
    $output_file = sprintf "simul_earth_%04.1f.ps", $time;
    $term_options = "postscript color enhanced ";

    $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 size square \n";
    printf CMDFILE "set grid \n";
    printf CMDFILE "set key top right \n";
    printf CMDFILE "set xlabel 'Position (years) ' \n";
    printf CMDFILE "set ylabel 'Time on Earth since 2500 (years) ' \n";
    printf CMDFILE "set title 'Twin paradox '\n";
    $label_text = sprintf "Earth time %.0f ", 2500 + $time;
    printf CMDFILE "set label 1 '$label_text' at 21,45 font 'Helvetica,24' \n";
  
    $cmd = "plot [$xmin:$xmax][$ymin:$ymax] ";
    $cmd .= " '$datafile' using 1:2 with lines lt 3 lw 10  t ''  ";
  
   
  
    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/.eps/.gif/;
      $giffile =~ s/.ps/.gif/;
      $cmd = "convert -flatten -background white -rotate 90 $psfile $giffile";
      printf "cmd is ..$cmd.. \n";
      $ret = `$cmd`;
    }
  
  }

}




if (1 == 1) {
 # make several plots, each showing the line of red-fleet-simultaneity
 #    as it moves forward in time
 #    2500 - 2520
 

  
  my($output_file, $term_options);

  for ($time = 0.0; $time < 14.0; $time += 2.0) {

    # we use two points to define line of ship simultaneity
    #   (the end points in space are -100 and +100 because they
    #    lie safely beyond edges of our graph)
    #
    #   one point is         x1 = -100,   t1 = $time
    #   the other point is   x2 =  100,   t2 = $time
    #
    # we need to convert those two points into Earth-frame position and time
    for ($x1 = -100; $x1 < 0; $x1 += 0.1) {

      $t1 = $time;
      $x2 = 100;    
      $t2 = $time;

      # convert to Earth-frame via Lorentz
      $e_x1 = $gamma*($x1 + $v*$t1);
      $e_t1 = $gamma*($t1 + $v*$x1);
      $e_x2 = $gamma*($x2 + $v*$t2);
      $e_t2 = $gamma*($t2 + $v*$x2);

      if ($e_x1 < 0.1) {
        # keep going through loop until we reach a positive e_x1
      }
      else {
        last;
      }
    }
    
    # create a file for plotting, with the Earth-frame points
    $datafile = "make_plot_series.dat";
    open(DATAFILE, ">$datafile") || die("can't open file $datafile");
    printf DATAFILE " %lf %lf \n", $e_x1, $e_t1;
    printf DATAFILE " %lf %lf \n", $e_x2, $e_t2;
    close(DATAFILE);

    # and this datafile will have the world line of the red ship
    $shipfile = "make_plot_series.ship.dat";
    open(SHIPFILE, ">$shipfile") || die("can't open file $shipfile");
    printf SHIPFILE " %lf %lf \n", 0.0, 0.0;
    printf SHIPFILE " %lf %lf \n", 27.0, 30.0;
    close(SHIPFILE);

    

    # get the arguments
    $output_file = sprintf "simul_red_%04.1f.ps", $time;
    $term_options = "postscript color enhanced ";

    $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 size square \n";
    printf CMDFILE "set grid \n";
    printf CMDFILE "set key top right \n";
    printf CMDFILE "set xlabel 'Position (years) ' \n";
    printf CMDFILE "set ylabel 'Time on Earth since 2500 (years) ' \n";
    printf CMDFILE "set title 'Twin paradox '\n";
    $label_text = sprintf "Red ship time %.0f ", 2500 + $time;
    printf CMDFILE "set label 1 '$label_text' at 5,55 font 'Helvetica,24' \n";
  
    $cmd = "plot [$xmin:$xmax][$ymin:$ymax] ";
    $cmd .= " '$datafile' using 1:2 with lines lc 1 lt 3 lw 10  t ''  ";
    $cmd .= " , ";
    $cmd .= " '$shipfile' using 1:2 with lines lt -1 lw 2 t '' ";
   
  
   
  
    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/.eps/.gif/;
      $giffile =~ s/.ps/.gif/;
      $cmd = "convert -flatten -background white -rotate 90 $psfile $giffile";
      printf "cmd is ..$cmd.. \n";
      $ret = `$cmd`;
    }
  
  }

}




exit 0;

