# -*- coding: utf-8 -*-
"""후보 부재 영역을 2D로 그려 눈으로 확인."""
import json, math, sys, numpy as np
import matplotlib; matplotlib.use('Agg')
import matplotlib.pyplot as plt
SCR = r'C:\Users\user\AppData\Local\Temp\claude\E-----Claude--20260728-claude-code-2-1-219\8defc145-8ae5-48d0-9058-355f3d9ae8ed\scratchpad'
X0, Y0, X1, Y1 = [float(v) for v in sys.argv[1:5]]
pad = float(sys.argv[5]) if len(sys.argv) > 5 else 100
X0-=pad; Y0-=pad; X1+=pad; Y1+=pad
d2 = json.load(open(SCR+r'\index2.json', encoding='utf-8'))
d1 = json.load(open(SCR+r'\index.json', encoding='utf-8'))
fig, ax = plt.subplots(figsize=(11, 9))
def inw(x, y): return X0 <= x <= X1 and Y0 <= y <= Y1
n = 0
for l in d2['lines']:
    if inw(l[0], l[1]) or inw(l[2], l[3]):
        ax.plot([l[0], l[2]], [l[1], l[3]], 'k-', lw=0.8); n += 1
for a in d2['arcs']:
    if inw(a[0], a[1]):
        s, e = a[3], a[4]
        if e < s: e += 360
        t = np.radians(np.linspace(s, e, 40))
        ax.plot(a[0]+a[2]*np.cos(t), a[1]+a[2]*np.sin(t), 'b-', lw=0.8); n += 1
for c in d1['circles']:
    if inw(c[2], c[3]):
        ax.add_patch(plt.Circle((c[2], c[3]), c[4], fill=False, color='r', lw=0.8))
for t in d1['texts']:
    if inw(t[2], t[3]):
        ax.text(t[2], t[3], t[4][:28], fontsize=5, color='g')
ax.set_aspect('equal'); ax.set_xlim(X0, X1); ax.set_ylim(Y0, Y1)
ax.set_title('region %.0f,%.0f - %.0f,%.0f  (%d segs)' % (X0, Y0, X1, Y1, n))
out = SCR + r'\region_%.0f_%.0f.png' % (X0, Y0)
plt.savefig(out, dpi=150, bbox_inches='tight'); print(out, n)
