public boolean intersects ( Shape shape ) { if ( shape instanceof Circle ) { Circle other = ( Circle ) shape ; float totalRad2 = getRadius ( ) + other . getRadius ( ) ; if ( Math . abs ( other . getCenterX ( ) - getCenterX ( ) ) > totalRad2 ) { return false ; } if ( Math . abs ( other . getCenterY ( ) - getCenterY ( ) ) > totalRad2 ) { return false ; } totalRad2 *= totalRad2 ; float dx = Math . abs ( other . getCenterX ( ) - getCenterX ( ) ) ; float dy = Math . abs ( other . getCenterY ( ) - getCenterY ( ) ) ; return totalRad2 >= ( ( dx * dx ) + ( dy * dy ) ) ; } else if ( shape instanceof Rectangle ) { return intersects ( ( Rectangle ) shape ) ; } else { return super . intersects ( shape ) ; } }
Check if this circle touches another