Python distance measurement method, python Distance Measurement
The reason for writing this is actually to hope to have some concepts about distance. Of course, this is also very basic, but just a thousand miles of travel begins with a single step. All kinds of path algorithms, such as a *, will use this.
There are three ways to measure the distance.
1. Euclidean distance, which is the most commonly used Distance Measurement Method
(X1-x2) ^ 2 + (y1-y2) ^ 2) ^ 0.5
Obtain the distance from a prototype area.
# Set the starting coordinate to the origin, that is, (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. Block distance: This is generally used in many games. It is called blocks because Western streets are usually southeast, northeast, southwest, and northwest.
| X1-x2 | + | y1-y2 |
Obtain the distance of a diamond area.
# Set the starting coordinate to the origin, that is, (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 distance between the checkerboard. This is easy to understand. It's like a square lattice like a checkerboard.
Max (| x1-x2 |, | y1-y2 |)
Obtain the distance from a Square area.
# Set the starting coordinate to the origin, that is, (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
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.