### ----------------------------------------------------
### -- Geo::Ellipsoid
### -- Licenses: Artistic-2.0
### -- Authors: Tom Browder <tbrowder@acm.org> (translator Perl 5 to Raku, current maintainer), Jim Gibson (CPAN Perl 5 author, v1.12)
### -- File: gentest/gentest-ellipsoid.raku
### ----------------------------------------------------
### -- Chunk 2 of 2
( $lat1, $lon1, $lat2, $lon2 );
  my( $range2, $bearing2 ) = $ellipsoid2->to( $dlat1, $dlon1, $dlat2, $dlon2 );
  #$bearing *= $degrees_per_radian;
  print "displacement() returns (@d)\n" if $debug;

  printf "Rad: Range = %.4f m., bearing = %.4f rad.\n", $range, $bearing;
  printf "Rad: East = %.4f m., north = %.4f m.\n", @d;

  printf "Deg: Range = %.4f m., bearing = %.4f deg.\n", $range2, $bearing2;
  printf "Deg: East = %.4f m., north = %.4f m.\n", @e;
}

sub print_target
{
  my( $lat1deg, $lat1min, $lat1sec, 
      $lon1deg, $lon1min, $lon1sec, 
      $range, $degrees ) = @_;

  my $lat1 = Angle($lat1deg,$lat1min,$lat1sec);
  my $lon1 = Angle($lon1deg,$lon1min,$lon1sec);

  my @here = ( $lat1, $lon1 );
  my $ellipsoid = Geo::Ellipsoid->new(
    units=>'radians',
    ellip=>'WGS84',
    debug=>$debug
  );

  my $radians = $degrees / $degrees_per_radian;
  my $x = $range*sin($radians);
  my $y = $range*cos($radians);

  my @d = ($x,$y);

  my @there = $ellipsoid->at(@here,@d);

  printf "Starting at (%.8f,%.8f)\n", @here;
  printf "and going (x=%.4f m.,y=%.4f m.)\n", @d;
  printf "or (%.8f  m.,%.8f deg.)\ngives (%.8f,%.8f)\n", $range, $degrees,
    @there;
}

#	Angle
#
#	Return angle in radians given the list 
#	( degree, minute, second, hundreths-of-second)
#
sub Angle
{
  my $deg = shift || 0;
  my $min = shift || 0;
  my $sec = shift || 0;
  my $csec = shift || 0;	# optional 100th's of a second

  #print "convert (@_) to angle in radians\n" if $debug;
  my $frac = ( $min + (($sec + ($csec/100))/60))/60;
  my $angle = $deg;
  #print "  angle=$angle, frac=$frac\n" if $debug;
  if( $angle < 0 ) {
    $angle += 360 - $frac;
  }else{
    $angle += $frac;
  }
  #print "  angle.frac = $angle\n" if $debug;
  return $angle/$degrees_per_radian;
}

sub polar
{
  my( $x, $y ) = @_;
  my $range = sqrt( $x*$x + $y*$y );
  my $bearing = $halfpi - atan2($y,$x);
  return ($range, $bearing);
}
 


