modified getOptimalSize

now its take originally width and height. if its not found it call
themself again with inverted sizes.