Calculate perpendicular distance