How to calculate midpoint