ÿþ/ *   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -     * / 
 / *     L a t i t u d e / l o n g i t u d e   s p h e r i c a l   g e o d e s y   f o r m u l a e   &   s c r i p t s   ( c )   C h r i s   V e n e s s   2 0 0 2 - 2 0 1 0                         * / 
 / *       -   w w w . m o v a b l e - t y p e . c o . u k / s c r i p t s / l a t l o n g . h t m l                                                                                                 * / 
 / *                                                                                                                                                                                                 * / 
 / *     S a m p l e   u s a g e :                                                                                                                                                                   * / 
 / *         v a r   p 1   =   n e w   L a t L o n ( 5 1 . 5 1 3 6 ,   - 0 . 0 9 8 3 ) ;                                                                                                             * / 
 / *         v a r   p 2   =   n e w   L a t L o n ( 5 1 . 4 7 7 8 ,   - 0 . 0 0 1 5 ) ;                                                                                                             * / 
 / *         v a r   d i s t   =   p 1 . d i s t a n c e T o ( p 2 ) ;                     / /   i n   k m                                                                                           * / 
 / *         v a r   b r n g   =   p 1 . b e a r i n g T o ( p 2 ) ;                       / /   i n   d e g r e e s   c l o c k w i s e   f r o m   n o r t h                                       * / 
 / *         . . .   e t c                                                                                                                                                                           * / 
 / *   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -     * / 
 
 / *   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -     * / 
 / *     N o t e   t h a t   m i n i m a l   e r r o r   c h e c k i n g   i s   p e r f o r m e d   i n   t h i s   e x a m p l e   c o d e !                                                       * / 
 / *   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -     * / 
 
 
 / * * 
   *   C r e a t e s   a   p o i n t   o n   t h e   e a r t h ' s   s u r f a c e   a t   t h e   s u p p l i e d   l a t i t u d e   /   l o n g i t u d e 
   * 
   *   @ c o n s t r u c t o r 
   *   @ p a r a m   { N u m b e r }   l a t :   l a t i t u d e   i n   n u m e r i c   d e g r e e s 
   *   @ p a r a m   { N u m b e r }   l o n :   l o n g i t u d e   i n   n u m e r i c   d e g r e e s 
   *   @ p a r a m   { N u m b e r }   [ r a d = 6 3 7 1 ] :   r a d i u s   o f   e a r t h   i f   d i f f e r e n t   v a l u e   i s   r e q u i r e d   f r o m   s t a n d a r d   6 , 3 7 1 k m 
   * / 
 f u n c t i o n   L a t L o n ( l a t ,   l o n ,   r a d )   { 
     i f   ( t y p e o f ( r a d )   = =   ' u n d e f i n e d ' )   r a d   =   6 3 7 1 ;     / /   e a r t h ' s   m e a n   r a d i u s   i n   k m 
     / /   o n l y   a c c e p t   n u m b e r s   o r   v a l i d   n u m e r i c   s t r i n g s 
     t h i s . _ l a t   =   t y p e o f ( l a t ) = = ' n u m b e r '   ?   l a t   :   t y p e o f ( l a t ) = = ' s t r i n g '   & &   l a t . t r i m ( ) ! = ' '   ?   + l a t   :   N a N ; 
     t h i s . _ l o n   =   t y p e o f ( l a t ) = = ' n u m b e r '   ?   l o n   :   t y p e o f ( l o n ) = = ' s t r i n g '   & &   l o n . t r i m ( ) ! = ' '   ?   + l o n   :   N a N ; 
     t h i s . _ r a d i u s   =   t y p e o f ( r a d ) = = ' n u m b e r '   ?   r a d   :   t y p e o f ( r a d ) = = ' s t r i n g '   & &   t r i m ( l o n ) ! = ' '   ?   + r a d   :   N a N ; 
 } 
 
 
 / * * 
   *   R e t u r n s   t h e   d i s t a n c e   f r o m   t h i s   p o i n t   t o   t h e   s u p p l i e d   p o i n t ,   i n   k m   
   *   ( u s i n g   H a v e r s i n e   f o r m u l a ) 
   * 
   *   f r o m :   H a v e r s i n e   f o r m u l a   -   R .   W .   S i n n o t t ,   " V i r t u e s   o f   t h e   H a v e r s i n e " , 
   *               S k y   a n d   T e l e s c o p e ,   v o l   6 8 ,   n o   2 ,   1 9 8 4 
   * 
   *   @ p a r a m       { L a t L o n }   p o i n t :   L a t i t u d e / l o n g i t u d e   o f   d e s t i n a t i o n   p o i n t 
   *   @ p a r a m       { N u m b e r }   [ p r e c i s i o n = 4 ] :   n o   o f   s i g n i f i c a n t   d i g i t s   t o   u s e   f o r   r e t u r n e d   v a l u e 
   *   @ r e t u r n s   { N u m b e r }   D i s t a n c e   i n   k m   b e t w e e n   t h i s   p o i n t   a n d   d e s t i n a t i o n   p o i n t 
   * / 
 L a t L o n . p r o t o t y p e . d i s t a n c e T o   =   f u n c t i o n ( p o i n t ,   p r e c i s i o n )   { 
     / /   d e f a u l t   4   s i g   f i g s   r e f l e c t s   t y p i c a l   0 . 3 %   a c c u r a c y   o f   s p h e r i c a l   m o d e l 
     i f   ( t y p e o f   p r e c i s i o n   = =   ' u n d e f i n e d ' )   p r e c i s i o n   =   4 ;     
     
     v a r   R   =   t h i s . _ r a d i u s ; 
     v a r   l a t 1   =   t h i s . _ l a t . t o R a d ( ) ,   l o n 1   =   t h i s . _ l o n . t o R a d ( ) ; 
     v a r   l a t 2   =   p o i n t . _ l a t . t o R a d ( ) ,   l o n 2   =   p o i n t . _ l o n . t o R a d ( ) ; 
     v a r   d L a t   =   l a t 2   -   l a t 1 ; 
     v a r   d L o n   =   l o n 2   -   l o n 1 ; 
 
     v a r   a   =   M a t h . s i n ( d L a t / 2 )   *   M a t h . s i n ( d L a t / 2 )   + 
                     M a t h . c o s ( l a t 1 )   *   M a t h . c o s ( l a t 2 )   *   
                     M a t h . s i n ( d L o n / 2 )   *   M a t h . s i n ( d L o n / 2 ) ; 
     v a r   c   =   2   *   M a t h . a t a n 2 ( M a t h . s q r t ( a ) ,   M a t h . s q r t ( 1 - a ) ) ; 
     v a r   d   =   R   *   c ; 
     r e t u r n   d . t o P r e c i s i o n F i x e d ( p r e c i s i o n ) ; 
 } 
 
 
 / * * 
   *   R e t u r n s   t h e   ( i n i t i a l )   b e a r i n g   f r o m   t h i s   p o i n t   t o   t h e   s u p p l i e d   p o i n t ,   i n   d e g r e e s 
   *       s e e   h t t p : / / w i l l i a m s . b e s t . v w h . n e t / a v f o r m . h t m # C r s 
   * 
   *   @ p a r a m       { L a t L o n }   p o i n t :   L a t i t u d e / l o n g i t u d e   o f   d e s t i n a t i o n   p o i n t 
   *   @ r e t u r n s   { N u m b e r }   I n i t i a l   b e a r i n g   i n   d e g r e e s   f r o m   N o r t h 
   * / 
 L a t L o n . p r o t o t y p e . b e a r i n g T o   =   f u n c t i o n ( p o i n t )   { 
     v a r   l a t 1   =   t h i s . _ l a t . t o R a d ( ) ,   l a t 2   =   p o i n t . _ l a t . t o R a d ( ) ; 
     v a r   d L o n   =   ( p o i n t . _ l o n - t h i s . _ l o n ) . t o R a d ( ) ; 
 
     v a r   y   =   M a t h . s i n ( d L o n )   *   M a t h . c o s ( l a t 2 ) ; 
     v a r   x   =   M a t h . c o s ( l a t 1 ) * M a t h . s i n ( l a t 2 )   - 
                     M a t h . s i n ( l a t 1 ) * M a t h . c o s ( l a t 2 ) * M a t h . c o s ( d L o n ) ; 
     v a r   b r n g   =   M a t h . a t a n 2 ( y ,   x ) ; 
     
     r e t u r n   ( b r n g . t o D e g ( ) + 3 6 0 )   %   3 6 0 ; 
 } 
 
 
 / * * 
   *   R e t u r n s   f i n a l   b e a r i n g   a r r i v i n g   a t   s u p p l i e d   d e s t i n a t i o n   p o i n t   f r o m   t h i s   p o i n t ;   t h e   f i n a l   b e a r i n g   
   *   w i l l   d i f f e r   f r o m   t h e   i n i t i a l   b e a r i n g   b y   v a r y i n g   d e g r e e s   a c c o r d i n g   t o   d i s t a n c e   a n d   l a t i t u d e 
   * 
   *   @ p a r a m       { L a t L o n }   p o i n t :   L a t i t u d e / l o n g i t u d e   o f   d e s t i n a t i o n   p o i n t 
   *   @ r e t u r n s   { N u m b e r }   F i n a l   b e a r i n g   i n   d e g r e e s   f r o m   N o r t h 
   * / 
 L a t L o n . p r o t o t y p e . f i n a l B e a r i n g T o   =   f u n c t i o n ( p o i n t )   { 
     / /   g e t   i n i t i a l   b e a r i n g   f r o m   s u p p l i e d   p o i n t   b a c k   t o   t h i s   p o i n t . . . 
     v a r   l a t 1   =   p o i n t . _ l a t . t o R a d ( ) ,   l a t 2   =   t h i s . _ l a t . t o R a d ( ) ; 
     v a r   d L o n   =   ( t h i s . _ l o n - p o i n t . _ l o n ) . t o R a d ( ) ; 
 
     v a r   y   =   M a t h . s i n ( d L o n )   *   M a t h . c o s ( l a t 2 ) ; 
     v a r   x   =   M a t h . c o s ( l a t 1 ) * M a t h . s i n ( l a t 2 )   - 
                     M a t h . s i n ( l a t 1 ) * M a t h . c o s ( l a t 2 ) * M a t h . c o s ( d L o n ) ; 
     v a r   b r n g   =   M a t h . a t a n 2 ( y ,   x ) ; 
                     
     / /   . . .   &   r e v e r s e   i t   b y   a d d i n g   1 8 0 ° 
     r e t u r n   ( b r n g . t o D e g ( ) + 1 8 0 )   %   3 6 0 ; 
 } 
 
 
 / * * 
   *   R e t u r n s   t h e   m i d p o i n t   b e t w e e n   t h i s   p o i n t   a n d   t h e   s u p p l i e d   p o i n t . 
   *       s e e   h t t p : / / m a t h f o r u m . o r g / l i b r a r y / d r m a t h / v i e w / 5 1 8 2 2 . h t m l   f o r   d e r i v a t i o n 
   * 
   *   @ p a r a m       { L a t L o n }   p o i n t :   L a t i t u d e / l o n g i t u d e   o f   d e s t i n a t i o n   p o i n t 
   *   @ r e t u r n s   { L a t L o n }   M i d p o i n t   b e t w e e n   t h i s   p o i n t   a n d   t h e   s u p p l i e d   p o i n t 
   * / 
 L a t L o n . p r o t o t y p e . m i d p o i n t T o   =   f u n c t i o n ( p o i n t )   { 
     l a t 1   =   t h i s . _ l a t . t o R a d ( ) ,   l o n 1   =   t h i s . _ l o n . t o R a d ( ) ; 
     l a t 2   =   p o i n t . _ l a t . t o R a d ( ) ; 
     v a r   d L o n   =   ( p o i n t . _ l o n - t h i s . _ l o n ) . t o R a d ( ) ; 
 
     v a r   B x   =   M a t h . c o s ( l a t 2 )   *   M a t h . c o s ( d L o n ) ; 
     v a r   B y   =   M a t h . c o s ( l a t 2 )   *   M a t h . s i n ( d L o n ) ; 
 
     l a t 3   =   M a t h . a t a n 2 ( M a t h . s i n ( l a t 1 ) + M a t h . s i n ( l a t 2 ) , 
                                         M a t h . s q r t (   ( M a t h . c o s ( l a t 1 ) + B x ) * ( M a t h . c o s ( l a t 1 ) + B x )   +   B y * B y )   ) ; 
     l o n 3   =   l o n 1   +   M a t h . a t a n 2 ( B y ,   M a t h . c o s ( l a t 1 )   +   B x ) ; 
 
     r e t u r n   n e w   L a t L o n ( l a t 3 . t o D e g ( ) ,   l o n 3 . t o D e g ( ) ) ; 
 } 
 
 
 / * * 
   *   R e t u r n s   t h e   d e s t i n a t i o n   p o i n t   f r o m   t h i s   p o i n t   h a v i n g   t r a v e l l e d   t h e   g i v e n   d i s t a n c e   ( i n   k m )   o n   t h e   
   *   g i v e n   i n i t i a l   b e a r i n g   ( b e a r i n g   m a y   v a r y   b e f o r e   d e s t i n a t i o n   i s   r e a c h e d ) 
   * 
   *       s e e   h t t p : / / w i l l i a m s . b e s t . v w h . n e t / a v f o r m . h t m # L L 
   * 
   *   @ p a r a m       { N u m b e r }   b r n g :   I n i t i a l   b e a r i n g   i n   d e g r e e s 
   *   @ p a r a m       { N u m b e r }   d i s t :   D i s t a n c e   i n   k m 
   *   @ r e t u r n s   { L a t L o n }   D e s t i n a t i o n   p o i n t 
   * / 
 L a t L o n . p r o t o t y p e . d e s t i n a t i o n P o i n t   =   f u n c t i o n ( b r n g ,   d i s t )   { 
     d i s t   =   t y p e o f ( d i s t ) = = ' n u m b e r '   ?   d i s t   :   t y p e o f ( d i s t ) = = ' s t r i n g '   & &   d i s t . t r i m ( ) ! = ' '   ?   + d i s t   :   N a N ; 
     d i s t   =   d i s t / t h i s . _ r a d i u s ;     / /   c o n v e r t   d i s t   t o   a n g u l a r   d i s t a n c e   i n   r a d i a n s 
     b r n g   =   b r n g . t o R a d ( ) ;     / /   
     v a r   l a t 1   =   t h i s . _ l a t . t o R a d ( ) ,   l o n 1   =   t h i s . _ l o n . t o R a d ( ) ; 
 
     v a r   l a t 2   =   M a t h . a s i n (   M a t h . s i n ( l a t 1 ) * M a t h . c o s ( d i s t )   +   
                                                 M a t h . c o s ( l a t 1 ) * M a t h . s i n ( d i s t ) * M a t h . c o s ( b r n g )   ) ; 
     v a r   l o n 2   =   l o n 1   +   M a t h . a t a n 2 ( M a t h . s i n ( b r n g ) * M a t h . s i n ( d i s t ) * M a t h . c o s ( l a t 1 ) ,   
                                                               M a t h . c o s ( d i s t ) - M a t h . s i n ( l a t 1 ) * M a t h . s i n ( l a t 2 ) ) ; 
     l o n 2   =   ( l o n 2 + 3 * M a t h . P I ) % ( 2 * M a t h . P I )   -   M a t h . P I ;     / /   n o r m a l i s e   t o   - 1 8 0 . . . + 1 8 0 
 
     r e t u r n   n e w   L a t L o n ( l a t 2 . t o D e g ( ) ,   l o n 2 . t o D e g ( ) ) ; 
 } 
 
 
 / * * 
   *   R e t u r n s   t h e   p o i n t   o f   i n t e r s e c t i o n   o f   t w o   p a t h s   d e f i n e d   b y   p o i n t   a n d   b e a r i n g 
   * 
   *       s e e   h t t p : / / w i l l i a m s . b e s t . v w h . n e t / a v f o r m . h t m # I n t e r s e c t i o n 
   * 
   *   @ p a r a m       { L a t L o n }   p 1 :   F i r s t   p o i n t 
   *   @ p a r a m       { N u m b e r }   b r n g 1 :   I n i t i a l   b e a r i n g   f r o m   f i r s t   p o i n t 
   *   @ p a r a m       { L a t L o n }   p 2 :   S e c o n d   p o i n t 
   *   @ p a r a m       { N u m b e r }   b r n g 2 :   I n i t i a l   b e a r i n g   f r o m   s e c o n d   p o i n t 
   *   @ r e t u r n s   { L a t L o n }   D e s t i n a t i o n   p o i n t   ( n u l l   i f   n o   u n i q u e   i n t e r s e c t i o n   d e f i n e d ) 
   * / 
 L a t L o n . i n t e r s e c t i o n   =   f u n c t i o n ( p 1 ,   b r n g 1 ,   p 2 ,   b r n g 2 )   { 
     b r n g 1   =   t y p e o f   b r n g 1   = =   ' n u m b e r '   ?   b r n g 1   :   t y p e o f   b r n g 1   = =   ' s t r i n g '   & &   t r i m ( b r n g 1 ) ! = ' '   ?   + b r n g 1   :   N a N ; 
     b r n g 2   =   t y p e o f   b r n g 2   = =   ' n u m b e r '   ?   b r n g 2   :   t y p e o f   b r n g 2   = =   ' s t r i n g '   & &   t r i m ( b r n g 2 ) ! = ' '   ?   + b r n g 2   :   N a N ; 
     l a t 1   =   p 1 . _ l a t . t o R a d ( ) ,   l o n 1   =   p 1 . _ l o n . t o R a d ( ) ; 
     l a t 2   =   p 2 . _ l a t . t o R a d ( ) ,   l o n 2   =   p 2 . _ l o n . t o R a d ( ) ; 
     b r n g 1 3   =   b r n g 1 . t o R a d ( ) ,   b r n g 2 3   =   b r n g 2 . t o R a d ( ) ; 
     d L a t   =   l a t 2 - l a t 1 ,   d L o n   =   l o n 2 - l o n 1 ; 
     
     d i s t 1 2   =   2 * M a t h . a s i n (   M a t h . s q r t (   M a t h . s i n ( d L a t / 2 ) * M a t h . s i n ( d L a t / 2 )   +   
         M a t h . c o s ( l a t 1 ) * M a t h . c o s ( l a t 2 ) * M a t h . s i n ( d L o n / 2 ) * M a t h . s i n ( d L o n / 2 )   )   ) ; 
     i f   ( d i s t 1 2   = =   0 )   r e t u r n   n u l l ; 
     
     / /   i n i t i a l / f i n a l   b e a r i n g s   b e t w e e n   p o i n t s 
     b r n g A   =   M a t h . a c o s (   (   M a t h . s i n ( l a t 2 )   -   M a t h . s i n ( l a t 1 ) * M a t h . c o s ( d i s t 1 2 )   )   /   
         (   M a t h . s i n ( d i s t 1 2 ) * M a t h . c o s ( l a t 1 )   )   ) ; 
     i f   ( i s N a N ( b r n g A ) )   b r n g A   =   0 ;     / /   p r o t e c t   a g a i n s t   r o u n d i n g 
     b r n g B   =   M a t h . a c o s (   (   M a t h . s i n ( l a t 1 )   -   M a t h . s i n ( l a t 2 ) * M a t h . c o s ( d i s t 1 2 )   )   /   
         (   M a t h . s i n ( d i s t 1 2 ) * M a t h . c o s ( l a t 2 )   )   ) ; 
     
     i f   ( M a t h . s i n ( l o n 2 - l o n 1 )   >   0 )   { 
         b r n g 1 2   =   b r n g A ; 
         b r n g 2 1   =   2 * M a t h . P I   -   b r n g B ; 
     }   e l s e   { 
         b r n g 1 2   =   2 * M a t h . P I   -   b r n g A ; 
         b r n g 2 1   =   b r n g B ; 
     } 
     
     a l p h a 1   =   ( b r n g 1 3   -   b r n g 1 2   +   M a t h . P I )   %   ( 2 * M a t h . P I )   -   M a t h . P I ;     / /   a n g l e   2 - 1 - 3 
     a l p h a 2   =   ( b r n g 2 1   -   b r n g 2 3   +   M a t h . P I )   %   ( 2 * M a t h . P I )   -   M a t h . P I ;     / /   a n g l e   1 - 2 - 3 
     
     i f   ( M a t h . s i n ( a l p h a 1 ) = = 0   & &   M a t h . s i n ( a l p h a 2 ) = = 0 )   r e t u r n   n u l l ;     / /   i n f i n i t e   i n t e r s e c t i o n s 
     i f   ( M a t h . s i n ( a l p h a 1 ) * M a t h . s i n ( a l p h a 2 )   <   0 )   r e t u r n   n u l l ;               / /   a m b i g u o u s   i n t e r s e c t i o n 
     
     / / a l p h a 1   =   M a t h . a b s ( a l p h a 1 ) ; 
     / / a l p h a 2   =   M a t h . a b s ( a l p h a 2 ) ; 
     / /   . . .   E d   W i l l i a m s   t a k e s   a b s   o f   a l p h a 1 / a l p h a 2 ,   b u t   s e e m s   t o   b r e a k   c a l c u l a t i o n ? 
     
     a l p h a 3   =   M a t h . a c o s (   - M a t h . c o s ( a l p h a 1 ) * M a t h . c o s ( a l p h a 2 )   +   
                                               M a t h . s i n ( a l p h a 1 ) * M a t h . s i n ( a l p h a 2 ) * M a t h . c o s ( d i s t 1 2 )   ) ; 
     d i s t 1 3   =   M a t h . a t a n 2 (   M a t h . s i n ( d i s t 1 2 ) * M a t h . s i n ( a l p h a 1 ) * M a t h . s i n ( a l p h a 2 ) ,   
                                               M a t h . c o s ( a l p h a 2 ) + M a t h . c o s ( a l p h a 1 ) * M a t h . c o s ( a l p h a 3 )   ) 
     l a t 3   =   M a t h . a s i n (   M a t h . s i n ( l a t 1 ) * M a t h . c o s ( d i s t 1 3 )   +   
                                         M a t h . c o s ( l a t 1 ) * M a t h . s i n ( d i s t 1 3 ) * M a t h . c o s ( b r n g 1 3 )   ) ; 
     d L o n 1 3   =   M a t h . a t a n 2 (   M a t h . s i n ( b r n g 1 3 ) * M a t h . s i n ( d i s t 1 3 ) * M a t h . c o s ( l a t 1 ) ,   
                                               M a t h . c o s ( d i s t 1 3 ) - M a t h . s i n ( l a t 1 ) * M a t h . s i n ( l a t 3 )   ) ; 
     l o n 3   =   l o n 1 + d L o n 1 3 ; 
     l o n 3   =   ( l o n 3 + M a t h . P I )   %   ( 2 * M a t h . P I )   -   M a t h . P I ;     / /   n o r m a l i s e   t o   - 1 8 0 . . 1 8 0 º 
     
     r e t u r n   n e w   L a t L o n ( l a t 3 . t o D e g ( ) ,   l o n 3 . t o D e g ( ) ) ; 
 } 
 
 
 / *   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -     * / 
 
 / * * 
   *   R e t u r n s   t h e   d i s t a n c e   f r o m   t h i s   p o i n t   t o   t h e   s u p p l i e d   p o i n t ,   i n   k m ,   t r a v e l l i n g   a l o n g   a   r h u m b   l i n e 
   * 
   *       s e e   h t t p : / / w i l l i a m s . b e s t . v w h . n e t / a v f o r m . h t m # R h u m b 
   * 
   *   @ p a r a m       { L a t L o n }   p o i n t :   L a t i t u d e / l o n g i t u d e   o f   d e s t i n a t i o n   p o i n t 
   *   @ r e t u r n s   { N u m b e r }   D i s t a n c e   i n   k m   b e t w e e n   t h i s   p o i n t   a n d   d e s t i n a t i o n   p o i n t 
   * / 
 L a t L o n . p r o t o t y p e . r h u m b D i s t a n c e T o   =   f u n c t i o n ( p o i n t )   { 
     v a r   R   =   t h i s . _ r a d i u s ; 
     v a r   l a t 1   =   t h i s . _ l a t . t o R a d ( ) ,   l a t 2   =   p o i n t . _ l a t . t o R a d ( ) ; 
     v a r   d L a t   =   ( p o i n t . _ l a t - t h i s . _ l a t ) . t o R a d ( ) ; 
     v a r   d L o n   =   M a t h . a b s ( p o i n t . _ l o n - t h i s . _ l o n ) . t o R a d ( ) ; 
     
     v a r   d P h i   =   M a t h . l o g ( M a t h . t a n ( l a t 2 / 2 + M a t h . P I / 4 ) / M a t h . t a n ( l a t 1 / 2 + M a t h . P I / 4 ) ) ; 
     v a r   q   =   ( ! i s N a N ( d L a t / d P h i ) )   ?   d L a t / d P h i   :   M a t h . c o s ( l a t 1 ) ;     / /   E - W   l i n e   g i v e s   d P h i = 0 
     / /   i f   d L o n   o v e r   1 8 0 °   t a k e   s h o r t e r   r h u m b   a c r o s s   1 8 0 °   m e r i d i a n : 
     i f   ( d L o n   >   M a t h . P I )   d L o n   =   2 * M a t h . P I   -   d L o n ; 
     v a r   d i s t   =   M a t h . s q r t ( d L a t * d L a t   +   q * q * d L o n * d L o n )   *   R ;   
     
     r e t u r n   d i s t . t o P r e c i s i o n F i x e d ( 4 ) ;     / /   4   s i g   f i g s   r e f l e c t s   t y p i c a l   0 . 3 %   a c c u r a c y   o f   s p h e r i c a l   m o d e l 
 } 
 
 / * * 
   *   R e t u r n s   t h e   b e a r i n g   f r o m   t h i s   p o i n t   t o   t h e   s u p p l i e d   p o i n t   a l o n g   a   r h u m b   l i n e ,   i n   d e g r e e s 
   * 
   *   @ p a r a m       { L a t L o n }   p o i n t :   L a t i t u d e / l o n g i t u d e   o f   d e s t i n a t i o n   p o i n t 
   *   @ r e t u r n s   { N u m b e r }   B e a r i n g   i n   d e g r e e s   f r o m   N o r t h 
   * / 
 L a t L o n . p r o t o t y p e . r h u m b B e a r i n g T o   =   f u n c t i o n ( p o i n t )   { 
     v a r   l a t 1   =   t h i s . _ l a t . t o R a d ( ) ,   l a t 2   =   p o i n t . _ l a t . t o R a d ( ) ; 
     v a r   d L o n   =   ( p o i n t . _ l o n - t h i s . _ l o n ) . t o R a d ( ) ; 
     
     v a r   d P h i   =   M a t h . l o g ( M a t h . t a n ( l a t 2 / 2 + M a t h . P I / 4 ) / M a t h . t a n ( l a t 1 / 2 + M a t h . P I / 4 ) ) ; 
     i f   ( M a t h . a b s ( d L o n )   >   M a t h . P I )   d L o n   =   d L o n > 0   ?   - ( 2 * M a t h . P I - d L o n )   :   ( 2 * M a t h . P I + d L o n ) ; 
     v a r   b r n g   =   M a t h . a t a n 2 ( d L o n ,   d P h i ) ; 
     
     r e t u r n   ( b r n g . t o D e g ( ) + 3 6 0 )   %   3 6 0 ; 
 } 
 
 / * * 
   *   R e t u r n s   t h e   d e s t i n a t i o n   p o i n t   f r o m   t h i s   p o i n t   h a v i n g   t r a v e l l e d   t h e   g i v e n   d i s t a n c e   ( i n   k m )   o n   t h e   
   *   g i v e n   b e a r i n g   a l o n g   a   r h u m b   l i n e 
   * 
   *   @ p a r a m       { N u m b e r }   b r n g :   B e a r i n g   i n   d e g r e e s   f r o m   N o r t h 
   *   @ p a r a m       { N u m b e r }   d i s t :   D i s t a n c e   i n   k m 
   *   @ r e t u r n s   { L a t L o n }   D e s t i n a t i o n   p o i n t 
   * / 
 L a t L o n . p r o t o t y p e . r h u m b D e s t i n a t i o n P o i n t   =   f u n c t i o n ( b r n g ,   d i s t )   { 
     v a r   R   =   t h i s . _ r a d i u s ; 
     v a r   d   =   p a r s e F l o a t ( d i s t ) / R ;     / /   d   =   a n g u l a r   d i s t a n c e   c o v e r e d   o n   e a r t h ' s   s u r f a c e 
     v a r   l a t 1   =   t h i s . _ l a t . t o R a d ( ) ,   l o n 1   =   t h i s . _ l o n . t o R a d ( ) ; 
     b r n g   =   b r n g . t o R a d ( ) ; 
 
     v a r   l a t 2   =   l a t 1   +   d * M a t h . c o s ( b r n g ) ; 
     v a r   d L a t   =   l a t 2 - l a t 1 ; 
     v a r   d P h i   =   M a t h . l o g ( M a t h . t a n ( l a t 2 / 2 + M a t h . P I / 4 ) / M a t h . t a n ( l a t 1 / 2 + M a t h . P I / 4 ) ) ; 
     v a r   q   =   ( ! i s N a N ( d L a t / d P h i ) )   ?   d L a t / d P h i   :   M a t h . c o s ( l a t 1 ) ;     / /   E - W   l i n e   g i v e s   d P h i = 0 
     v a r   d L o n   =   d * M a t h . s i n ( b r n g ) / q ; 
     / /   c h e c k   f o r   s o m e   d a f t   b u g g e r   g o i n g   p a s t   t h e   p o l e 
     i f   ( M a t h . a b s ( l a t 2 )   >   M a t h . P I / 2 )   l a t 2   =   l a t 2 > 0   ?   M a t h . P I - l a t 2   :   - ( M a t h . P I - l a t 2 ) ; 
     l o n 2   =   ( l o n 1 + d L o n + 3 * M a t h . P I ) % ( 2 * M a t h . P I )   -   M a t h . P I ; 
   
     r e t u r n   n e w   L a t L o n ( l a t 2 . t o D e g ( ) ,   l o n 2 . t o D e g ( ) ) ; 
 } 
 
 / *   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -     * / 
 
 
 / * * 
   *   R e t u r n s   t h e   l a t i t u d e   o f   t h i s   p o i n t ;   s i g n e d   n u m e r i c   d e g r e e s   i f   n o   f o r m a t ,   o t h e r w i s e   f o r m a t   &   d p   
   *   a s   p e r   G e o . t o L a t ( ) 
   * 
   *   @ p a r a m       { S t r i n g }   [ f o r m a t ] :   R e t u r n   v a l u e   a s   ' d ' ,   ' d m ' ,   ' d m s ' 
   *   @ p a r a m       { N u m b e r }   [ d p = 0 | 2 | 4 ] :   N o   o f   d e c i m a l   p l a c e s   t o   d i s p l a y 
   *   @ r e t u r n s   { N u m b e r | S t r i n g }   N u m e r i c   d e g r e e s   i f   n o   f o r m a t   s p e c i f i e d ,   o t h e r w i s e   d e g / m i n / s e c 
   * 
   *   @ r e q u i r e s   G e o 
   * / 
 L a t L o n . p r o t o t y p e . l a t   =   f u n c t i o n ( f o r m a t ,   d p )   { 
     i f   ( t y p e o f   f o r m a t   = =   ' u n d e f i n e d ' )   r e t u r n   t h i s . _ l a t ; 
     
     r e t u r n   G e o . t o L a t ( t h i s . _ l a t ,   f o r m a t ,   d p ) ; 
 } 
 
 / * * 
   *   R e t u r n s   t h e   l o n g i t u d e   o f   t h i s   p o i n t ;   s i g n e d   n u m e r i c   d e g r e e s   i f   n o   f o r m a t ,   o t h e r w i s e   f o r m a t   &   d p   
   *   a s   p e r   G e o . t o L o n ( ) 
   * 
   *   @ p a r a m       { S t r i n g }   [ f o r m a t ] :   R e t u r n   v a l u e   a s   ' d ' ,   ' d m ' ,   ' d m s ' 
   *   @ p a r a m       { N u m b e r }   [ d p = 0 | 2 | 4 ] :   N o   o f   d e c i m a l   p l a c e s   t o   d i s p l a y 
   *   @ r e t u r n s   { N u m b e r | S t r i n g }   N u m e r i c   d e g r e e s   i f   n o   f o r m a t   s p e c i f i e d ,   o t h e r w i s e   d e g / m i n / s e c 
   * 
   *   @ r e q u i r e s   G e o 
   * / 
 L a t L o n . p r o t o t y p e . l o n   =   f u n c t i o n ( f o r m a t ,   d p )   { 
     i f   ( t y p e o f   f o r m a t   = =   ' u n d e f i n e d ' )   r e t u r n   t h i s . _ l o n ; 
     
     r e t u r n   G e o . t o L o n ( t h i s . _ l o n ,   f o r m a t ,   d p ) ; 
 } 
 
 / * * 
   *   R e t u r n s   a   s t r i n g   r e p r e s e n t a t i o n   o f   t h i s   p o i n t ;   f o r m a t   a n d   d p   a s   p e r   l a t ( ) / l o n ( ) 
   * 
   *   @ p a r a m       { S t r i n g }   [ f o r m a t ] :   R e t u r n   v a l u e   a s   ' d ' ,   ' d m ' ,   ' d m s ' 
   *   @ p a r a m       { N u m b e r }   [ d p = 0 | 2 | 4 ] :   N o   o f   d e c i m a l   p l a c e s   t o   d i s p l a y 
   *   @ r e t u r n s   { S t r i n g }   C o m m a - s e p a r a t e d   l a t i t u d e / l o n g i t u d e 
   * 
   *   @ r e q u i r e s   G e o 
   * / 
 L a t L o n . p r o t o t y p e . t o S t r i n g   =   f u n c t i o n ( f o r m a t ,   d p )   { 
     i f   ( t y p e o f   f o r m a t   = =   ' u n d e f i n e d ' )   f o r m a t   =   ' d m s ' ; 
     
     i f   ( i s N a N ( t h i s . _ l a t )   | |   i s N a N ( t h i s . _ l o n ) )   r e t u r n   ' - , - ' ; 
     
     r e t u r n   G e o . t o L a t ( t h i s . _ l a t ,   f o r m a t ,   d p )   +   ' ,   '   +   G e o . t o L o n ( t h i s . _ l o n ,   f o r m a t ,   d p ) ; 
 } 
 
 / *   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -     * / 
 
 / /   - - - -   e x t e n d   N u m b e r   o b j e c t   w i t h   m e t h o d s   f o r   c o n v e r t i n g   d e g r e e s / r a d i a n s 
 
 / * *   C o n v e r t s   n u m e r i c   d e g r e e s   t o   r a d i a n s   * / 
 i f   ( t y p e o f ( N u m b e r . p r o t o t y p e . t o R a d )   = = =   " u n d e f i n e d " )   { 
     N u m b e r . p r o t o t y p e . t o R a d   =   f u n c t i o n ( )   { 
         r e t u r n   t h i s   *   M a t h . P I   /   1 8 0 ; 
     } 
 } 
 
 / * *   C o n v e r t s   r a d i a n s   t o   n u m e r i c   ( s i g n e d )   d e g r e e s   * / 
 i f   ( t y p e o f ( N u m b e r . p r o t o t y p e . t o D e g )   = = =   " u n d e f i n e d " )   { 
     N u m b e r . p r o t o t y p e . t o D e g   =   f u n c t i o n ( )   { 
         r e t u r n   t h i s   *   1 8 0   /   M a t h . P I ; 
     } 
 } 
 
 / * *   
   *   F o r m a t s   t h e   s i g n i f i c a n t   d i g i t s   o f   a   n u m b e r ,   u s i n g   o n l y   f i x e d - p o i n t   n o t a t i o n   ( n o   e x p o n e n t i a l ) 
   *   
   *   @ p a r a m       { N u m b e r }   p r e c i s i o n :   N u m b e r   o f   s i g n i f i c a n t   d i g i t s   t o   a p p e a r   i n   t h e   r e t u r n e d   s t r i n g 
   *   @ r e t u r n s   { S t r i n g }   A   s t r i n g   r e p r e s e n t a t i o n   o f   n u m b e r   w h i c h   c o n t a i n s   p r e c i s i o n   s i g n i f i c a n t   d i g i t s 
   * / 
 i f   ( t y p e o f ( N u m b e r . p r o t o t y p e . t o P r e c i s i o n F i x e d )   = = =   " u n d e f i n e d " )   { 
     N u m b e r . p r o t o t y p e . t o P r e c i s i o n F i x e d   =   f u n c t i o n ( p r e c i s i o n )   { 
         i f   ( i s N a N ( t h i s ) )   r e t u r n   ' N a N ' ; 
         v a r   n u m b   =   t h i s   <   0   ?   - t h i s   :   t h i s ;     / /   c a n ' t   t a k e   l o g   o f   - v e   n u m b e r . . . 
         v a r   s i g n   =   t h i s   <   0   ?   ' - '   :   ' ' ; 
         
         i f   ( n u m b   = =   0 )   {   n   =   ' 0 . ' ;   w h i l e   ( p r e c i s i o n - - )   n   + =   ' 0 ' ;   r e t u r n   n   } ;     / /   c a n ' t   t a k e   l o g   o f   z e r o 
     
         v a r   s c a l e   =   M a t h . c e i l ( M a t h . l o g ( n u m b ) * M a t h . L O G 1 0 E ) ;     / /   n o   o f   d i g i t s   b e f o r e   d e c i m a l 
         v a r   n   =   S t r i n g ( M a t h . r o u n d ( n u m b   *   M a t h . p o w ( 1 0 ,   p r e c i s i o n - s c a l e ) ) ) ; 
         i f   ( s c a l e   >   0 )   {     / /   a d d   t r a i l i n g   z e r o s   &   i n s e r t   d e c i m a l   a s   r e q u i r e d 
             l   =   s c a l e   -   n . l e n g t h ; 
             w h i l e   ( l - -   >   0 )   n   =   n   +   ' 0 ' ; 
             i f   ( s c a l e   <   n . l e n g t h )   n   =   n . s l i c e ( 0 , s c a l e )   +   ' . '   +   n . s l i c e ( s c a l e ) ; 
         }   e l s e   {                     / /   p r e f i x   d e c i m a l   a n d   l e a d i n g   z e r o s   i f   r e q u i r e d 
             w h i l e   ( s c a l e + +   <   0 )   n   =   ' 0 '   +   n ; 
             n   =   ' 0 . '   +   n ; 
         } 
         r e t u r n   s i g n   +   n ; 
     } 
 } 
 
 / * *   T r i m s   w h i t e s p a c e   f r o m   s t r i n g   ( q . v .   b l o g . s t e v e n l e v i t h a n . c o m / a r c h i v e s / f a s t e r - t r i m - j a v a s c r i p t )   * / 
 i f   ( t y p e o f ( S t r i n g . p r o t o t y p e . t r i m )   = = =   " u n d e f i n e d " )   { 
     S t r i n g . p r o t o t y p e . t r i m   =   f u n c t i o n ( )   { 
         r e t u r n   S t r i n g ( t h i s ) . r e p l a c e ( / ^ \ s \ s * / ,   ' ' ) . r e p l a c e ( / \ s \ s * $ / ,   ' ' ) ; 
     } 
 } 
 
 
