/ *   
 
   *   P R O J E C T :   N y A R T o o l k i t 
 
   *   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
   *   T h i s   w o r k   i s   b a s e d   o n   t h e   o r i g i n a l   A R T o o l K i t   d e v e l o p e d   b y 
 
   *       H i r o k a z u   K a t o 
 
   *       M a r k   B i l l i n g h u r s t 
 
   *       H I T L a b ,   U n i v e r s i t y   o f   W a s h i n g t o n ,   S e a t t l e 
 
   *   h t t p : / / w w w . h i t l . w a s h i n g t o n . e d u / a r t o o l k i t / 
 
   * 
 
   *   T h e   N y A R T o o l k i t   i s   J a v a   e d i t i o n   A R T o o l K i t   c l a s s   l i b r a r y . 
 
   *   C o p y r i g h t   ( C ) 2 0 0 8 - 2 0 0 9   R y o   I i z u k a 
 
   * 
 
   *   T h i s   p r o g r a m   i s   f r e e   s o f t w a r e :   y o u   c a n   r e d i s t r i b u t e   i t   a n d / o r   m o d i f y 
 
   *   i t   u n d e r   t h e   t e r m s   o f   t h e   G N U   G e n e r a l   P u b l i c   L i c e n s e   a s   p u b l i s h e d   b y 
 
   *   t h e   F r e e   S o f t w a r e   F o u n d a t i o n ,   e i t h e r   v e r s i o n   3   o f   t h e   L i c e n s e ,   o r 
 
   *   ( a t   y o u r   o p t i o n )   a n y   l a t e r   v e r s i o n . 
 
   *   
 
   *   T h i s   p r o g r a m   i s   d i s t r i b u t e d   i n   t h e   h o p e   t h a t   i t   w i l l   b e   u s e f u l , 
 
   *   b u t   W I T H O U T   A N Y   W A R R A N T Y ;   w i t h o u t   e v e n   t h e   i m p l i e d   w a r r a n t y   o f 
 
   *   M E R C H A N T A B I L I T Y   o r   F I T N E S S   F O R   A   P A R T I C U L A R   P U R P O S E .     S e e   t h e 
 
   *   G N U   G e n e r a l   P u b l i c   L i c e n s e   f o r   m o r e   d e t a i l s . 
 
   * 
 
   *   Y o u   s h o u l d   h a v e   r e c e i v e d   a   c o p y   o f   t h e   G N U   G e n e r a l   P u b l i c   L i c e n s e 
 
   *   a l o n g   w i t h   t h i s   p r o g r a m .     I f   n o t ,   s e e   < h t t p : / / w w w . g n u . o r g / l i c e n s e s / > . 
 
   *   
 
   *   F o r   f u r t h e r   i n f o r m a t i o n   p l e a s e   c o n t a c t . 
 
   * 	 h t t p : / / n y a t l a . j p / n y a t o o l k i t / 
 
   * 	 < a i r m a i l ( a t ) e b o n y . p l a l a . o r . j p >   o r   < n y a t l a ( a t ) n y a t l a . j p > 
 
   *   
 
   * / 
 
 n a m e s p a c e   j p . n y a t l a . n y a r t o o l k i t . c s . c o r e 
 
 { 
 
         / * * 
 
           *   S0n0000o00*}0V҉b_h0N[W0f00]0n0pMOn0{W0~0Y00
 
           *   A R T o o l K i t n0V҉b_iQQtn0 Ng0Y00
 
           * / 
 
         p u b l i c   c l a s s   N y A R C o o r d 2 S q u a r e V e r t e x I n d e x e s 
 
         { 
 
                 p r i v a t e   c o n s t   d o u b l e   V E R T E X _ F A C T O R   =   1 . 0 ; / /   }iQn00000	 
 
                 p r i v a t e   r e a d o n l y   N y A R V e r t e x C o u n t e r   _ _ g e t S q u a r e V e r t e x _ w v 1   =   n e w   N y A R V e r t e x C o u n t e r ( ) ; 
 
                 p r i v a t e   r e a d o n l y   N y A R V e r t e x C o u n t e r   _ _ g e t S q u a r e V e r t e x _ w v 2   =   n e w   N y A R V e r t e x C o u n t e r ( ) ; 
 
                 / * * 
 
                   *   0000000g0Y00
 
                   *   0000000ubW0~0Y00
 
                   * / 
 
                 p u b l i c   N y A R C o o r d 2 S q u a r e V e r t e x I n d e x e s ( ) 
 
                 { 
 
                         r e t u r n ; 
 
                 } 
 
                 / * * 
 
                   *   S0n0peo00^jƖTK00pP܈k0j00]0F0j04X@b0{@bcW0f00]0n000000juS0ԏW0~0Y00
 
                   *   @ p a r a m   i _ c o o r d 
 
                   *   *퐹pƖT0<h
}W0_00000000
 
                   *   @ p a r a m   i _ a r e a 
 
                   *   wb_$R[n0000$P0wb_n0'YM0U000]0n00000ibY000000n0peg0c[W0~0Y00
 
                   *   ( l) S0n000000o00000000000gn0'YM0U0L0q_I{0000n0'YM0U0k0q_0SQ0~0Y00
 
                   *   @ p a r a m   o _ v e r t e x 
 
                   *   4 pn0000000SQ0S0MRg0Y004  }N
Nn0MR0c[W0f0O0`0U0D00
 
                   *   @ r e t u r n 
 
                   *   pL0d0K00h0t r u e 0ԏW0~0Y00
 
                   * / 
 
                 p u b l i c   b o o l   g e t V e r t e x I n d e x e s ( N y A R I n t C o o r d i n a t e s   i _ c o o r d ,   i n t   i _ a r e a ,   i n t [ ]   o _ v e r t e x ) 
 
                 { 
 
                         N y A R V e r t e x C o u n t e r   w v 1   =   t h i s . _ _ g e t S q u a r e V e r t e x _ w v 1 ; 
 
                         N y A R V e r t e x C o u n t e r   w v 2   =   t h i s . _ _ g e t S q u a r e V e r t e x _ w v 2 ; 
 
                         i n t   i _ c o o r d _ n u m   =   i _ c o o r d . l e n g t h ; 
 
                         i n t   v e r t e x 1 _ i n d e x   =   g e t F a r P o i n t ( i _ c o o r d . i t e m s ,   i _ c o o r d _ n u m ,   0 ) ; 
 
                         i n t   p r e v _ v e r t e x _ i n d e x   =   ( v e r t e x 1 _ i n d e x   +   i _ c o o r d _ n u m )   %   i _ c o o r d _ n u m ; 
 
                         i n t   v 1   =   g e t F a r P o i n t ( i _ c o o r d . i t e m s ,   i _ c o o r d _ n u m ,   v e r t e x 1 _ i n d e x ) ; 
 
                         d o u b l e   t h r e s h   =   ( i _ a r e a   /   0 . 7 5 )   *   0 . 0 1   *   V E R T E X _ F A C T O R ; 
 
 
 
                         o _ v e r t e x [ 0 ]   =   v e r t e x 1 _ i n d e x ; 
 
 
 
                         i f   ( ! w v 1 . g e t V e r t e x ( i _ c o o r d . i t e m s ,   i _ c o o r d _ n u m ,   v e r t e x 1 _ i n d e x ,   v 1 ,   t h r e s h ) ) 
 
                         { 
 
                                 r e t u r n   f a l s e ; 
 
                         } 
 
                         i f   ( ! w v 2 . g e t V e r t e x ( i _ c o o r d . i t e m s ,   i _ c o o r d _ n u m ,   v 1 ,   p r e v _ v e r t e x _ i n d e x ,   t h r e s h ) ) 
 
                         { 
 
                                 r e t u r n   f a l s e ; 
 
                         } 
 
 
 
                         i n t   v 2 ; 
 
                         i f   ( w v 1 . n u m b e r _ o f _ v e r t e x   = =   1   & &   w v 2 . n u m b e r _ o f _ v e r t e x   = =   1 ) 
 
                         { 
 
                                 o _ v e r t e x [ 1 ]   =   w v 1 . v e r t e x [ 0 ] ; 
 
                                 o _ v e r t e x [ 2 ]   =   v 1 ; 
 
                                 o _ v e r t e x [ 3 ]   =   w v 2 . v e r t e x [ 0 ] ; 
 
                         } 
 
                         e l s e   i f   ( w v 1 . n u m b e r _ o f _ v e r t e x   >   1   & &   w v 2 . n u m b e r _ o f _ v e r t e x   = =   0 ) 
 
                         { 
 
                                 / / pMOn00wpK00[҉pn0n01 / 2 k0B00h0N`W0f00i"}Y000
 
                                 i f   ( v 1   > =   v e r t e x 1 _ i n d e x ) 
 
                                 { 
 
                                         v 2   =   ( v 1   -   v e r t e x 1 _ i n d e x )   /   2   +   v e r t e x 1 _ i n d e x ; 
 
                                 } 
 
                                 e l s e 
 
                                 { 
 
                                         v 2   =   ( ( v 1   +   i _ c o o r d _ n u m   -   v e r t e x 1 _ i n d e x )   /   2   +   v e r t e x 1 _ i n d e x )   %   i _ c o o r d _ n u m ; 
 
                                 } 
 
                                 i f   ( ! w v 1 . g e t V e r t e x ( i _ c o o r d . i t e m s ,   i _ c o o r d _ n u m ,   v e r t e x 1 _ i n d e x ,   v 2 ,   t h r e s h ) ) 
 
                                 { 
 
                                         r e t u r n   f a l s e ; 
 
                                 } 
 
                                 i f   ( ! w v 2 . g e t V e r t e x ( i _ c o o r d . i t e m s ,   i _ c o o r d _ n u m ,   v 2 ,   v 1 ,   t h r e s h ) ) 
 
                                 { 
 
                                         r e t u r n   f a l s e ; 
 
                                 } 
 
                                 i f   ( w v 1 . n u m b e r _ o f _ v e r t e x   = =   1   & &   w v 2 . n u m b e r _ o f _ v e r t e x   = =   1 ) 
 
                                 { 
 
                                         o _ v e r t e x [ 1 ]   =   w v 1 . v e r t e x [ 0 ] ; 
 
                                         o _ v e r t e x [ 2 ]   =   w v 2 . v e r t e x [ 0 ] ; 
 
                                         o _ v e r t e x [ 3 ]   =   v 1 ; 
 
                                 } 
 
                                 e l s e 
 
                                 { 
 
                                         r e t u r n   f a l s e ; 
 
                                 } 
 
                         } 
 
                         e l s e   i f   ( w v 1 . n u m b e r _ o f _ v e r t e x   = =   0   & &   w v 2 . n u m b e r _ o f _ v e r t e x   >   1 ) 
 
                         { 
 
                                 / / v 2   =   ( v 1 +   e n d _ o f _ c o o r d ) / 2 ; 
 
                                 i f   ( v 1   < =   p r e v _ v e r t e x _ i n d e x ) 
 
                                 { 
 
                                         v 2   =   ( v 1   +   p r e v _ v e r t e x _ i n d e x )   /   2 ; 
 
                                 } 
 
                                 e l s e 
 
                                 { 
 
                                         v 2   =   ( ( v 1   +   i _ c o o r d _ n u m   +   p r e v _ v e r t e x _ i n d e x )   /   2 )   %   i _ c o o r d _ n u m ; 
 
 
 
                                 } 
 
                                 i f   ( ! w v 1 . g e t V e r t e x ( i _ c o o r d . i t e m s ,   i _ c o o r d _ n u m ,   v 1 ,   v 2 ,   t h r e s h ) ) 
 
                                 { 
 
                                         r e t u r n   f a l s e ; 
 
                                 } 
 
                                 i f   ( ! w v 2 . g e t V e r t e x ( i _ c o o r d . i t e m s ,   i _ c o o r d _ n u m ,   v 2 ,   p r e v _ v e r t e x _ i n d e x ,   t h r e s h ) ) 
 
                                 { 
 
                                         r e t u r n   f a l s e ; 
 
                                 } 
 
                                 i f   ( w v 1 . n u m b e r _ o f _ v e r t e x   = =   1   & &   w v 2 . n u m b e r _ o f _ v e r t e x   = =   1 ) 
 
                                 { 
 
                                         o _ v e r t e x [ 1 ]   =   v 1 ; 
 
                                         o _ v e r t e x [ 2 ]   =   w v 1 . v e r t e x [ 0 ] ; 
 
                                         o _ v e r t e x [ 3 ]   =   w v 2 . v e r t e x [ 0 ] ; 
 
                                 } 
 
                                 e l s e 
 
                                 { 
 
 
 
                                         r e t u r n   f a l s e ; 
 
                                 } 
 
                         } 
 
                         e l s e 
 
                         { 
 
                                 r e t u r n   f a l s e ; 
 
                         } 
 
                         r e t u r n   t r u e ; 
 
                 } 
 
                 / * * 
 
                   *   i _ p o i n t n0*퐧^jK000 g0`ek0B00*퐧^jn0000000cW0~0Y00
 
                   *   @ p a r a m   i _ x c o o r d 
 
                   *   @ p a r a m   i _ y c o o r d 
 
                   *   @ p a r a m   i _ c o o r d _ n u m 
 
                   *   @ r e t u r n 
 
                   * / 
 
                 p r i v a t e   s t a t i c   i n t   g e t F a r P o i n t ( N y A R I n t P o i n t 2 d [ ]   i _ c o o r d ,   i n t   i _ c o o r d _ n u m ,   i n t   i _ p o i n t ) 
 
                 { 
 
                         / / 
 
                         i n t   s x   =   i _ c o o r d [ i _ p o i n t ] . x ; 
 
                         i n t   s y   =   i _ c o o r d [ i _ p o i n t ] . y ; 
 
                         i n t   d   =   0 ; 
 
                         i n t   w ,   x ,   y ; 
 
                         i n t   r e t   =   0 ; 
 
                         f o r   ( i n t   i   =   i _ p o i n t   +   1 ;   i   <   i _ c o o r d _ n u m ;   i + + ) 
 
                         { 
 
                                 x   =   i _ c o o r d [ i ] . x   -   s x ; 
 
                                 y   =   i _ c o o r d [ i ] . y   -   s y ; 
 
                                 w   =   x   *   x   +   y   *   y ; 
 
                                 i f   ( w   >   d ) 
 
                                 { 
 
                                         d   =   w ; 
 
                                         r e t   =   i ; 
 
                                 } 
 
                         } 
 
                         f o r   ( i n t   i   =   0 ;   i   <   i _ p o i n t ;   i + + ) 
 
                         { 
 
                                 x   =   i _ c o o r d [ i ] . x   -   s x ; 
 
                                 y   =   i _ c o o r d [ i ] . y   -   s y ; 
 
                                 w   =   x   *   x   +   y   *   y ; 
 
                                 i f   ( w   >   d ) 
 
                                 { 
 
                                         d   =   w ; 
 
                                         r e t   =   i ; 
 
                                 } 
 
                         } 
 
                         r e t u r n   r e t ; 
 
                 } 
 
         } 
 
 
 
 
 
 
 
 
 
         / * * 
 
           *   g e t _ v e r t e x pe0R0Y0_00n0000
 
           *   
 
           * / 
 
         s e a l e d   c l a s s   N y A R V e r t e x C o u n t e r 
 
         { 
 
                 p u b l i c   r e a d o n l y   i n t [ ]   v e r t e x   =   n e w   i n t [ 1 0 ] ; / /   6 ~0g0JR00
 
 
 
                 p u b l i c   i n t   n u m b e r _ o f _ v e r t e x ; 
 
 
 
                 p r i v a t e   d o u b l e   t h r e s h ; 
 
 
 
                 p r i v a t e   N y A R I n t P o i n t 2 d [ ]   _ c o o r d ; 
 
 
 
 
 
                 p u b l i c   b o o l   g e t V e r t e x ( N y A R I n t P o i n t 2 d [ ]   i _ c o o r d ,   i n t   i _ c o o r d _ l e n ,   i n t   s t ,   i n t   e d ,   d o u b l e   i _ t h r e s h ) 
 
                 { 
 
                         t h i s . n u m b e r _ o f _ v e r t e x   =   0 ; 
 
                         t h i s . t h r e s h   =   i _ t h r e s h ; 
 
                         t h i s . _ c o o r d   =   i _ c o o r d ; 
 
                         r e t u r n   g e t _ v e r t e x ( s t ,   e d ,   i _ c o o r d _ l e n ) ; 
 
                 } 
 
 
 
                 / * * 
 
                   *   s t a t i c   i n t   g e t _ v e r t e x (   i n t   x _ c o o r d [ ] ,   i n t   y _ c o o r d [ ] ,   i n t   s t ,   i n t   e d , d o u b l e   t h r e s h ,   i n t   v e r t e x [ ] ,   i n t   * v n u m )   pen0Nfpe
 
                   *   
 
                   *   @ p a r a m   x _ c o o r d 
 
                   *   @ p a r a m   y _ c o o r d 
 
                   *   @ p a r a m   s t 
 
                   *   @ p a r a m   e d 
 
                   *   @ p a r a m   t h r e s h 
 
                   *   @ r e t u r n 
 
                   * / 
 
                 p r i v a t e   b o o l   g e t _ v e r t e x ( i n t   s t ,   i n t   e d ,   i n t   i _ c o o r d _ l e n ) 
 
                 { 
 
                         / / 00: ^j$Po06 5 5 3 6 0H0j0Q00p0i n t 3 2 g0qbc0f0'YN+Yj0n0g0	Yf0
 
                         / / d m a x o04 WNj0n0g000h0W0f00i n t 6 4 X00j0D0h0000
 
                         i n t   v 1   =   0 ; 
 
                         N y A R I n t P o i n t 2 d [ ]   c o o r d   =   t h i s . _ c o o r d ; 
 
                         i n t   a   =   c o o r d [ e d ] . y   -   c o o r d [ s t ] . y ; 
 
                         i n t   b   =   c o o r d [ s t ] . x   -   c o o r d [ e d ] . x ; 
 
                         i n t   c   =   c o o r d [ e d ] . x   *   c o o r d [ s t ] . y   -   c o o r d [ e d ] . y   *   c o o r d [ s t ] . x ; 
 
                         d o u b l e   d m a x   =   0 ; 
 
                         i f   ( s t   <   e d ) 
 
                         { 
 
                                 / / s t h0e d L01 :S
 
                                 f o r   ( i n t   i   =   s t   +   1 ;   i   <   e d ;   i + + ) 
 
                                 { 
 
                                         d o u b l e   d   =   a   *   c o o r d [ i ] . x   +   b   *   c o o r d [ i ] . y   +   c ; 
 
                                         i f   ( d   *   d   >   d m a x ) 
 
                                         { 
 
                                                 d m a x   =   d   *   d ; 
 
                                                 v 1   =   i ; 
 
                                         } 
 
                                 } 
 
                         } 
 
                         e l s e 
 
                         { 
 
                                 / / s t h0e d L02 :S
 
                                 f o r   ( i n t   i   =   s t   +   1 ;   i   <   i _ c o o r d _ l e n ;   i + + ) 
 
                                 { 
 
                                         d o u b l e   d   =   a   *   c o o r d [ i ] . x   +   b   *   c o o r d [ i ] . y   +   c ; 
 
                                         i f   ( d   *   d   >   d m a x ) 
 
                                         { 
 
                                                 d m a x   =   d   *   d ; 
 
                                                 v 1   =   i ; 
 
                                         } 
 
                                 } 
 
                                 f o r   ( i n t   i   =   0 ;   i   <   e d ;   i + + ) 
 
                                 { 
 
                                         d o u b l e   d   =   a   *   c o o r d [ i ] . x   +   b   *   c o o r d [ i ] . y   +   c ; 
 
                                         i f   ( d   *   d   >   d m a x ) 
 
                                         { 
 
                                                 d m a x   =   d   *   d ; 
 
                                                 v 1   =   i ; 
 
                                         } 
 
                                 } 
 
                         } 
 
 
 
 
 
                         i f   ( d m a x   /   ( d o u b l e ) ( a   *   a   +   b   *   b )   >   t h r e s h ) 
 
                         { 
 
                                 i f   ( ! g e t _ v e r t e x ( s t ,   v 1 ,   i _ c o o r d _ l e n ) ) 
 
                                 { 
 
                                         r e t u r n   f a l s e ; 
 
                                 } 
 
                                 i f   ( n u m b e r _ o f _ v e r t e x   >   5 ) 
 
                                 { 
 
                                         r e t u r n   f a l s e ; 
 
                                 } 
 
                                 v e r t e x [ n u m b e r _ o f _ v e r t e x ]   =   v 1 ; / /   v e r t e x [ ( * v n u m ) ]   =   v 1 ; 
 
                                 n u m b e r _ o f _ v e r t e x + + ; / /   ( * v n u m ) + + ; 
 
 
 
                                 i f   ( ! g e t _ v e r t e x ( v 1 ,   e d ,   i _ c o o r d _ l e n ) ) 
 
                                 { 
 
                                         r e t u r n   f a l s e ; 
 
                                 } 
 
                         } 
 
                         r e t u r n   t r u e ; 
 
                 } 
 
         } 
 
 } 