private boolean intersects ( Rectangle other ) { Rectangle box = other ; Circle circle = this ; if ( box . contains ( x + radius , y + radius ) ) { return true ; } float x1 = box . getX ( ) ; float y1 = box . getY ( ) ; float x2 = box . getX ( ) + box . getWidth ( ) ; float y2 = box . getY ( ) + box . getHeight ( ) ; Line [ ] lines = new Line [ 4 ] ; lines [ 0 ] = new Line ( x1 , y1 , x2 , y1 ) ; lines [ 1 ] = new Line ( x2 , y1 , x2 , y2 ) ; lines [ 2 ] = new Line ( x2 , y2 , x1 , y2 ) ; lines [ 3 ] = new Line ( x1 , y2 , x1 , y1 ) ; float r2 = circle . getRadius ( ) * circle . getRadius ( ) ; Vector2f pos = new Vector2f ( circle . getCenterX ( ) , circle . getCenterY ( ) ) ; for ( int i = 0 ; i < 4 ; i ++ ) { float dis = lines [ i ] . distanceSquared ( pos ) ; if ( dis < r2 ) { return true ; } } return false ; }
Check if this circle touches a rectangle