#! python3
# 03_llm_attractor_annotated.py -- VIS 146B week 5, part 2
#
# A plausible chatbot answer to the prompt below, with the problems marked "# CHECK:".
# It is a teaching example written in the style of LLM output; it was not produced by any one model.
# It runs without errors and looks right at a glance. That is the point: every problem is something you
# only find by checking.
#
# THE PROMPT (this kind of wording invites these mistakes: it never says which way the radius should go,
# asks for the pattern "edge to edge", and doesn't mention units):
#   "Write a Rhino Python script that makes a 200 x 200 laser-cut screen. Fill it edge to edge with a grid
#    of circular holes. The hole radius should go from 1 to 6 based on the distance to an attractor point at
#    (60, 140). Keep at least 2 mm between neighboring holes. Use rhinoscriptsyntax."
#
# VERIFICATION CHECKLIST (do these before you trust any generated geometry)
#   1. Read it: can you say what every line does? Mark the ones you can't.
#   2. Units: what unit is your document in (Properties > Units)? Does the script check?
#   3. Direction: are holes big or small near the attractor? Is that what you meant?
#   4. Bridges: measure the thinnest wood between two neighbouring holes (Distance command, or SelDup and
#      check gaps). Is it really >= 2 mm everywhere, at the LARGEST radius?
#   5. Edges: do any holes cross or touch the sheet outline? (A laser will cut them open.)
#   6. Duplicates: run SelDup. Any duplicate curves will be cut (or plotted) twice.
#   7. Extremes: move the attractor to a corner and to outside the sheet; try radius 0.5 to 10. Still OK?
#   8. Count: how many holes did you get, and how many did you expect?
#
# HOW TO RUN: ScriptEditor > open this file > Run, in an empty document. Then go through the checklist.

import rhinoscriptsyntax as rs

# Attractor screen parameters
width = 200
height = 200
min_radius = 1
max_radius = 6
min_gap = 2
attractor = (60, 140, 0)

# CHECK: units. "200" is only 200 mm if the document is in millimeters. Nothing here checks
#   (rs.UnitSystem() == 2 would). In an inch document this makes a screen 200 INCHES wide.

# Spacing between hole centers, leaving room for the minimum gap
spacing = max_radius * 2 + min_gap / 2
# CHECK: minimum bridge. Two neighbours at max_radius would leave spacing - 2 * max_radius of wood:
#   13 - 12 = 1 mm, not 2. The comment says "leaving room for the minimum gap", but min_gap is halved
#   and nothing caps the radius. Measure the two biggest neighbouring holes (near the attractor): the
#   thinnest bridge in this run is about 1.4 mm.


def frange(start, stop, step):
    """Range function that works with floats."""
    values = []
    x = start
    while x <= stop:
        values.append(x)
        x += step
    return values


# Generate grid points across the full sheet
points = []
for x in frange(0, width, spacing):
    for y in frange(0, height, spacing):
        points.append((x, y, 0))
# CHECK: edges. The grid starts at 0, so the first row and column of holes are centred ON the sheet
#   outline: half of each hole hangs off the sheet. "Edge to edge" was taken literally. The last row
#   stops at 195 (15 x 13), so its bigger holes cross the far edge too: 63 of the 256 holes cross the
#   outline. Is that a hole, or a notch the laser cuts into the edge?

# Find the farthest point so we can normalize distances
max_dist = max(rs.Distance(p, attractor) for p in points)

# Create circles with radius based on distance from the attractor
for p in points:
    d = rs.Distance(p, attractor)
    # Closer points get larger holes
    radius = max_radius - (d / max_dist) * (max_radius - min_radius)
    rs.AddCircle(p, radius)
# CHECK: direction. The prompt never said which way, so the model picked "closer = larger". Week 4's
#   screen does the opposite. Neither is wrong, but did YOU decide, or did the model?
# CHECK: range. Dividing by max_dist (not remapping from the nearest to the farthest distance) means the
#   biggest hole is a little smaller than max_radius unless a grid point lands exactly on the attractor
#   (here 5.85, not 6). Print the smallest and largest radius you actually got.

# Add holes along the border so the pattern reaches the edges of the sheet
for x in frange(0, width, spacing):
    rs.AddCircle((x, 0, 0), max_radius - (rs.Distance((x, 0, 0), attractor) / max_dist) * (max_radius - min_radius))
    rs.AddCircle((0, x, 0), max_radius - (rs.Distance((0, x, 0), attractor) / max_dist) * (max_radius - min_radius))
# CHECK: duplicates. The main loop already made every hole on the bottom row (y = 0) and the left column
#   (x = 0). This adds them again, exactly on top, and the corner hole three times. Run SelDup: the
#   laser would cut each of these twice (scorching, or a dropped part).

# Draw the outline of the screen
rs.AddRectangle(rs.WorldXYPlane(), width, height)

print("Created screen with {} holes".format(len(points)))
# CHECK: count. This reports len(points), not the number of circles actually added: the duplicates
#   above are not counted: it prints 256, but 288 circles were added. Select all circles (SelCrv) and compare.
