public Object findIntersection ( Rectangle2D rect ) { double centerX = rect . getX ( ) + rect . getWidth ( ) / 2 ; double centerY = rect . getY ( ) + rect . getHeight ( ) / 2 ; int indexX = ( int ) ( ( centerX - offsetX ) / gridWidth ) ; int indexY = ( int ) ( ( centerY - offsetY ) / gridHeight ) ; // outside box if ( ( indexX < 0 ) || ( indexX >= countX ) || ( indexY < 0 ) || ( indexY >= countY ) ) return null ; // check the surrounding points for ( int y = Math . max ( 0 , indexY - 1 ) ; y <= Math . min ( countY - 1 , indexY + 1 ) ; y ++ ) { for ( int x = Math . max ( 0 , indexX - 1 ) ; x <= Math . min ( countX - 1 , indexX + 1 ) ; x ++ ) { GridCell gtest = gridArray [ y ] [ x ] ; if ( ! gtest . used ) continue ; if ( intersectsOverlap ( rect , gtest . objectBB ) ) // hits an adjacent rectangle return gtest . o ; } } return null ; }
Check if the given rect intersects an already drawn object