请建议我如何在二维域上生成点。假设我们有正方形、圆形、星形域、随机草图域等,那么我们如何获得整个内部域以及域边界上的点。
在 python 或 Matlab 编码中。
import numpy as np
import matplotlib.pyplot as plt
def shape(x_left,x_right,num_x,num_y,top_func,bottom_func):
x = np.linspace(x_left,x_right,num_x)
y_top = top_func(x)
y_bottom = bottom_func(x)
y = np.linspace(y_bottom,y_top,num_y)
return np.c_[np.tile(x,num_y),np.ravel(y)]
def plot_points(points):
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_aspect('equal',adjustable='box')
plt.scatter(points[:,0],points[:,1])
plt.show()
circle_top = lambda x: np.sqrt(1-x**2)
circle_bottom = lambda x: -np.sqrt(1-x**2)
circle_points = shape(-1,1,20,20,circle_top,circle_bottom)
plot_points(circle_points)
square_top = lambda x: 1*np.ones(x.shape)
square_bottom = lambda x: -1*np.ones(x.shape)
square_points = shape(-1,1,20,20,square_top,square_bottom)
plot_points(square_points)