def is_point_in_polygon_v2(p, polygon):
    n = len(polygon)
    inside = False

    j = n - 1

    for i in range(n):
        a = polygon[i]
        b = polygon[j]

        # 1) Cas spécial : point sur le segment -> considéré dedans
        if is_point_on_segment(p, a, b):
            return True

        # 2) Ray casting (rayon horizontal vers +∞ sur X)
        intersect = ((a[1] > p[1]) != (b[1] > p[1]))

        if intersect:
            x_intersect = (
                (b[0] - a[0]) * (p[1] - a[1]) / (b[1] - a[1]) + a[0]
            )

            if p[0] < x_intersect:
                inside = not inside

        j = i

    return inside

def is_point_on_segment(p, a, b, eps=1e-6):
    # p, a, b sont des tuples ou listes (x, y)

    cross = (p[1] - a[1]) * (b[0] - a[0]) - (p[0] - a[0]) * (b[1] - a[1])

    if abs(cross) > eps:
        return False  # pas colinéaire

    dot = (p[0] - a[0]) * (b[0] - a[0]) + (p[1] - a[1]) * (b[1] - a[1])
    if dot < 0:
        return False

    len_sq = (b[0] - a[0]) ** 2 + (b[1] - a[1]) ** 2
    if dot > len_sq:
        return False

    return True

ltpPosTest = ((-320.00, -320.00), (-320.00, -160.00), (-320.00, 0.00), (-320.00, 160.00), (-320.00, 320.00), 
              (-160.00, -320.00), (-160.00, -160.00), (-160.00, 0.00), (-160.00, 160.00), (-160.00, 320.00), 
              (0.00, -320.00), (0.00, -160.00), (0.00, 0.00), (0.00, 160.00), (0.00, 320.00), 
              (160.00, -320.00), (160.00, -160.00), (160.00, 0.00), (160.00, 160.00), (160.00, 320.00), 
              (320.00, -320.00), (320.00, -160.00), (320.00, 0.00), (320.00, 160.00), (320.00, 320.00), 
              (480.00, -320.00), (480.00, -160.00), (480.00, 0.00), (480.00, 160.00), (480.00, 320.00))


ltpPosPylgon = ((677.19, -17.33), (527.29, 173.79), (280.10, 297.31), (-55.11, 369.39), (-232.25, 185.40), 
                (-454.19, 37.33), (-332.15, -164.76), (-98.27, -347.48), (254.93, -299.39), (531.82, -204.47))

for p in ltpPosTest:

    print("test pos " + str(p[0]/160) + ", " + str(p[1]/160+2))
    print("\t is in polygon ? " + str(is_point_in_polygon_v2(p, ltpPosPylgon)))

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: