public static void scaleToRatio ( Rectangle2D fixed , Rectangle2D toScale , boolean doShrink ) { double origWidth = fixed . getWidth ( ) ; double origHeight = fixed . getHeight ( ) ; double toAdaptWidth = toScale . getWidth ( ) ; double toAdaptHeight = toScale . getHeight ( ) ; double scaleWidth = 0 ; double scaleHeight = 0 ; scaleWidth = toAdaptWidth / origWidth ; scaleHeight = toAdaptHeight / origHeight ; double scaleFactor ; if ( doShrink ) { scaleFactor = Math . min ( scaleWidth , scaleHeight ) ; } else { scaleFactor = Math . max ( scaleWidth , scaleHeight ) ; } double newWidth = origWidth * scaleFactor ; double newHeight = origHeight * scaleFactor ; double dw = ( toAdaptWidth - newWidth ) / 2.0 ; double dh = ( toAdaptHeight - newHeight ) / 2.0 ; double newX = toScale . getX ( ) + dw ; double newY = toScale . getY ( ) + dh ; double newW = toAdaptWidth - 2 * dw ; double newH = toAdaptHeight - 2 * dh ; toScale . setRect ( newX , newY , newW , newH ) ; }
Extends or shrinks a rectangle following the ration of a fixed one .