Point in a poly?

Is there any built in methods for this, or does anyone have some full code for it please?

Currently i’ve got this which uses the Jordan curve theorem (ref):


    c=False
    tot=len(v)
    k=0
    j=tot-1
    while k<tot:
        if (
            ((v[k][1]>y) != (v[j][1]>y)) and 
            ( x &lt; (v[j][0]-v[k][0]) * (y-v[k]<i>[1]) / (v[j][1]-v[k][I][1]) + v[k][I][0]) ):
            c= not c
       </i>[/I][/I] j=k
        k+=1
    return c


x = 5 # 25
y = 5
verts=[(0,0),(0,10),(10,10),(10,0)]
print(pin(x, y, verts))

Yes, the test is a square, but goes to show it’s weaknesses…

I’m just doing an implementation of a ray casting method using the odd-even rule, and probably a winding number method… Any comments on speed, or built in c/c++ functions, etc would be nice.

Cheers!

And here’s solution 1 from here


def inpoly(x, y, v):
    tot=len(v)
    counter=0
    k=1
    xinters=0.0
    p1=[]
    p2=[]
    p1=v[0]
    while k&lt;=tot:
        p2=v[k%tot]
        if y &gt; min(p1[1],p2[1]):
            if y &lt;= max(p1[1],p2[1]):
                if x &lt;= max(p1[0],p2[0]):
                    if p1[1] != p2[1]:
                        xinters = (y-p1[1]) * (p2[0]-p1[0]) / (p2[1]-p1[1]) + p1[0]
                        if (p1[0] == p2[0]) or (x &lt;= xinters):
                            counter+=1
        p1 = p2
        k+=1
    
    if counter % 2 == 0:
        return False
    else:
        return True
        
x = 5
y = 5
v=[(0,0),(0,10),(10,10),(10,0)]
print(inpoly(x, y, v))