I am trying to randomly sample points on a polygon boundary made of arbitrary number of points. The polygon consist of a set of x,y coordinates. I would like to keep the original vertices of the polygon as well as add the randomly sampled points, and preserve the shape of the polygon as much as possible. As shown in this example, given an orginal polygon (blue vertices, left), I want a randomly sampled polygon with N=25 points along the original polygon's boundary (red vertices, right).
下面的代码是我尝试使用样条插值来执行此操作,但插值的结果最终会显着扭曲多边形的形状(使其更圆),特别是如果多边形一开始就有少量顶点。
def single_parametric_interpolate(obj_x_loc,obj_y_loc,numPts=50):
'''
Interpolate a single given bounding box obj_x_loc,obj_y_loc
return a new set of coordinates interpolated on numPts
'''
tck, u =splprep(np.array([obj_x_loc,obj_y_loc]),s=0,per=1)
u_new = np.linspace(u.min(),u.max(),numPts)
new_points = splev(u_new, tck,der=0)
return new_points
有没有更好的方法来进行这种类型的采样,并且不会扭曲多边形的最终形状?