数据挖掘笔记:点到线段的距离计算

1. 写在前面

最近在搞一个"大曲率弯道"场景的数据挖掘,里面有个逻辑是给定自车的定位坐标和车道线的坐标点,根据点到线段的距离,去找到自车所在的车道中心线。

然后发现这个计算其实在很多场景中都是可以用到的,所以就想通过一篇文章来整理下这块的原理和代码实战,算是把学校学习的向量知识真正的应用到实战中。

2. 原理

首先得知道一个点:点到线段最短距离的运算与点到直线的最短距离的运算二者之间存在一定的差别 ,即求点到线段最短距离时需要考虑参考点在沿线段方向的投影点是否在线段 上,若在线段上才可采用点到直线距离公式。

三种情况:

  1. 点在投射在线段上, 点到线段的距离 等效 点到直线的距离, 图(a)
  2. 点在线段外, 点到线段的距离为该点到最近短点的距离, 图(b)(c )
  3. 点在线段上, 点到线段的距离为0

参考这篇文章, 介绍3种方法:

3. 实战

有了理论, 下面给出代码实战, 用python实现了两版。

python 复制代码
import numpy as np
import math

# 向量之间直接运算
def distance_point_to_segment(point, segment_start, segment_end):
    """
    计算点到线段的最短距离
    :param point: 点的坐标,形如(x, y)
    :param segment_start: 线段起点坐标,形如(x, y)
    :param segment_end: 线段终点坐标,形如(x, y)
    :return: 最短距离
    """
    segment_vec = segment_end - segment_start
    point_vec = point - segment_start
    projection_length = np.dot(point_vec, segment_vec) / np.dot(segment_vec, segment_vec)
    if projection_length < 0:
        return np.linalg.norm(point_vec)
    elif projection_length > 1:
        return np.linalg.norm(point - segment_end)
    else:
        projection = segment_start + projection_length * segment_vec
        return np.linalg.norm(point - projection)

# 如果用坐标的计算方式
def distance_point_to_line_segment(point, start, end):
    """
    计算点到线段的距离
    :param point: 点 (x, y)
    :param start: 线段起点 (x, y)
    :param end: 线段终点 (x, y)
    :return: 点到线段的距离
    """
    px, py = point
    sx, sy = start
    ex, ey = end

    # 计算线段的长度
    line_length = math.sqrt((ex - sx) ** 2 + (ey - sy) ** 2)
    if line_length == 0:
        return math.sqrt((px - sx) ** 2 + (py - sy) ** 2)

    # 计算点到线段所在直线的投影比例
    dot_product = ((px - sx) * (ex - sx) + (py - sy) * (ey - sy)) / (line_length ** 2)
    if dot_product < 0:
        return math.sqrt((px - sx) ** 2 + (py - sy) ** 2)
    elif dot_product > 1:
        return math.sqrt((px - ex) ** 2 + (py - ey) ** 2)
    else:
        # 计算投影点的坐标
        proj_x = sx + dot_product * (ex - sx)
        proj_y = sy + dot_product * (ey - sy)
        return math.sqrt((px - proj_x) ** 2 + (py - proj_y) ** 2)

# 三个点计算曲率
def get_lane_curve(lane, closest_end_point_idx):
    point_len = len(lane.geometry.points)
    point_1 = lane.geometry.points[closest_end_point_idx]
    point_0 = lane.geometry.points[min(closest_end_point_idx + 12, point_len - 2)]
    point_2 = lane.geometry.points[max(closest_end_point_idx - 8, 0)]

    cur_point = np.array((point_1.xyz.x, point_1.xyz.y))

    points = lane.geometry.points
    # 向后搜索30米的点(point_0)
    for i in range(closest_end_point_idx, point_len):
        next_point = np.array((points[i].xyz.x, points[i].xyz.y))
        if np.linalg.norm(next_point - cur_point) >= 15:
            point_0 = points[i]
            break

    # 向前搜索30米的点(point_2)
    for i in range(closest_end_point_idx, -1, -1):
        next_point = np.array((points[i].xyz.x, points[i].xyz.y))
        if np.linalg.norm(next_point - cur_point) >= 15:
            point_2 = points[i]
            break

    point_0 = np.array((point_0.xyz.x, point_0.xyz.y))
    point_1 = np.array((point_1.xyz.x, point_1.xyz.y))
    point_2 = np.array((point_2.xyz.x, point_2.xyz.y))
    x0, y0 = point_0
    x1, y1 = point_1
    x2, y2 = point_2
    cross_product = (x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0)
    if cross_product == 0:
        curve_radius = float('inf')
    else:
        # 计算三边长度
        ab = np.linalg.norm(point_1 - point_0)
        bc = np.linalg.norm(point_1 - point_2)
        ca = np.linalg.norm(point_0 - point_2)
        # 计算曲率半径
        curve_radius = (ab * bc * ca) / (2 * abs(cross_product))
    return curve_radius

lua实现了一版

lua 复制代码
-- 计算点到线段的最短距离
function distance_point_to_segment(point, segment_start, segment_end)
    local segment_vec = vec_sub(segment_end, segment_start)
    local point_vec = vec_sub(point, segment_start)

    local segment_length_sq = vec_dot(segment_vec, segment_vec)
    if segment_length_sq == 0 then
        return vec_norm(point_vec)
    end

    -- 限制在[0,1]区间
    local t = vec_dot(point_vec, segment_vec) / segment_length_sq
    t = math.max(0, math.min(1, t))

    local projection = vec_add(segment_start, vec_mul_scalar(segment_vec, t))
    return vec_norm(vec_sub(point, projection))
end

参考资料:

相关推荐
FairyGirlhub18 分钟前
神经网络的初始化:权重与偏置的数学策略
人工智能·深度学习·神经网络
大写-凌祁4 小时前
零基础入门深度学习:从理论到实战,GitHub+开源资源全指南(2025最新版)
人工智能·深度学习·开源·github
焦耳加热5 小时前
阿德莱德大学Nat. Commun.:盐模板策略实现废弃塑料到单原子催化剂的高值转化,推动环境与能源催化应用
人工智能·算法·机器学习·能源·材料工程
深空数字孪生5 小时前
储能调峰新实践:智慧能源平台如何保障风电消纳与电网稳定?
大数据·人工智能·物联网
wan5555cn5 小时前
多张图片生成视频模型技术深度解析
人工智能·笔记·深度学习·算法·音视频
格林威6 小时前
机器视觉检测的光源基础知识及光源选型
人工智能·深度学习·数码相机·yolo·计算机视觉·视觉检测
今天也要学习吖6 小时前
谷歌nano banana官方Prompt模板发布,解锁六大图像生成风格
人工智能·学习·ai·prompt·nano banana·谷歌ai
Hello123网站6 小时前
glean-企业级AI搜索和知识发现平台
人工智能·产品运营·ai工具
AKAMAI6 小时前
Queue-it 为数十亿用户增强在线体验
人工智能·云原生·云计算
雁于飞6 小时前
vscode中使用git、githup的基操
笔记·git·vscode·学习·elasticsearch·gitee·github