"""格子 / Lattice — Truchet tiles with quarter-circle arcs."""
import numpy as np
from PIL import Image, ImageDraw
from _common import W, H, C, rgb, paper, cli


def render(seed, cell=120):
    rng = np.random.default_rng(seed)
    bg, col = C["kon"], C["fuji"]
    img = Image.new("RGB", (W, H), rgb(bg)); d = ImageDraw.Draw(img)
    for gy in range(0, H + cell, cell):
        for gx in range(0, W + cell, cell):
            h = cell / 2
            if rng.random() < 0.5:
                d.arc([gx - h, gy - h, gx + h, gy + h], 0, 90, fill=rgb(col), width=9)
                d.arc([gx + h, gy + h, gx + 3 * h, gy + 3 * h], 180, 270, fill=rgb(col), width=9)
            else:
                d.arc([gx + h, gy - h, gx + 3 * h, gy + h], 90, 180, fill=rgb(col), width=9)
                d.arc([gx - h, gy + h, gx + h, gy + 3 * h], 270, 360, fill=rgb(col), width=9)
    return paper(img, rng)


SEED = 53

if __name__ == "__main__":
    cli(render, SEED)
