Python Two Realization Methods of of for Calculating the Straight Line Distance between Two Points
- 2021-07-10 20:18:14
- OfStack
Method 1:
# Import math Bag
import math
# Function that defines points
class Point:
def __init__(self,x=0,y=0):
self.x=x
self.y=y
def getx(self):
return self.x
def gety(self):
return self.y
# Define a straight line function
class Getlen:
def __init__(self,p1,p2):
self.x=p1.getx()-p2.getx()
self.y=p1.gety()-p2.gety()
# Use math.sqrt () Find the square root
self.len= math.sqrt((self.x**2)+(self.y**2))
# Define the function to get the length of a straight line
def getlen(self):
return self.len
# Set point p1 The coordinates of are ( 0,0 )
p1=Point(0,0)
# Set point p2 The coordinates of are ( 3,4 )
p2=Point(3,4)
# Define an object
l=Getlen(p1,p2)
# Gets the length of a straight line between two points
d=l.getlen())
Method 2:
import numpy as np
import math
p1=np.array([0,0])
p2=np.array([1000,2000])
p3=p2-p1
p4=math.hypot(p3[0],p3[1])
print(p4)