首页 > 其他分享 >计算点到直线的距离

计算点到直线的距离

时间:2023-05-16 22:15:53浏览次数:36  
标签:直线 point 距离 point1 point2 np 点到 line array

 

计算点到直线的距离

import numpy as np

def get_distance_from_point_to_line(point, line_point1, line_point2):
    #对于两点坐标为同一点时,返回点与点的距离
    if line_point1 == line_point2:
        point_array = np.array(point )
        point1_array = np.array(line_point1)
        return np.linalg.norm(point_array -point1_array )
    #计算直线的三个参数
    A = line_point2[1] - line_point1[1]
    B = line_point1[0] - line_point2[0]
    C = (line_point1[1] - line_point2[1]) * line_point1[0] + \
        (line_point2[0] - line_point1[0]) * line_point1[1]
    #根据点到直线的距离公式计算距离
    distance = np.abs(A * point[0] + B * point[1] + C) / (np.sqrt(A**2 + B**2))
    return distance


dis1 = get_distance_from_point_to_line((-2,0),(5,3),(7,3))
print(dis1)

dis2 = get_distance_from_point_to_line((0,0),(0,5),(5,0))
print(dis2)


#

 

输出:

3.0
3.5355339059327373

 

 

#####################

标签:直线,point,距离,point1,point2,np,点到,line,array
From: https://www.cnblogs.com/herd/p/17400224.html

相关文章