Greetings,
I am trying to bin an array of points (x, y) into an array of boxes [(x0, y0), (x1, y0), (x0, y1), (x1, y1)] (tuples are the corner points)
So far I have the following routine:
def isInside(self, point, x0, x1, y0, y1):
pr1 = getProduct(point, (x0, y0), (x1, y0))
if pr1 >= 0:
pr2 = getProduct(point, (x1, y0), (x1, y1))
if pr2 >= 0:
pr3 = getProduct(point, (x1, y1), (x0, y1))
if pr3 >= 0:
pr4 = getProduct(point, (x0, y1), (x0, y0))
if pr4 >= 0:
return True
return False
def getProduct(origin, pointA, pointB):
product = (pointA[0] - origin[0])*(pointB[1] - origin[1]) - (pointB[0] - origin[0])*(pointA[1] - origin[1])
return product
Is there any better way then point-by-point lookup? Maybe some not-obvious numpy routine?
Thank you!
Are these boxes axis aligned? i.e. are the edges parallel to the coordinate axes? If so this can be done quite efficiently with vectorized comparisons on NumPy arrays.
amending to >= and <= if you prefer them to be inclusive.
If you need it for an arbitrary quadrilateral,
matplotlibactually has a routinematplotlib.nxutils.points_inside_polythat you could use (if you have it installed) or else copy it (it’s BSD-licensed). See this page for a discussion of the algorithms used and other algorithms for inside-a-polygon tests.