Python _ distance measurement, python Distance Measurement

Source: Internet
Author: User

Python _ distance measurement, 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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.