Correct Answer: 2. x = (h1 - h2) / 2
Let's say we have two rectangles. One is taller and wider than the other. We want to place and vertically center the smaller rectangle inside the larger one.
The height of the larger rectangle is h1
. The height of the smaller one is h2
. How do we solve for x
, where x
is the vertical offset of one rectangle against the other?
In case you'd like to take a shot at it, here are a few options:
1. x = h1 / 2
2. x = (h1 - h2) / 2
3. x = (h1 + h2) / 2