#!/usr/bin/perl -w
use Tk;
use Math::Trig;

my $mw=MainWindow->new;
$mw->title("Doubles V1.0");
$mw->resizable(0,0);

$c0=$mw->Frame(-relief=>'groove',
	       -borderwidth=>2,
	       -label=>"Copier/coller des éléments")
	       ->pack(-side=>'top',-padx=>8, -pady=>8);

$c1=$mw->Frame(-relief=>'groove',
	       -borderwidth=>2)->pack(-side=>'top',-padx=>8, -pady=>8);


$c2=$mw->Frame(-relief=>'groove',
	       -borderwidth=>2,
	       -label=>"Année?")
  ->pack(-side=>'top', -pady=>5);


$c3=$mw->Frame(-relief=>'groove',
	       -borderwidth=>2)
	       #-label=>"Position")
  ->pack(-side=>'top', -pady=>5);



$c4=$mw->Frame->pack(-side=>'bottom');

$c0->Entry(-width=>32,-textvariable=>\$texte)->pack(-side=>'left');
$c0->Button(-text=>"Valider", -command=>\&validation)->pack((-side=>'left'));
$c0->Button(-text=>"Raz", -command=>\&raz)->pack((-side=>'left'));

$c1->Entry(-width=>6,-textvariable=>\$P0)->grid(
$c1->Entry(-width=>6,-textvariable=>\$T0),
$c1->Entry(-width=>6,-textvariable=>\$e0),
$c1->Entry(-width=>6,-textvariable=>\$a0),
$c1->Entry(-width=>6,-textvariable=>\$i0),
$c1->Entry(-width=>6,-textvariable=>\$GO0),
$c1->Entry(-width=>6,-textvariable=>\$po0));

$c1->Label(-text=>'P')->grid(
$c1->Label(-text=>'T'),
$c1->Label(-text=>'e'),
$c1->Label(-text=>'a'),
$c1->Label(-text=>'i'),
$c1->Label(-text=>'GO'),
$c1->Label(-text=>'po'));



$c2->Entry(-width=>5,textvariable=>\$t)->pack(-side=>'top',
				   -expand=>1,
				   -padx=>5,
				   -pady=>3);

$c2->Button(-text => "Calcul",-command=>\&calcul)->pack(-side => 'top',
				                        -padx=>5,
							-pady=>5);


$c3->Label(-textvariable=>\$echelle)->pack;
$taille=120;
$canevas=$c3->Canvas(-height=>$taille,-width=>$taille,-background=>'grey')->pack;
$canevas->createOval(1,1,$taille,$taille,-fill=>'black');

$c3->Label(-textvariable=>\$result_ang)->pack(-side => 'left');
$c3->Label(-textvariable=>\$result_sep)->pack(-side => 'right');

$c4->Button(-text => "Sortie",-command=>sub{exit})->pack(-side => 'bottom',
						      -padx=>5,
						      -pady=>5);



MainLoop;

sub calcul
{
  if ($e0 <0 or $e0>=1)
    {
      return;
    }

  $P=$P0; $i=$i0; $T=$T0; $GO=$GO0; $e=$e0; $po=$po0; $a=$a0;
  $i=deg2rad($i); # tranformation en radians
  $GO=deg2rad($GO); # tranformation en radians
  $po=deg2rad($po); # tranformation en radians

  $n=360.0/$P;
  $M=$n*($t-$T);
  $M=deg2rad($M);
  $E=&kepler($M,$e);
  $r=$a*(1-$e*cos($E));
  $tmp=sqrt((1+$e)/(1-$e))*tan($E/2);
  $v=atan($tmp)*2;

  $theta_om=atan(sin($v+$po)*cos($i)/cos($v+$po));
  $theta=$theta_om+$GO;
  $rho=$r*cos($v+$po)/cos($theta-$GO);
  if ($rho<0)
    {
      $rho=-$rho;
      $theta=$theta+deg2rad(180);
    }

  $result_sep=sprintf "%2.1f\"",$rho;
  $result_ang=sprintf "%3.0f°", (rad2deg($theta)*100%36000)/100;


  $coeff2=$e*0.5+0.5;
  $coeff1=$taille/(4*$a*$coeff2);

  $centrex=(cos($theta-deg2rad(90))*$rho*$coeff1*0.9)+($taille/2);
  $centrey=-(sin($theta-deg2rad(90))*$rho*$coeff1*0.9)+($taille/2);

  $echelle=sprintf "<----  %2.1f \" ---->", $a*4*$coeff2;

  $canevas->createOval(1,1,$taille,$taille,-fill=>'black');
  $canevas->createOval(($taille/2)-10,($taille/2)-10,($taille/2)+10,($taille/2)+10,-fill=>'yellow');
  $canevas->createOval($centrex+5,$centrey+5,$centrex-5,$centrey-5,
		       -fill=>'blue');

}

sub kepler
  {
    my ($MK,$ek)=@_;
    my $EK=$MK;
    my $E_VIEUX=9999999.;
    while (abs($EK-$E_VIEUX) > 0.00000001)
      {
	$E_VIEUX=$EK;
	$EK=$MK+$ek*sin($EK);
      }
    return $EK;
  }

sub validation
  {
    @champ=split /\s+/,$texte;

    $P0=$champ[1];
    $T0=$champ[3];
    $e0=$champ[4];
    $a0=$champ[5];
    $i0=$champ[6];
    $GO0=$champ[7];
    $po0=$champ[8];
  }

sub raz
  {
    $texte='';
  }
