public Object findClosest ( Point2D pt ) { Object o = null ; int indexX = ( int ) ( ( pt . getX ( ) - offsetX ) / gridWidth ) ; int indexY = ( int ) ( ( pt . getY ( ) - offsetY ) / gridHeight ) ; if ( debugClosest ) System . out . println ( "findClosest " + pt + " " + indexX + " " + indexY ) ; if ( ( indexX < 0 ) || ( indexX >= countX ) || ( indexY < 0 ) || ( indexY >= countY ) ) // outside box return null ; GridCell gwant = gridArray [ indexY ] [ indexX ] ; if ( gwant . used ) // that was easy return gwant . o ; // check the surrounding points along perimeter of increasing diameter for ( int p = 1 ; p < Math . max ( countX - 1 , countY - 1 ) ; p ++ ) if ( null != ( o = findClosestAlongPerimeter ( pt , indexX , indexY , p ) ) ) return o ; return null ; // nothing found }
Find the closest marked cell to the given point