inpolygon for Python - Examples of matplotlib.path.Path contains_points() method?

后端 未结 3 2264
温柔的废话
温柔的废话 2021-02-05 23:07

I have been searching for a python alternative to MATLAB\'s inpolygon() and I have come across contains_points as a good option.

However, the docs are a little bare with

3条回答
  •  独厮守ぢ
    2021-02-05 23:27

    Make sure that the vertices are ordered as wanted. Below vertices are ordered in a way that the resulting path is a pair of triangles rather than a rectangle. So, contains_points only returns True for points inside any of the triangles.

    >>> p = path.Path(np.array([bfp1, bfp2, bfp4, bfp3]))
    >>> p
    Path([[ 5.53147871  0.78330843]
     [ 1.78330843  5.46852129]
     [ 0.53147871 -3.21669157]
     [-3.21669157  1.46852129]], None)
    >>> IsPointInside = np.array([[1, 2], [1, 9]])
    >>> IsPointInside
    array([[1, 2],
           [1, 9]])
    >>> p.contains_points(IsPointInside)
    array([False, False], dtype=bool)
    >>> 
    

    The output for the first point would have been True if bfp3 and bfp4 were swapped.

提交回复
热议问题