Recently, I encountered a problem. I need to calculate the distance between the point in the plane and the straight line. I found that the mathematical knowledge was returned to the teacher. After Du Niang, I found the calculation method, which is hereby recorded.
Calculation formula from point to line:
Through the formula derivation, the information is obtained
A: Slope of straight line
B: Fixed value – 1
C: Line intercept B
The code is converted to python
def get_point_line_distance(self, point, line): point_x = point point_y = point line_s_x = line line_s_y = line line_e_x = line line_e_y = line #If the line is parallel to the y-axis, the distance is the absolute value of the difference between the x-coordinate of the point and the x-coordinate of any point on the line if line_e_x - line_s_x == 0: return math.fabs(point_x - line_s_x) #If the line is parallel to the x-axis, the distance is the absolute value of the difference between the y-coordinate of the point and the y-coordinate of any point on the line if line_e_y - line_s_y == 0: return math.fabs(point_y - line_s_y) #Slope k = (line_e_y - line_s_y) / (line_e_x - line_s_x) #Intercept b = line_s_y - k * line_s_x #Bring in the formula to get the distance dis dis = math.fabs(k * point_x - point_y + b) / math.pow(k * k + 1, 0.5) return dis
The above Python implementation of finding the distance between a point in a plane and a straight line is all I share with you. I hope I can give you a reference and I hope you can support developeppaer more.