Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Inside Block by tokiojapan55
from typing import Tuple
def is_inside(polygon: Tuple[Tuple[int, int]], point: Tuple[int, int]) -> bool:
sign = lambda v: 1 if v>0 else -1 if v<0 else 0
lines = [(x1,y1,x2,y2) for (x1,y1),(x2,y2) in zip(polygon,polygon[1:]+polygon[:1])]
cross_x = list()
x,y = point
for (x1,y1,x2,y2),(xx1,yy1,xx2,yy2) in zip(lines, lines[1:]+lines[:1]):
if y2==y:
cross_x.append(x2)
if sign(y1-y)*sign(yy2-y)==1:
cross_x.append(x2)
elif sign(y1-y)*sign(y2-y)==-1:
cross_x.append((x1-x2)/(y1-y2)*(y-y1)+x1)
if cross_x:
for n,xx in enumerate(sorted(cross_x)):
if x==xx:
return True
if x
June 23, 2020