Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Inside Block by kurosawa4434
def is_inside(polygon, point):
def cross(p1, p2):
x1, y1 = p1
x2, y2 = p2
# horizontal line
if y1 - y2 == 0:
if y1 == point[1]:
if min(x1, x2) <= point[0] <= max(x1, x2):
return 1, True
return 0, False
# vertical line
if x1 - x2 == 0:
if min(y1, y2) <= point[1] <= max(y1, y2):
if point[0] <= max(x1, x2):
return 1, point[0] == max(x1, x2)
return 0, False
# diagonal line
a = (y1 - y2) / (x1 - x2)
b = y1 - x1 * a
x = (point[1] - b) / a
if point[0] <= x:
if min(y1, y2) <= point[1] <= max(y1, y2):
return 1, point[0] == x or point[1] in (y1, y2)
return 0, False
# MAIN
cross_points = 0
for x in range(len(polygon)):
num, on_line = cross(polygon[x], polygon[x-1])
if on_line:
return True
cross_points += num
return cross_points % 2
July 17, 2016
Comments: