# 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
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.

## Java Engineer Interview Questions

The content covers: Java, mybatis, zookeeper, Dubbo, elasticsearch, memcached, redis, mysql, spring, spring boot, springcloud, rabbitmq, Kafka, Linux, etcMybatis interview questions1. What is mybatis?1. Mybatis is a semi ORM (object relational mapping) framework. It encapsulates JDBC internally. During development, you only need to pay attention to the SQL statement itself, and you don’t need to […]