#!/usr/bin/perl

# parameters

$hoffset=1;
$voffset=2;
$targetsize=6;    # target size in inches

$file="target";
$rho_h=.25;
$rho_v=.25;

while ($_ = $ARGV[0])
{
  shift;
  if (/^-rho_h$/)      { $rho_h=0+$ARGV[0];shift; }   # horizontal cross ratio
  elsif (/^-rho_v$/)   { $rho_v=0+$ARGV[0];shift; }   # vertical cross ratio
  elsif (/^-size$/)   { $targetsize=0+$ARGV[0];shift; }   # size in inches
  elsif (/^-o$/)      { $file=$ARGV[0];shift; }     # output filename
  else {
    warn "Usage: maketarget [-rho_h value] [-rho_v value] [-o filename]\n";
    exit;
  }
}

print "maketarget: rho_h = $rho_h, rho_v = $rho_v >$file.ps\n";


open (FILE, "|tee $file.fig | fig2dev -Lps >$file.ps") || die ("$file: $!\n");
#open (FILE, "|fig2dev -Lps >$file.ps") || die ("$file: $!\n");

# make computations

# convert inches to xfig units
$hoffset=1200*$hoffset;
$voffset=1200*$voffset;
$targetsize=1200*$targetsize;

$x2h=sqrt($rho_h)/(1+sqrt($rho_h));
$x3h=$x2h/($rho_h+$x2h-$rho_h*$x2h);

$x2v=sqrt($rho_v)/(1+sqrt($rho_v));
$x3v=$x2v/($rho_v+$x2v-$rho_v*$x2v);

$x2v=int($targetsize*$x2v+.5);
$x3v=int($targetsize*$x3v+.5);
$x2h=int($targetsize*$x2h+.5);
$x3h=int($targetsize*$x3h+.5);

$ht=$hoffset+$targetsize;
$vt=$voffset+$targetsize;
$h2=$hoffset+$x2h;
$v2=$voffset+$x2v;
$h3=$hoffset+$x3h;
$v3=$voffset+$x3v;

$htext=$hoffset;
$vtext2=$voffset-$x2v;
$vtext1=$vtext2-.2*1200;
print FILE "#FIG 3.2
Portrait
Center
Inches
Letter  
100.00
Single
-2
1200 2
2 2 0 0 0 0 50 -1 20 0.000 0 0 7 0 0 5
	 $hoffset $voffset $ht $voffset $ht $vt $hoffset $vt $hoffset $voffset
2 2 0 0 0 7 40 -1 20 0.000 0 0 -1 0 0 5
	 $h2 $v2 $h3 $v2 $h3 $v3 $h2 $v3 $h2 $v2
4 0 0 50 -1 0 12 0.0000 4 180 930 $htext $vtext1 rho_v = $rho_v\\001
4 0 0 50 -1 0 12 0.0000 4 180 930 $htext $vtext2 rho_h = $rho_h\\001
";

close FILE

