Checking if a point is inside a polygon

后端 未结 3 2025
情话喂你
情话喂你 2020-11-30 21:34

I have a class describing a Point (has 2 coordinates x and y) and a class describing a Polygon which has a list of Points which correspond to corners (self.corners) I need t

3条回答
  •  情深已故
    2020-11-30 22:18

    I would suggest using the Path class from matplotlib

    import matplotlib.path as mplPath
    import numpy as np
    
    poly = [190, 50, 500, 310]
    bbPath = mplPath.Path(np.array([[poly[0], poly[1]],
                         [poly[1], poly[2]],
                         [poly[2], poly[3]],
                         [poly[3], poly[0]]]))
    
    bbPath.contains_point((200, 100))
    

    (There is also a contains_points function if you want to test for multiple points)

提交回复
热议问题