"""Small planar operations shared by the runtime and Blender, without native deps.""" import math def contains(polygon, point): x,y=point;inside=False for a,b in zip(polygon,polygon[1:]+polygon[:1]): if (a[1]>y)!=(b[1]>y) and x<(b[0]-a[0])*(y-a[1])/(b[1]-a[1])+a[0]:inside=not inside return inside def area(polygon): return abs(sum(a[0]*b[1]-b[0]*a[1] for a,b in zip(polygon,polygon[1:]+polygon[:1])))/2 def clip_rectangle(polygon, bounds): """Clip a simple room contour to an axis-aligned tile; never fill a column.""" points=[list(p) for p in polygon] for axis,edge,sign in [(0,bounds[0],1),(0,bounds[2],-1),(1,bounds[1],1),(1,bounds[3],-1)]: output=[] for a,b in zip(points,points[1:]+points[:1]): ia=(a[axis]-edge)*sign>=-1e-9;ib=(b[axis]-edge)*sign>=-1e-9 if ia:output.append(a) if ia!=ib: t=(edge-a[axis])/(b[axis]-a[axis]);output.append([a[i]+t*(b[i]-a[i]) for i in (0,1)]) points=output if not points:break return points def segments(polygon): for a,b in zip(polygon,polygon[1:]+polygon[:1]): dx,dy=b[0]-a[0],b[1]-a[1];length=math.hypot(dx,dy) if length<1e-8:continue yield a,b,length,[-dy/length,dx/length]