# JStrack:  copyright 1997/2010 by Jim Graham, N5IAL, all rights reserved.
#

# This procedure accepts a latitude (dec.) and returns the y coordinate.

proc set_latt {latt} {
   global min_long max_long min_latt max_latt
   global ybase

   if {$latt <= $min_latt} { return $ybase($min_latt) }
   if {$latt >= $max_latt} { return $ybase($max_latt) }

   for {set low $min_latt} {$low < $max_latt} {incr low 2} {
      set high [expr {$low + 2}]
      if {$latt >= $low && $latt < $high} {
         set base_latt $low
         set next_latt $high
         break
      }
   }

   set vary [expr {($ybase($base_latt) - $ybase($next_latt)) / 2}]
   return [expr {$ybase($base_latt) - (($latt - $base_latt) * $vary)}]
}


# This procedure accepts a y coordinate and returns the latitude (dec.).

proc get_latt {y} {
   global min_long max_long min_latt max_latt
   global ybase

   set base_latt -1 ; set next_latt -1

   for {set low $min_latt} {$low < $max_latt} {incr low 2} {
      set high [expr {$low + 2}]
      if {$y > $ybase($high)} {
         set base_latt $low
         set next_latt $high
         break
      }
   }

   if {$base_latt < 0 || $next_latt < 0} { return -1 }

   set vary [expr {($ybase($base_latt) - $ybase($next_latt)) / 2}]
   return [format %0.2f [expr {(($ybase($base_latt) - $y) / $vary) + $base_latt}]]
}

# This procedure accepts a longitude (dec.) and returns the x coordinate.

proc set_long {long} {
   global min_long max_long min_latt max_latt
   global xbase

   if {$long <=  $min_long} { return $xbase($min_long) }
   if {$long >= $max_long} { return $xbase($max_long) }

   for {set low $min_long} {$low < $max_long} {incr low 2} {
      set high [expr {$low + 2}]
      if {$long >= $low && $long < $high} {
         set base_long $low
         set next_long $high
         break
      }
   }

   set varx [expr {($xbase($base_long) - $xbase($next_long)) / 2}]
   return [expr {$xbase($base_long) - (($long - $base_long) * $varx)}]
}

# This procedure accepts an x coordinate and returns the longitude (dec.).

proc get_long {x} {
   global min_long max_long min_latt max_latt
   global xbase

   set base_long -1 ; set next_long -1

   for {set low $min_long} {$low < $max_long} {incr low 2} {
      set high [expr {$low + 2}]
      if {$x > $xbase($high)} {
         set base_long $low
         set next_long $high
         break
      }
   }

   if {$base_long < 0 || $next_long < 0} { return -1 }

   set varx [expr {($xbase($base_long) - $xbase($next_long)) / 2}]
   return [format %0.2f [expr {(($xbase($base_long) - $x) / $varx) + $base_long}]]
}


# This procedure determines whether or not the coordinates are on the
# tracking chart.  Returns 1 if they are, 0 if they aren't.

proc valid_coords {latt long} {
   global min_long max_long min_latt max_latt
   global plot_err

   if {$latt < $min_latt && $long < $min_long} { set plot_err 11 ; return 0 }
   if {$latt < $min_latt && $long > $max_long} { set plot_err 12 ; return 0 }
   if {$latt > $max_latt && $long < $min_long} { set plot_err 13 ; return 0 }
   if {$latt > $max_latt && $long > $max_long} { set plot_err 14 ; return 0 }
   if {$latt < $min_latt} { set plot_err 1 ; return 0 }
   if {$latt > $max_latt} { set plot_err 2 ; return 0 }
   if {$long < $min_long} { set plot_err 3 ; return 0 }
   if {$long > $max_long} { set plot_err 4 ; return 0 }
   set plot_err 0
   return 1
}

