Implementation of Python to calculate the distance from point in plane to line

Time:2020-7-15

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[0]
  point_y = point[1]
  line_s_x = line[0][0]
  line_s_y = line[0][1]
  line_e_x = line[1][0]
  line_e_y = line[1][1]
  #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.