重新采样多边形坐标以均匀间隔它们

问题描述

我有一个这样的多边形:

enter image description here

顶点顺序正确(我认为是逆时针):

coords = [
    {x: 428.0,y: 273.0},{x: 410.0,y: 335.0},{x: 398.0,y: 350.0},{x: 384.0,y: 355.0},{x: 363.0,y: 410.0},{x: 354.0,y: 424.0},{x: 318.0,y: 455.0},{x: 299.0,y: 458.0},{x: 284.0,y: 464.0},{x: 250.0,y: 490.0},{x: 229.0,y: 492.0},{x: 204.0,y: 484.0},{x: 187.0,y: 469.0},{x: 176.0,y: 449.0},{x: 164.0,y: 435.0},{x: 144.0,y: 392.0},{x: 132.0,y: 349.0},{x: 122.0,y: 334.0},{x: 120.0,y: 324.0},{x: 121.0,y: 294.0},{x: 119.0,y: 274.0},y: 264.0},{x: 118.0,y: 249.0},y: 224.0},y: 209.0},{x: 130.0,y: 194.0},{x: 138.0,y: 159.0},{x: 147.0,y: 139.0},{x: 155.0,y: 112.0},{x: 170.0,y: 89.0},{x: 190.0,y: 67.0},{x: 220.0,y: 54.0},{x: 280.0,y: 47.0},{x: 310.0,y: 55.0},{x: 330.0,y: 56.0},{x: 345.0,y: 60.0},{x: 355.0,{x: 367.0,y: 80.0},{x: 375.0,y: 84.0},{x: 382.0,y: 95.0},{x: 395.0,y: 106.0},{x: 403.0,y: 120.0},y: 151.0},{x: 417.0,y: 210.0},{x: 419.0,y: 215.0},{x: 425.0,y: 218.0},{x: 431.0,y: 225.0},{x: 430.0,y: 265.0},]

我想对这些顶点重新采样以均匀分布它们。这可能可以通过重新插入 2D 顶点来完成,但这不是我喜欢的事情,我想知道在尝试重新发明轮子之前,图书馆是否已经这样做了。我该怎么办?

编辑

例如,使用 n=15,我得到一组如下所示的坐标:

enter image description here

编辑 2

使用@kaya3 解决方案,n=128

enter image description here

解决方法

这是一个可能的解决方案:通过将多边形的总周长除以 k 并线性插值来选择 k 个均匀分布的点。

import math

def distance(p,q):
    return math.hypot(q['x'] - p['x'],q['y'] - p['y'])

def lerp(p,q,t):
    return {
        'x': (1 - t) * p['x'] + t * q['x'],'y': (1 - t) * p['y'] + t * q['y']
    }

def resample_polygon(polygon,k):
    edges = list(zip(polygon,polygon[1:] + polygon[:1]))
    arc_length = sum(distance(p,q) for p,q in edges) / k
    result = [polygon[0]]
    t = 0
    for p,q in edges:
        d_t = distance(p,q) / arc_length
        while t + d_t >= len(result) < k:
            v = lerp(p,(len(result) - t) / d_t)
            result.append(v)
        t += d_t
    return result