#!/usr/bin/perl
#
# create a datafile which can be plotted as a color map 
#    showing a two-slit interference pattern
#
# MWR 4/21/2019

$mypi = 3.14159;

# size of the region between walls
$xwidth = 20.0;
$ywidth = 20.0;
# spacing of the two slits
$d = 1.5;
# holes are at x=0 and these y values
$y_1 = ($ywidth/2.0) + ($d/2.0);
$y_2 = ($ywidth/2.0) - ($d/2.0);

# wavelength 
$lambda = 0.1;
# wave number
$k = (2.0*$mypi)/$lambda;

$amp = 1.0;

$xmin = 0;
$xmax = $xwidth;
$ymin = 0.0;
$ymax = $ywidth;
$step = 0.02;

# print header to help interpret later
printf "#  spacing d = %8.3f \n", $d;
printf "#  lambda    = %8.3f \n", $lambda;
printf "# \n";

for ($x = $xmin; $x < $xmax; $x += $step) {

  for ($y = $ymin; $y < $ymax; $y += $step) {

    $xmid = $x + ($step/2.0);
    $ymid = $y + ($step/2.0);

    # compute the radius length r_1
    $dx = $xmid - 0.0;
    $dy = $ymid - $y_1;
    $r_1 = sqrt( ($dx*$dx) + ($dy*$dy) );

    # compute the radius length r_2
    $dx = $xmid - 0.0;
    $dy = $ymid - $y_2;
    $r_2 = sqrt( ($dx*$dx) + ($dy*$dy) );

    # compute the average radius length r_avg
    $r_avg = 0.5*($r_1 + $r_2);
    if ($debug > 0) {
      printf " x %6.3f  y %6.3f  r_1 %8.3f  r_2 %8.3f \n", $xmid, $ymid, $r_1, $r_2;
    }

    # sum of the two waves, not including radial decrease
    $sum = $amp * cos( $k*($r_1 - $r_2)/2.0 );
    # sum of the two waves, including radial decrease
    $r_sum = $sum / $r_avg;


    printf " %8.4f %8.4f %8.4f \n", $xmid, $ymid, $sum;

  }

  printf "\n";

}


exit 0;
