首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >沿多边形边界生成等距点但CW/CCW

沿多边形边界生成等距点但CW/CCW
EN

Stack Overflow用户
提问于 2020-11-24 22:47:52
回答 2查看 515关注 0票数 2

假设我有一个多边形的顶点,它们都是定向的。我希望沿着这个多边形的边界生成n个等距点。有没有人知道任何现有的包可以这样做,如果不是,一个算法可以使用吗?我在Python工作。例如,如果所讨论的多边形是矩形,下面是我想要的:

在这里输入图像描述

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-11-25 05:23:21

shapely

代码语言:javascript
复制
import shapely.geometry as sg
import shapely.affinity as sa
import matplotlib.pyplot as P
import numpy as np

n = 7
k = 11
ori = sg.Point([0,0])
p = [sg.Point([0,1])]
for j in range(1,n):
    p.append(sa.rotate(p[-1],360/n,origin=ori))
ngon = sg.Polygon(p)
P.figure()
P.plot(*ngon.exterior.xy,"-k")
P.scatter(*np.transpose([ngon.exterior.interpolate(t).xy for t in np.linspace(
    0,ngon.length,k,False)])[0])
P.axis("equal");P.box("off");P.axis("off")
P.show(block=0)

票数 1
EN

Stack Overflow用户

发布于 2020-11-25 16:14:37

添加到可能的解决方案中,所以我有一个记录。这个化身只是使用numpy来强化多边形或多边形的边界。

代码语言:javascript
复制
def _pnts_on_line_(a, spacing=1, is_percent=False):  # densify by distance
"""Add points, at a fixed spacing, to an array representing a line.

**See**  `densify_by_distance` for documentation.

Parameters
----------
a : array
    A sequence of `points`, x,y pairs, representing the bounds of a polygon
    or polyline object.
spacing : number
    Spacing between the points to be added to the line.
is_percent : boolean
    Express the densification as a percent of the total length.

Notes
-----
Called by `pnt_on_poly`.
"""
N = len(a) - 1                                    # segments
dxdy = a[1:, :] - a[:-1, :]                       # coordinate differences
leng = np.sqrt(np.einsum('ij,ij->i', dxdy, dxdy))  # segment lengths
if is_percent:                                    # as percentage
    spacing = abs(spacing)
    spacing = min(spacing / 100, 1.)
    steps = (sum(leng) * spacing) / leng          # step distance
else:
    steps = leng / spacing                        # step distance
deltas = dxdy / (steps.reshape(-1, 1))            # coordinate steps
pnts = np.empty((N,), dtype='O')                  # construct an `O` array
for i in range(N):              # cycle through the segments and make
    num = np.arange(steps[i])   # the new points
    pnts[i] = np.array((num, num)).T * deltas[i] + a[i]
a0 = a[-1].reshape(1, -1)       # add the final point and concatenate
return np.concatenate((*pnts, a0), axis=0)

结果表明,六角密度在4单位点间距。

代码语言:javascript
复制
h = np.array([[-8.66,  5.00], [ 0.00, 10.00], [ 8.66,  5.00], [ 8.66, -5.00],
              [ 0.00,-10.00], [-8.66, -5.00], [-8.66,  5.00]])  # ---- hexagon

增编

正如评论中所建议的那样,我将添加按因素形式的致密化。

代码语言:javascript
复制
h = np.array([[-8.66,  5.00], [-2.5, 5.0], [ 0.00, 10.00], [2.5, 5.0], [ 8.66,  5.00], [ 8.66, -5.00], [ 0.00,-10.00], [-8.66,  5.00]])  # ---- a polygon
_densify_by_factor(h, factor=3)

def _densify_by_factor(a, factor=2):
"""Densify a 2D array using np.interp.

Parameters
----------
a : array
    A 2D array of points representing a polyline/polygon boundary.
fact : number
    The factor to density the line segments by.
"""
a = np.squeeze(a)
n_fact = len(a) * factor
b = np.arange(0, n_fact, factor)
b_new = np.arange(n_fact - 1)     # Where you want to interpolate
c0 = np.interp(b_new, b, a[:, 0])
c1 = np.interp(b_new, b, a[:, 1])
n = c0.shape[0]
c = np.zeros((n, 2))
c[:, 0] = c0
c[:, 1] = c1
return c

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64995977

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档