Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Inside Block by Moff
from collections import namedtuple
Point = namedtuple('Point', ['x', 'y'])
def intersect(a1, a2, b1, b2):
def area(a, b, c):
return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x)
def intersect_single(a, b, c, d):
return max(min(a, b), min(c, d)) < max(max(a, b), max(c, d))
return intersect_single(a1.x, a2.x, b1.x, b2.x) and \
intersect_single(a1.y, a2.y, b1.y, b2.y) and \
area(a1, a2, b1) * area(a1, a2, b2) <= 0 and \
area(b1, b2, a1) * area(b1, b2, a2) <= 0
def trace(poly, p):
return sum(1 for i in range(1, len(poly)) if
intersect(poly[i - 1], poly[i], p, Point(100000, p.y)))
def is_boundary_segment(t, p1, p2):
return abs((p2.y - p1.y) * (p2.x - t.x) - (p2.y - t.y) * (p2.x - p1.x)) < 1E-3 and \
min(p1.x, p2.x) <= t.x <= max(p1.x, p2.x) and min(p1.y, p2.y) <= t.y <= max(p1.y, p2.y)
def is_boundary(poly, p):
return any(is_boundary_segment(p, poly[i-1], poly[i]) for i in range(len(poly)))
def is_inside(poly_points, p):
"""even-odd rule"""
poly = [Point(*row) for row in poly_points]
p = Point(*p)
return is_boundary(poly, p) or trace(poly, p) & 1
Aug. 4, 2015
Comments: