/ *   
 
   *   P R O J E C T :   N y A R T o o l k i t C S 
 
   *   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
   *   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 C S   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 > 
 
   *   
 
   * / 
 
 u s i n g   S y s t e m . D i a g n o s t i c s ; 
 
 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0Wpx , y h00E^0ؚU0g0wb_0[W0~0Y00
 
           * / 
 
         p u b l i c   c l a s s   N y A R I n t R e c t 
 
         { 
 
                 / * *   wb_n0]
Nn0p( X ) * / 
 
                 p u b l i c   i n t   x ; 
 
                 / * *   wb_n0]
Nn0p( Y ) * / 
 
                 p u b l i c   i n t   y ; 
 
                 / * *   wb_n0E^( X ) * / 
 
                 p u b l i c   i n t   w ; 
 
                 / * *   wb_n0ؚU0( Y ) * / 
 
                 p u b l i c   i n t   h ; 
 
                 / * * 
 
                   *   0000000g0Y00
 
                   *   *gRgSn00000000ubW0~0Y00
 
                   * / 
 
                 p u b l i c   N y A R I n t R e c t ( ) 
 
                 { 
 
                 } 
 
                 / * * 
 
                   *   0000000g0Y00Rg$P0c[W0f00000000ubW0~0Y00
 
                   *   @ p a r a m   i _ x 
 
                   *   { @ l i n k   # x } n0$P
 
                   *   @ p a r a m   i _ y 
 
                   *   { @ l i n k   # y } n0$P
 
                   *   @ p a r a m   i _ w 
 
                   *   { @ l i n k   # w } n0$P
 
                   *   @ p a r a m   i _ h 
 
                   *   { @ l i n k   # h } n0$P
 
                   * / 
 
                 p u b l i c   N y A R I n t R e c t ( i n t   i _ x ,   i n t   i _ y ,   i n t   i _ w ,   i n t   i _ h ) 
 
                 { 
 
                         t h i s . s e t V a l u e ( i _ x ,   i _ y ,   i _ w ,   i _ h ) ; 
 
                 } 
 
                 / * * 
 
                   *   S0n0peo00pƖT0SbY00wb_0{W0f00000000k0000W0~0Y00
 
                   *   @ p a r a m   i _ v e r t e x 
 
                   *   pƖT0<h
}W0_0MR
 
                   *   @ p a r a m   i _ n u m _ o f _ v e r t e x 
 
                   *   {[ah0Y00 }n0pe
 
                   * / 
 
                 p u b l i c   v o i d   s e t A r e a R e c t ( N y A R D o u b l e P o i n t 2 d [ ]   i _ v e r t e x ,   i n t   i _ n u m _ o f _ v e r t e x ) 
 
                 { 
 
                         / / 0000Bl000
 
                         i n t   x m a x ,   x m i n ,   y m a x ,   y m i n ; 
 
                         x m i n   =   x m a x   =   ( i n t ) i _ v e r t e x [ i _ n u m _ o f _ v e r t e x   -   1 ] . x ; 
 
                         y m i n   =   y m a x   =   ( i n t ) i _ v e r t e x [ i _ n u m _ o f _ v e r t e x   -   1 ] . y ; 
 
                         f o r   ( i n t   i   =   i _ n u m _ o f _ v e r t e x   -   2 ;   i   > =   0 ;   i - - ) 
 
                         { 
 
                                 i f   ( i _ v e r t e x [ i ] . x   <   x m i n ) 
 
                                 { 
 
                                         x m i n   =   ( i n t ) i _ v e r t e x [ i ] . x ; 
 
                                 } 
 
                                 e l s e   i f   ( i _ v e r t e x [ i ] . x   >   x m a x ) 
 
                                 { 
 
                                         x m a x   =   ( i n t ) i _ v e r t e x [ i ] . x ; 
 
                                 } 
 
                                 i f   ( i _ v e r t e x [ i ] . y   <   y m i n ) 
 
                                 { 
 
                                         y m i n   =   ( i n t ) i _ v e r t e x [ i ] . y ; 
 
                                 } 
 
                                 e l s e   i f   ( i _ v e r t e x [ i ] . y   >   y m a x ) 
 
                                 { 
 
                                         y m a x   =   ( i n t ) i _ v e r t e x [ i ] . y ; 
 
                                 } 
 
                         } 
 
                         t h i s . h   =   y m a x   -   y m i n   +   1 ; 
 
                         t h i s . x   =   x m i n ; 
 
                         t h i s . w   =   x m a x   -   x m i n   +   1 ; 
 
                         t h i s . y   =   y m i n ; 
 
                 } 
 
                 / * * 
 
                   *   S0n0peo00pƖT0SbY00wb_0{W0f00000000k0000W0~0Y00
 
                   *   @ p a r a m   i _ v e r t e x 
 
                   *   pƖT0<h
}W0_0MR
 
                   *   @ p a r a m   i _ n u m _ o f _ v e r t e x 
 
                   *   {[ah0Y00 }n0pe
 
                   * / 
 
                 p u b l i c   v o i d   s e t A r e a R e c t ( N y A R I n t P o i n t 2 d [ ]   i _ v e r t e x ,   i n t   i _ n u m _ o f _ v e r t e x ) 
 
                 { 
 
                         / / 0000Bl000
 
                         i n t   x m a x ,   x m i n ,   y m a x ,   y m i n ; 
 
                         x m i n   =   x m a x   =   ( i n t ) i _ v e r t e x [ i _ n u m _ o f _ v e r t e x   -   1 ] . x ; 
 
                         y m i n   =   y m a x   =   ( i n t ) i _ v e r t e x [ i _ n u m _ o f _ v e r t e x   -   1 ] . y ; 
 
                         f o r   ( i n t   i   =   i _ n u m _ o f _ v e r t e x   -   2 ;   i   > =   0 ;   i - - ) 
 
                         { 
 
                                 i f   ( i _ v e r t e x [ i ] . x   <   x m i n ) 
 
                                 { 
 
                                         x m i n   =   ( i n t ) i _ v e r t e x [ i ] . x ; 
 
                                 } 
 
                                 e l s e   i f   ( i _ v e r t e x [ i ] . x   >   x m a x ) 
 
                                 { 
 
                                         x m a x   =   ( i n t ) i _ v e r t e x [ i ] . x ; 
 
                                 } 
 
                                 i f   ( i _ v e r t e x [ i ] . y   <   y m i n ) 
 
                                 { 
 
                                         y m i n   =   ( i n t ) i _ v e r t e x [ i ] . y ; 
 
                                 } 
 
                                 e l s e   i f   ( i _ v e r t e x [ i ] . y   >   y m a x ) 
 
                                 { 
 
                                         y m a x   =   ( i n t ) i _ v e r t e x [ i ] . y ; 
 
                                 } 
 
                         } 
 
                         t h i s . h   =   y m a x   -   y m i n   +   1 ; 
 
                         t h i s . x   =   x m i n ; 
 
                         t h i s . w   =   x m a x   -   x m i n   +   1 ; 
 
                         t h i s . y   =   y m i n ; 
 
                 } 
 
 
 
                 / * * 
 
                   *   S0n0peo00wb_0WQk00000W0~0Y00
 
                   *   @ p a r a m   i _ l e f t 
 
                   *   0000Y00]
 
                   *   @ p a r a m   i _ t o p 
 
                   *   0000Y00
N
 
                   *   @ p a r a m   i _ r i g h t 
 
                   *   0000Y00S
 
                   *   @ p a r a m   i _ b o t t o m 
 
                   *   0000Y00N
 
                   * / 
 
                 p u b l i c   v o i d   c l i p ( i n t   i _ l e f t ,   i n t   i _ t o p ,   i n t   i _ r i g h t ,   i n t   i _ b o t t o m ) 
 
                 { 
 
                         i n t   x   =   t h i s . x ; 
 
                         i n t   y   =   t h i s . y ; 
 
                         i n t   r   =   x   +   t h i s . w   -   1 ; 
 
                         i n t   b   =   y   +   t h i s . h   -   1 ; 
 
                         i f   ( x   <   i _ l e f t ) 
 
                         { 
 
                                 x   =   i _ l e f t ; 
 
                         } 
 
                         e l s e   i f   ( x   >   i _ r i g h t ) 
 
                         { 
 
                                 x   =   i _ r i g h t ; 
 
                         } 
 
                         i f   ( y   <   i _ t o p ) 
 
                         { 
 
                                 y   =   i _ t o p ; 
 
                         } 
 
                         e l s e   i f   ( y   >   i _ b o t t o m ) 
 
                         { 
 
                                 y   =   i _ b o t t o m ; 
 
                         } 
 
                         i n t   l ; 
 
                         l   =   ( r   >   i _ r i g h t )   ?   i _ r i g h t   -   x   :   r   -   x ; 
 
                         i f   ( l   <   0 ) 
 
                         { 
 
                                 t h i s . w   =   0 ; 
 
                         } 
 
                         e l s e 
 
                         { 
 
                                 t h i s . w   =   l   +   1 ; 
 
                         } 
 
                         l   =   ( b   >   i _ b o t t o m )   ?   i _ b o t t o m   -   y   :   b   -   y ; 
 
                         i f   ( l   <   0 ) 
 
                         { 
 
                                 t h i s . h   =   0 ; 
 
                         } 
 
                         e l s e 
 
                         { 
 
                                 t h i s . h   =   l   +   1 ; 
 
                         } 
 
                         t h i s . x   =   x ; 
 
                         t h i s . y   =   y ; 
 
                         r e t u r n ; 
 
                 } 
 
 
 
                 / * * 
 
                   *   S0n0peo00pL0wb_n0{VQk0B00K0$R[W0~0Y00
 
                   *   @ p a r a m   i _ x 
 
                   *   gY00^j( X ) 
 
                   *   @ p a r a m   i _ y 
 
                   *   gY00^j( Y ) 
 
                   *   @ r e t u r n 
 
                   *  0pL0wb_n0-Nk0B00p00t r u e 0ԏW0~0Y00
 
                   * / 
 
                 p u b l i c   b o o l   i s I n n e r P o i n t ( i n t   i _ x ,   i n t   i _ y ) 
 
                 { 
 
                         i n t   x   =   i _ x   -   t h i s . x ; 
 
                         i n t   y   =   i _ y   -   t h i s . y ; 
 
 
 
                         r e t u r n   ( 0   < =   x   & &   x   <   t h i s . w   & &   0   < =   y   & &   y   <   t h i s . h ) ; 
 
                 } 
 
                 / * * 
 
                   *   S0n0peo00pL0wb_n0{VQk0B00K0$R[W0~0Y00
 
                   *   @ p a r a m   i _ p o s 
 
                   *   gY00^j
 
                   *   @ r e t u r n 
 
                   *  0pL0wb_n0-Nk0B00p00t r u e 0ԏW0~0Y00
 
                   * / 
 
                 p u b l i c   b o o l   i s I n n e r P o i n t ( N y A R D o u b l e P o i n t 2 d   i _ p o s ) 
 
                 { 
 
                         i n t   x   =   ( i n t ) i _ p o s . x   -   t h i s . x ; 
 
                         i n t   y   =   ( i n t ) i _ p o s . y   -   t h i s . y ; 
 
                         r e t u r n   ( 0   < =   x   & &   x   <   t h i s . w   & &   0   < =   y   & &   y   <   t h i s . h ) ; 
 
                 } 
 
                 / * * 
 
                   *   S0n0peo00pL0wb_n0{VQk0B00K0$R[W0~0Y00
 
                   *   @ p a r a m   i _ p o s 
 
                   *   gY00^j
 
                   *   @ r e t u r n 
 
                   *  0pL0wb_n0-Nk0B00p00t r u e 0ԏW0~0Y00
 
                   * / 
 
                 p u b l i c   b o o l   i s I n n e r P o i n t ( N y A R I n t P o i n t 2 d   i _ p o s ) 
 
                 { 
 
                         i n t   x   =   i _ p o s . x   -   t h i s . x ; 
 
                         i n t   y   =   i _ p o s . y   -   t h i s . y ; 
 
                         r e t u r n   ( 0   < =   x   & &   x   <   t h i s . w   & &   0   < =   y   & &   y   <   t h i s . h ) ; 
 
                 } 
 
                 / * * 
 
                   *   S0n0peo00_pen0wb_L00S0n0wb_Qk0B00K0$R[W0~0Y00
 
                   *   @ p a r a m   i _ r e c t 
 
                   *   QtPk0B00K0y00wb_
 
                   *   @ r e t u r n 
 
                   *  0wb_L0QtPk0B00p00t r u e 0ԏW0~0Y00
 
                   * / 
 
                 p u b l i c   b o o l   i s I n n e r R e c t ( N y A R I n t R e c t   i _ r e c t ) 
 
                 { 
 
                         D e b u g . A s s e r t ( i _ r e c t . w   > =   0   & &   i _ r e c t . h   > =   0 ) ; 
 
                         i n t   l x   =   i _ r e c t . x   -   t h i s . x ; 
 
                         i n t   l y   =   i _ r e c t . y   -   t h i s . y ; 
 
                         i n t   l w   =   l x   +   i _ r e c t . w ; 
 
                         i n t   l h   =   l y   +   i _ r e c t . h ; 
 
                         r e t u r n   ( 0   < =   l x   & &   l x   <   t h i s . w   & &   0   < =   l y   & &   l y   <   t h i s . h   & &   l w   < =   t h i s . w   & &   l h   < =   t h i s . h ) ; 
 
                 } 
 
                 / * * 
 
                   *   S0n0peo00_peg0[U000wb_L00S0n0wb_Qk0B00K0$R[W0~0Y00
 
                   *   @ p a r a m   i _ x 
 
                   *   QtPk0B00K0y00wb_n0]
N^j( X ) 
 
                   *   @ p a r a m   i _ y 
 
                   *   QtPk0B00K0y00wb_n0]
N^j( Y ) 
 
                   *   @ p a r a m   i _ w 
 
                   *   QtPk0B00K0y00wb_n0E^
 
                   *   @ p a r a m   i _ h 
 
                   *   QtPk0B00K0y00wb_n0ؚU0
 
                   *   @ r e t u r n 
 
                   *  0wb_L0QtPk0B00p00t r u e 0ԏW0~0Y00
 
                   * / 
 
                 p u b l i c   b o o l   i s I n n e r R e c t ( i n t   i _ x ,   i n t   i _ y ,   i n t   i _ w ,   i n t   i _ h ) 
 
                 { 
 
                         D e b u g . A s s e r t ( i _ w   > =   0   & &   i _ h   > =   0 ) ; 
 
                         i n t   l x   =   i _ x   -   t h i s . x ; 
 
                         i n t   l y   =   i _ y   -   t h i s . y ; 
 
                         i n t   l w   =   l x   +   i _ w ; 
 
                         i n t   l h   =   l y   +   i _ h ; 
 
                         r e t u r n   ( 0   < =   l x   & &   l x   <   t h i s . w   & &   0   < =   l y   & &   l y   <   t h i s . h   & &   l w   < =   t h i s . w   & &   l h   < =   t h i s . h ) ; 
 
                 } 
 
                 / * * 
 
                   *   S0n0peo00d0n0wb_n0[҉pTXn0ݍn0NWN$P0{W0~0Y00
 
                   *   @ p a r a m   i _ r e c t 2 
 
                   *   kY00wb_
 
                   *   @ r e t u r n 
 
                   *   ]
N0SNn0pTXn0ݍn0NWN$P
 
                   * / 
 
                 p u b l i c   i n t   s q D i a g o n a l P o i n t D i f f ( N y A R I n t R e c t   i _ r e c t 2 ) 
 
                 { 
 
                         i n t   w 1 ,   w 2 ; 
 
                         i n t   r e t ; 
 
                         w 1   =   t h i s . x   -   i _ r e c t 2 . x ; 
 
                         w 2   =   t h i s . y   -   i _ r e c t 2 . y ; 
 
                         r e t   =   w 1   *   w 1   +   w 2   *   w 2 ; 
 
                         w 1   + =   t h i s . w   -   i _ r e c t 2 . w ; 
 
                         w 2   + =   t h i s . h   -   i _ r e c t 2 . h ; 
 
                         r e t   + =   w 1   *   w 1   +   w 2   *   w 2 ; 
 
                         r e t u r n   r e t ; 
 
                 } 
 
                 / * * 
 
                   *   S0n0peo00wb_n0[҉ݍn0NWNݍ▒0ԏW0~0Y00
 
                   *   @ r e t u r n 
 
                   *   wb_n0[҉ݍn0NWN$P0
 
                   * / 
 
                 p u b l i c   i n t   g e t D i a g o n a l S q D i s t ( ) 
 
                 { 
 
                         i n t   l h   =   t h i s . h ; 
 
                         i n t   l w   =   t h i s . w ; 
 
                         r e t u r n   l h   *   l h   +   l w   *   l w ; 
 
                 } 
 
 
 
                 / * * 
 
                   *   S0n0peo00000000n0$P0000000k0000W0~0Y00
 
                   *   @ p a r a m   i _ s o u r c e 
 
                   *   000Y00$P0<h
}W0_00000000
 
                   * / 
 
                 p u b l i c   v o i d   s e t V a l u e ( N y A R I n t R e c t   i _ s o u r c e ) 
 
                 { 
 
                         t h i s . x   =   i _ s o u r c e . x ; 
 
                         t h i s . y   =   i _ s o u r c e . y ; 
 
                         t h i s . h   =   i _ s o u r c e . h ; 
 
                         t h i s . w   =   i _ s o u r c e . w ; 
 
                 } 
 
                 / * * 
 
                   *   S0n0peo00000000k0$P0000W0~0Y00
 
                   *   @ p a r a m   i _ x 
 
                   *   eW0D0{ @ l i n k   # x } n0$P
 
                   *   @ p a r a m   i _ y 
 
                   *   eW0D0{ @ l i n k   # y } n0$P
 
                   *   @ p a r a m   i _ w 
 
                   *   eW0D0{ @ l i n k   # w } n0$P
 
                   *   @ p a r a m   i _ h 
 
                   *   eW0D0{ @ l i n k   # h } n0$P
 
                   * / 
 
                 p u b l i c   v o i d   s e t V a l u e ( i n t   i _ x ,   i n t   i _ y ,   i n t   i _ w ,   i n t   i _ h ) 
 
                 { 
 
                         t h i s . x   =   i _ x ; 
 
                         t h i s . y   =   i _ y ; 
 
                         t h i s . h   =   i _ h ; 
 
                         t h i s . w   =   i _ w ; 
 
                 } 
 
 
 
         } 
 
 } 
 
 