如何在凸多边形的周长上生成随机/统一点?

2024-06-17 09:09:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我有什么?

我有一个凸多边形,看起来像这样:

enter image description here

使用以下代码:

import numpy as np
from scipy.spatial import ConvexHull
import matplotlib.pyplot as plt


# Generate some random points for the demo.
np.random.seed(4321)
pts = 0.1 + 0.8*np.random.rand(15, 2)

ch = ConvexHull(pts)

# Get the indices of the hull points.
hull_indices = ch.vertices

# These are the actual points.
hull_pts = pts[hull_indices, :]

plt.plot(hull_pts[:, 0], hull_pts[:, 1], 'ko', markersize=10)
plt.fill(hull_pts[:, 0], hull_pts[:, 1], fill=False, edgecolor='b')
plt.xlim(0, 1)
plt.ylim(0, 1)
plt.show()

我想要什么?

  1. 我想在多边形的周长上生成N随机点
  2. 我想在示波器上的eaqul周界中记录N

我可以用scipy.spatial来做吗?怎么做


Tags: theimportasnppltrandomscipych
1条回答
网友
1楼 · 发布于 2024-06-17 09:09:47

此代码将值从间隔(0,1)映射到多边形周长,并执行1。(2.考虑到这种解决方案,这是微不足道的):

from scipy.spatial.distance import pdist

class IntervalToPerimeter:
    def __init__(self, vertices):
        self.vertices = np.concatenate([vertices, vertices[0][None]])
        self.mapping = np.array([pdist(self.vertices[i:i+2]) for i in range(len(self.vertices)-1)]).cumsum()
        self.mapping /= self.mapping.max()
        
    def transform(self, points):
        indices = (points[:, None] < self.mapping).argmax(axis=1)
        a, b = np.concatenate([[0], self.mapping])[indices], self.mapping[indices]
        
        return ((points - a)[:, None] * self.vertices[indices] + (b-points)[:, None] * self.vertices[indices+1])/(b-a)[:, None]

itp = IntervalToPerimeter(hull_pts)

transformed_points = itp.transform(np.random.uniform(size=50))

enter image description here

相关问题 更多 >