my $app_w = 400; my $app_h = 300; my $image_w = 600; my $image_h = 200; my $y_scale = $app_w / $image_w; my $x_scale = $app_h / $image_h; if( $y_scale > $x_scale ) { print "scale on x axis(scale y < scale x ==> $y_scale < $x_scale)\n"; printf("y-axis overlap: %s\n", int(int($image_h - $app_h) / 2)); }elsif( $y_scale < $x_scale ) { print "scale on y axis(scale y < scale x ==> $y_scale < $x_scale)\n"; printf("x-axis overlap: %s\n", int(int($image_w - $app_w) / 2)); }else{ print 'doesn\'t matter, it will fit. It has the same aspect ratio as the window.'; }