// ============================================================
//  CROSSINGS — SEGMENT INTERSECTIONS ON RANDOM CHORDS
//
//  Random chords span a circle; wherever two chords cross,
//  a small circle marks the intersection.  segisect finds
//  every crossing — the segment test means only real
//  crossings inside both chords are marked, not their
//  infinite extensions.
// ============================================================

// --- layout ---
let nChords = 14    // [4:24] number of chords
let radius  = 38    // [15:45] circle radius, mm
let stitch  = 1.8   // [1:0.1:4]
// --- markers ---
let dotR    = 1.0   // [0.4:0.2:3] crossing-circle radius, mm

seed 5
stitchlen stitch

// build random chords: two random points on the circle
let segs = []
for i = 0 to nChords - 1 [
  let a1 = random(360)
  let a2 = random(360)
  let p1 = [radius * sin(a1), radius * cos(a1)]
  let p2 = [radius * sin(a2), radius * cos(a2)]
  append(segs, [p1, p2])
]

// draw the chords
for s in segs [
  up  setpos(s[0])  down  setpos(s[1])
]
trim

// mark every crossing between different chords
color 2
for i = 0 to len(segs) - 2 [
  for j = i + 1 to len(segs) - 1 [
    let hit = segisect(segs[i][0], segs[i][1], segs[j][0], segs[j][1])
    if len(hit) > 0 [
      up  setpos(hit)  down
      circle dotR
      trim
    ]
  ]
]
