How to get list of points inside a polygon in python?

前端 未结 5 2057
春和景丽
春和景丽 2020-12-08 05:18

I searched a lot and cant find any practical answer to my question. I have a polygon. For example:

    [(86, 52), (85, 52), (81, 53), (80, 52), (79, 48), (81         


        
5条回答
  •  难免孤独
    2020-12-08 06:02

    I suggest to use matplotlib contains_points()

    from matplotlib.path import Path
    
    tupVerts=[(86, 52), (85, 52), (81, 53), (80, 52), (79, 48), (81, 49), (86, 53),
     (85, 51), (82, 54), (84, 54), (83, 49), (81, 52), (80, 50), (81, 48),
     (85, 50), (86, 54), (85, 54), (80, 48), (79, 50), (85, 49), (80, 51),
     (85, 53), (82, 49), (83, 54), (82, 53), (84, 49), (79, 49)]
    
    
    x, y = np.meshgrid(np.arange(300), np.arange(300)) # make a canvas with coordinates
    x, y = x.flatten(), y.flatten()
    points = np.vstack((x,y)).T 
    
    p = Path(tupVerts) # make a polygon
    grid = p.contains_points(points)
    mask = grid.reshape(300,300) # now you have a mask with points inside a polygon
    

提交回复
热议问题