Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Inside Block by UFO665
def coeffs(x1, y1, x2, y2):
a = y2 - y1
b = x1 - x2
c = - y1 * b - x1 * a
return a, b, c
def distance(x1, y1, x2, y2):
return ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5
def isCrossing(x1, y1, x2, y2, x3, y3, x4, y4):
a1, b1, c1 = coeffs(x1, y1, x2, y2)
a2, b2, c2 = coeffs(x3, y3, x4, y4)
det = float(a1 * b2 - a2 * b1)
ix = -(c1 * b2 - c2 * b1) / det
iy = -(a1 * c2 - a2 * c1) / det
if distance(x2, y2, x1, y1) < distance(x2, y2, ix, iy):
return False
elif min(x3, x4) <= ix <= max(x3, x4) and min(y3, y4) <= iy <= max(y3, y4):
return True
else:
return False
def isPointOnLine(x1, y1, x2, y2, x, y):
length = distance(x1, y1, x2, y2)
if distance(x1, y1, x, y) > length or distance(x2, y2, x, y) > length:
return False
a, b, c = coeffs(x1, y1, x2, y2)
return a * x + b * y + c == 0
def is_inside(polygon, point):
if point in polygon:
return True
iCrosses = 0
iCrossesInPoint = 0
x, y = point
for i, pt in enumerate(polygon):
x1, y1 = pt
x2, y2 = polygon[(i + 1) % len(polygon)]
if isPointOnLine(x1, y1, x2, y2, x, y):
return True
bCrossing = isCrossing(x, y, 99, 101, x1, y1, x2, y2)
if bCrossing:
iCrosses += 1
elif bCrossing is None:
iCrossesInPoint += 1
return (iCrosses + iCrossesInPoint / 2) % 2
Feb. 20, 2016