How to delete a set of meshgrid points inside a circle?

前端 未结 3 615
[愿得一人]
[愿得一人] 2021-01-13 22:10

I am trying to create a meshgrid without some of the points that falls within the circle having specified coordinates and a radius. I am not able to subtract the grid points

3条回答
  •  醉梦人生
    2021-01-13 22:41

    You cannot remove the points within the arrays x and y. It is a 2D problem and the values that need to be removed from x depend on y and inversely.

    What you can do is operate directly on the mesh you created (X and Y). For example,

    import math
    import numpy 
    import matplotlib.pyplot as plt
    
    
    N = 200
    x_start, x_end = -2.0, 2.0 
    y_start, y_end = -2.0, 2.0
    
    
    x = numpy.linspace(x_start, x_end, N)
    y = numpy.linspace(y_start, y_end, N)
    
    circle_x, circle_y, r= 0.0, 0.0, 0.4
    
    X, Y = numpy.meshgrid(x, y)
    
    ## Define points within circle
    pts = (X-circle_x)**2+(Y-circle_y)**2 <= r**2
    
    ## Create a constant mask over grid
    M = numpy.ones(X.shape)
    ## Assign 0 to mask for all points within circle
    M[pts] = 0
    
    size = 10
    fig = plt.figure()
    plt.imshow(M)
    plt.show()
    

    This does not remove any points from X or Y. If instead, you wish to only perform calculations on part of the points, you could do

    pts = (X-circle_x)**2+(Y-circle_y)**2 > r**2
    X = X[pts]
    Y = Y[pts]
    
    plt.scatter(X,Y)
    plt.show()
    

提交回复
热议问题