#!/usr/bin/env python3 """GXM21 conservative tile-range property regression (no visual deletions). Tests random affine camera matrices and translated/mirrored/sheared sprites. For every original tile whose projected rectangle can intersect a pixel of the target, optimized index bounds must retain that tile. Separately checks affine AABB identity vs 4-corner projection for arbitrary signed rectangles. """ import math import random rng = random.Random(20261013) EPS = 1e-3 def bbox(m, x, y, w, h): px = [m[0] * a + m[2] * b + m[4] for a, b in ((x, y), (x + w, y), (x, y + h), (x + w, y + h))] py = [m[1] * a + m[3] * b + m[5] for a, b in ((x, y), (x + w, y), (x, y + h), (x + w, y + h))] return min(px), min(py), max(px), max(py) def bbox_fast(m, x, y, w, h): ox = m[0] * x + m[2] * y + m[4] oy = m[1] * x + m[3] * y + m[5] ax, bx = m[0] * w, m[2] * h ay, by = m[1] * w, m[3] * h return (ox + min(0, ax) + min(0, bx), oy + min(0, ay) + min(0, by), ox + max(0, ax) + max(0, bx), oy + max(0, ay) + max(0, by)) def intersects_view(m, x, y, w, h, width, height): local=((x,y),(x+w,y),(x+w,y+h),(x,y+h)) quad=[(m[0]*a+m[2]*b+m[4], m[1]*a+m[3]*b+m[5]) for a,b in local] viewport=[(-2,-2),(width+2,-2),(width+2,height+2),(-2,height+2)] # Convex polygon SAT (projected bounding boxes alone are not enough: # a sheared or rotated offscreen rect can have an overlapping AABB). axes=[(1,0),(0,1)] for i in (0,1): ax=quad[(i+1)%4][0]-quad[i][0] ay=quad[(i+1)%4][1]-quad[i][1] axes.append((-ay,ax)) for ax,ay in axes: q=[ax*p[0]+ay*p[1] for p in quad] v=[ax*p[0]+ay*p[1] for p in viewport] if max(q)