Pick all points one by one and try the next points till the difference between y coordinates is smaller than d . This is a proven fact that this loop runs at most 6 times
for i in range ( size ) : NEW_LINE INDENT j = i + 1 NEW_LINE while j < size and ( strip [ j ] . y - strip [ i ] . y ) < min_val : NEW_LINE INDENT min_val = dist ( strip [ i ] , strip [ j ] ) NEW_LINE j += 1 NEW_LINE DEDENT DEDENT return min_val NEW_LINE