If I understand you correctly, you are assuming that always at least one axis is rectangular to your scale direction, and that's the reason that the given code is sufficient.