scripting - Python: checking if point is inside a polygon -


i have class describing point (has 2 coordinates x , y) , class describing polygon has list of points correspond corners (self.corners) need check if point in polygon

here function supposed check if point in in polygon. using ray casting method

def in_me(self, point):         result = false         n = len(self.corners)         p1x = int(self.corners[0].x)         p1y = int(self.corners[0].y)         in range(n+1):             p2x = int(self.corners[i % n].x)             p2y = int(self.corners[i % n].y)             if point.y > min(p1y,p2y):                 if point.x <= max(p1x,p2x):                     if p1y != p2y:                         xinters = (point.y-p1y)*(p2x-p1x)/(p2y-p1y)+p1x                         print xinters                     if p1x == p2x or point.x <= xinters:                         result = not result             p1x,p1y = p2x,p2y          return result 

i run test following shape , point:

pg1 = (0,0), (0,2), (2,2), (2,0) point = (1,1) 

the script happily returns false though point within line. unable find mistake

i suggest using path class matplotlib

import matplotlib.path mplpath import numpy 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 contains_points function if want test multiple points)


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Socket.connect doesn't throw exception in Android -

iphone - How do I keep MDScrollView from truncating my row headers and making my cells look bad? -