# An execution exercise after Sol LeWitt, Wall Drawing #118 (1971):
# "On a wall surface, any continuous stretch of wall, using a hard pencil, place fifty points at random.
#  The points should be evenly distributed over the area of the wall.
#  All of the points should be connected by straight lines."
# Drafter's reading of "at random" + "evenly distributed": Poisson-disk (dart throwing) —
# each point lands at random, but no two closer than a minimum distance.
# Usage: python3 wd118.py <mode: poisson|uniform> <seed> <out.svg>
import math, random, sys

mode, seed, out = sys.argv[1], int(sys.argv[2]), sys.argv[3]
W, H, PAD, N = 1200, 600, 30, 50          # a wall, wider than tall
rng = random.Random(seed)

def uniform():
    return [(rng.uniform(PAD, W - PAD), rng.uniform(PAD, H - PAD)) for _ in range(N)]

def poisson():
    r = 0.75 * math.sqrt((W - 2 * PAD) * (H - 2 * PAD) / N)   # min spacing, fraction of the "fair share" cell
    while True:
        pts, tries = [], 0
        while len(pts) < N and tries < 20000:
            tries += 1
            p = (rng.uniform(PAD, W - PAD), rng.uniform(PAD, H - PAD))
            if all(math.dist(p, q) >= r for q in pts):
                pts.append(p)
        if len(pts) == N:
            return pts
        r *= 0.97                                              # wall too crowded: relax, throw again

pts = poisson() if mode == "poisson" else uniform()
lines = [(a, b) for i, a in enumerate(pts) for b in pts[i + 1:]]
svg = [f'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 {W} {H}" width="{W}" height="{H}">',
       f'<rect width="{W}" height="{H}" fill="#F6F3EC"/>',
       '<g stroke="#2b2b2b" stroke-width="0.35" stroke-opacity="0.55" stroke-linecap="round">']
svg += [f'<line x1="{a[0]:.1f}" y1="{a[1]:.1f}" x2="{b[0]:.1f}" y2="{b[1]:.1f}"/>' for a, b in lines]
svg += ['</g>', '<g fill="#2b2b2b">'] + [f'<circle cx="{x:.1f}" cy="{y:.1f}" r="1.6"/>' for x, y in pts] + ['</g></svg>']
open(out, "w").write("\n".join(svg))
print(mode, seed, "points", len(pts), "lines", len(lines))
