繁体   English   中英

在 Matlab 或 Python 中的二维域上生成笛卡尔点

[英]Generating Cartesian points over 2d domain in Matlab or Python

请建议我如何在二维域上生成点。 假设我们有正方形、圆形、星形域、随机草图域等,那么我们如何获得整个内部域以及域边界上的点。

在 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)

圆圈 正方形

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM