"""依頼233：止まった所と、229 の寄せ方で寄せる形の真上の図（左が −x・上が +z〔車いすの来る側〕）。1 m = 260 px
使い方: python3 fig233.py <出力 png>"""
import sys, math
from PIL import Image, ImageDraw, ImageFont
S = 260; RAIL = 0.84; WALL = 0.90
W, H = int(2.4 * S) * 2 + 60, int(6.9 * S)
img = Image.new('RGB', (W, H), (235, 232, 224)); d = ImageDraw.Draw(img)
font = ImageFont.truetype('/System/Library/Fonts/ヒラギノ角ゴシック W6.ttc', 24)
def panel(ox, title, shift):
    X = lambda x: int(ox + 1.2 * S + x * S); Z = lambda z: int(H - 0.9 * S - z * S)
    d.rectangle([X(-WALL), Z(5.6), X(WALL), Z(-0.6)], fill=(219, 214, 201))
    for sx in (-1, 1):
        d.line([X(sx * RAIL), Z(5.6), X(sx * RAIL), Z(-0.6)], fill=(160, 120, 70), width=3)
    d.text((X(-WALL), Z(5.6) - 64), title, fill=(0, 0, 0), font=font)
    def rect(cx, cz, yaw, x0, x1, f, b, col, w=3):
        s, c = math.sin(yaw), math.cos(yaw)
        P = [(x0, f), (x1, f), (x1, -b), (x0, -b)]
        d.polygon([(X(cx + lx * c + lu * s), Z(cz - lu * c + lx * s)) for lx, lu in P], outline=col, width=w)
    # ハコブくん（止まった所 x −0.4643・前の端 z 0.382）
    rect(-0.4643, 0.0, 0.0, -0.33, 0.33, 0.380, 0.382, (20, 140, 100))   # ハコブくんは +z を向く（後ろ 0.380・前 0.382）
    d.text((X(-0.46) - 50, Z(0.0) - 12), 'ハコブ', fill=(20, 140, 100), font=font)
    G = 2.3; zf = 0.382 + G; root = zf + 0.64; oz = root + 0.594
    if not shift:
        rect(0.0, root, 0.0, -0.326, 0.326, 0.64, 0.43, (30, 50, 110)); d.text((X(0) - 32, Z(root) - 12), '車いす', fill=(30, 50, 110), font=font)
        rect(0.0, oz, 0.0, -0.238, 0.238, 0.157, 0.157, (40, 83, 171)); d.text((X(0) - 22, Z(oz) - 12), '大城', fill=(40, 83, 171), font=font)
        rect(0.538, oz, -math.pi / 2, -0.238, 0.238, 0.15, 0.14, (200, 40, 40)); d.text((X(0.538) - 22, Z(oz) + 40), '寺島', fill=(200, 40, 40), font=font)
        d.line([X(-RAIL), Z(root), X(-0.326), Z(root)], fill=(200, 0, 0), width=2); d.text((X(-RAIL) + 4, Z(root) + 4), '0.514', fill=(200, 0, 0), font=font)
        d.line([X(0.326), Z(root), X(RAIL), Z(root)], fill=(200, 0, 0), width=2); d.text((X(0.36), Z(root) + 4), '0.514', fill=(200, 0, 0), font=font)
        d.line([X(-0.9), Z(zf), X(0.9), Z(zf)], fill=(120, 120, 120), width=1)
        d.text((X(-0.15), Z((zf + 0.382) / 2) - 12), f'{G:.2f} m', fill=(80, 80, 80), font=font)
        d.text((X(-WALL), Z(-0.6) + 8), '要る幅 0.842 m（どちら側も狭い）', fill=(0, 0, 0), font=font)
    else:
        # 229 の寄せ方：向き 8°・向きの変わる所 0.6 m・x 0 → +0.42
        a = 0.6; tm = math.radians(8); ds = 0.01
        def lat(p):
            x = 0; s = 0; L = 2 * a + p
            while s < L:
                th = tm * (min(1, s / a) if s < a else 1 if s < a + p else max(0, (L - s) / a)); x += math.tan(th) * ds; s += ds
            return x
        lo, hi = 0, 20
        for _ in range(30):
            m = (lo + hi) / 2
            if lat(m) < 0.42: lo = m
            else: hi = m
        p = hi; L = 2 * a + p; x = 0; s = 0; k = 0
        while s <= L + 1e-9:
            th = tm * (min(1, s / a) if s < a else 1 if s < a + p else max(0, (L - s) / a))
            az = root + 0.15 - s; rx = x + 0.15 * math.sin(th); rz = az - 0.15 * math.cos(th)
            if k % 60 == 0 or s + ds > L:
                last = s + ds > L
                col = (30, 50, 110) if last or k == 0 else (150, 160, 190)
                rect(rx, rz, -th, -0.326, 0.326, 0.64, 0.43, col, 3 if last or k == 0 else 1)
                rect(rx - 0.594 * math.sin(th), rz + 0.594 * math.cos(th), -th, -0.238, 0.238, 0.157, 0.157, (40, 83, 171) if last or k == 0 else (160, 175, 210), 3 if last or k == 0 else 1)
            x += math.tan(th) * ds; s += ds; k += 1
        rect(0.538, oz, -math.pi / 2, -0.238, 0.238, 0.15, 0.14, (200, 40, 40)); d.text((X(0.538) - 22, Z(oz) + 40), '寺島', fill=(200, 40, 40), font=font)
        d.text((X(-WALL), Z(-0.6) + 8), f'前へ {L:.2f} m 進んで x +0.42（向き 8°まで）', fill=(0, 0, 0), font=font)
        # その場で寄せられる所（前へ 2.15 m まで）
        d.line([X(-0.9), Z(0.382 + 0.15), X(0.9), Z(0.382 + 0.15)], fill=(200, 0, 0), width=2)
        d.text((X(-0.25), Z(0.382 + 0.15) + 4), 'ハコブくんの前 0.15 m', fill=(200, 0, 0), font=font)
panel(0, '止まった所（両横の幅）', False)
panel(int(2.4 * S) + 60, '229 の寄せ方で x +0.42 へ', True)
img.save(sys.argv[1])
