Current location - Training Enrollment Network - Mathematics courses - Python calculates the distance between two coordinates.
Python calculates the distance between two coordinates.
In mathematics, the distance between two points is connected by a straight line, and then the length of this straight line is calculated to know the distance between two points, which is also true in Python. The specific operation is as follows:

The import module in 1.Python needs to use math module, which contains some operation functions, if it needs to deal with mathematics-related operations. You need to import it before using it. The method is as follows: importmath2. Create a class and get a point classpoint: def _ _ init _ _ (self, a = 0, B = 0): self. A = ego. B = bdefgeta (self): return to yourself. Adefgetb (self): Return to self. B In this code, a class named Point is first created and initialized with the coordinates of A and B, and then the coordinates of A and B of an instance are obtained by the methods of getA and getB respectively. 3. Calculate the distance between two points. A class is created above, which represents the coordinates of axis A and axis B.. Next, initialize two points and calculate the distance between them. These two points are p 1, p2. Then divide the square roots of these two numbers through the math module that Python has just imported, and finally get the distance between the two points. The code is classline: def _ _ init _ _ (self, p 1, p2): self.a = p1.geta ()-p2.geta () self.b = p1.getb ()-p2.geta. :return self . lenp 1 = Point( 1,2)p2=Point(4,6)lineLen=Line(p 1,p2)print(lineLen.getlen())。