updated midpoint algorithm