抓取点 过滤距离较小的点

目录

过滤和已知点距离较小的点

调用代码:

返回组内距离最大的两组点


过滤和已知点距离较小的点

import numpy as np

def dist(p1, p2):
    return np.linalg.norm(np.array(p1) - np.array(p2))

def group_distance(a, b):
    # a, b 都是长度为2的点对
    return min(
        dist(a[0], b[0]) + dist(a[1], b[1]),
        dist(a[0], b[1]) + dist(a[1], b[0])
    )

def filter_groups(a, b_list, threshold=10):
    # 返回距离 >= threshold 的 b 组
    return [b for b in b_list if group_distance(a, b) >= threshold]

调用代码:

a = [(0, 0), (0, 2)]
b_list = [
    [(1, 0), (1, 2)],   # 距离约为 2.0 + 2.0 = 4.0
    [(5, 0), (5, 2)],   # 距离约为 5.0 + 5.0 = 10.0
    [(10, 0), (10, 2)]  # 距离约为 10.0 + 10.0 = 20.0
]

result = filter_groups(a, b_list, threshold=10)
print(result)

返回组内距离最大的两组点

import numpy as np

def point_distance(p1, p2):
    return np.linalg.norm(np.array(p1) - np.array(p2))

import numpy as np

def point_distance(p1, p2):
    return np.linalg.norm(np.array(p1) - np.array(p2))

def top_k_by_inner_distance(b_list, k=2):
    # 每组只有两个点
    groups_with_dist = [(group, point_distance(group[0], group[1])) for group in b_list]
    # 按距离从大到小排序
    groups_with_dist.sort(key=lambda x: x[1], reverse=True)

    # 拆成两个列表分别返回
    top_groups = [group for group, dist in groups_with_dist[:k]]
    top_distances = [dist for group, dist in groups_with_dist[:k]]

    return top_groups, top_distances

使用例子:

b_list = [
    [(1, 0), (1, 2)],
    [(5, 0), (5, 5)],
    [(10, 0), (10, 10)],
    [(3, 3), (6, 7)]
]

result = top_k_by_inner_distance(b_list, k=2)
print(result)
# 输出:
# [[(10, 0), (10, 10)], [(5, 0), (5, 5)]]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI算法网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值