A farmer fences triangular pens in a field and wants to know which pen each sheep is in.
Write pen_of(pens, sheep) -> list[int]. Each pen is three integer corners ((x1, y1), (x2, y2), (x3, y3)), listed clockwise or counter-clockwise, with non-zero area; pens may overlap. For each sheep (x, y), return the smallest index of a pen containing it, or -1. A sheep on a fence (edge or corner) is inside.
pens = [((0, 0), (4, 0), (0, 4)), # pen 0
((10, 0), (4, 3), (10, 6))] # pen 1, listed clockwise
pen_of(pens, [(1, 1), (2, 2), (8, 3), (4, 3), (5, 5)])
# [0, 0, 1, 1, -1]
# (2, 2) is on pen 0's slanted fence; (4, 3) is a corner of pen 1
Up to 200 pens and 200 sheep; coordinates within ±10^9. Integers only: no floats, division or angles.
Show hint
the sign of cross(a, b, c) = (b.x - a.x)·(c.y - a.y) - (b.y - a.y)·(c.x - a.x) says which side of a→b the point c is on. A point is in a triangle (boundary included) unless it's strictly left of one edge and strictly right of another.