SoFunction
Updated on 2024-10-30

The python distance measurement method

The reason for writing this, in fact, is to hope that there are some concepts of distance, of course, this is also very basic, but the journey of a thousand miles begins with a step well, a variety of path algorithms, such as a* what will be used to this

Distance is measured in three ways

1, European distance, this is the most commonly used distance measurements

((x1-x2)^2 + (y1-y2)^2)^0.5

Get the distance of a prototype region

# Let the starting coordinate be the origin, i.e. (0,0)
y_result = []
for y in range(10,-10,-1):
 x_result = []
 for x in range(-10,10,1):
  #((0-x)**2 + (0-y)**2)**0.5
  if ((0-x)**2 + (0-y)**2)**0.5 <= 9:
   x_result.append('*')
  else:
   x_result.append(' ')
 y_result.append(''.join(x_result))

for i in y_result:
 print i

2, neighborhood distance, this general game will be used more, the reason why called neighborhoods, because the western streets are usually southeast, northeast, southwest, northwest, such as

|x1-x2| + |y1-y2|

Get the distance of a diamond-shaped region

# Let the starting coordinate be the origin, i.e. (0,0)
y_result = []
for y in range(10,-10,-1):
 x_result = []
 for x in range(-10,10,1):
  #(abs(0-x) + abs(0-y))
  if (abs(0-x) + abs(0-y)) <= 9:
   x_result.append('*')
  else:
   x_result.append(' ')
 y_result.append(''.join(x_result))

for i in y_result:
 print i

3, the board distance, this is good to understand, like a chessboard like a square grid

max(|x1-x2|, |y1-y2|)

Get the distance of a square area

# Let the starting coordinate be the origin, i.e. (0,0)
y_result = []
for y in range(10,-10,-1):
 x_result = []
 for x in range(-10,10,1):
  #max(abs(0-x),abs(0-y))
  if max(abs(0-x),abs(0-y)) <= 9:
   x_result.append('*')
  else:
   x_result.append(' ')
 y_result.append(''.join(x_result))

for i in y_result:
 print i

This is the whole content of this article.