Python: find if point lay on the border of a polygon

algorithm, computational-geometry, point, polygon, python

Solution

Everyone is overcomplicating things. Here is a short point on polygon, assuming you have a distance function and a small EPSILON.

def pointOnPolygon(point, poly):
    for i in range(len(poly)):
        a, b = poly[i - 1], poly[i]
        if abs(dist(a, point) + dist(b, point) - dist(a, b)) < EPSILON:
            return true
    return false

Problem

I have a point-i and i wish to create a function to know if this point lies on the border of a polygon. using: ``` def point_inside_polygon(x, y, poly): """Deciding if a point is inside (True, False otherwise) a polygon, where poly is a list of pairs (x,y) containing the coordinates of the polygon's vertices. The algorithm is called the 'Ray Casting Method'""" n = len(poly) inside = False p1x, p1y = poly[0] for i in range(n): p2x, p2y = poly[i % n] if y > min(p1y, p2y): if y <= max(p1y, p2y): if x <= max(p1x, p2x): if p1y != p2y: xinters = (y-p1y) * (p2x-p1x) / (p2y-p1y) + p1x if p1x == p2x or x <= xinters: inside = not inside p1x, p1y = p2x, p2y return inside ``` I am able to know only if the points lies within the polygon. ``` poly = [(0,0), (2,0), (2,2), (0,2)] point_inside_polygon(1,1, poly) True point_inside_polygon(0,0, poly) false point_inside_polygon(2,0, poly) False point_inside_polygon(2,2, poly) True point_inside_polygon(0,2, poly) True ``` How can I write a function to find if a point lay on the border of a polygon instead?

Original source

Related problems