Assuming two points, (x1, y1) and (x2, y2), how do we find the minimum number of steps required to reach (x2, y2) from (x1, y1), assuming we can move vertically, horizontally or diagonally?
I have the following code that produces this result, but does so slowly. I'd like this result in O(1). How can I do this, preferably without using libraries?
def _dist(x1, y1, x2, y2):
steps = 0
while (x1, y1) != (x2, y2):
dX, dY = calculate_delta(x1, y1, x2, y2)
(x1, y1) = (x1 + dX, y1 + dY)
steps += 1
return steps
def calculate_delta(s_i, s_j, e_i, e_j):
i = 0 if s_i == e_i else -1 if e_i < s_i else 1
j = 0 if s_j == e_j else -1 if e_j < s_j else 1
return i, j