public boolean markIfClear ( Rectangle2D rect , Object o ) { 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 ) ; if ( debugMark ) System . out . println ( "markIfClear " + rect + " " + indexX + " " + indexY ) ; if ( ( indexX < 0 ) || ( indexX >= countX ) || ( indexY < 0 ) || ( indexY >= countY ) ) // outside box return false ; GridCell gwant = gridArray [ indexY ] [ indexX ] ; if ( gwant . used ) // already taken return false ; if ( null != findIntersection ( rect ) ) return false ; // its ok to use gwant . used = true ; gwant . objectBB = rect ; gwant . o = o ; return true ; }
Check if the given rect intersects an already drawn one . If not set the corresponding cell as marked store object return true meaning ok to draw .