Shuffling things around

This commit is contained in:
arne 2024-07-20 23:55:42 +02:00
commit 07c61fed6a

View file

@ -3,7 +3,7 @@ import { vec2, type Vec2 } from "@thi.ng/vectors"
import { $canvas } from "@thi.ng/rdom-canvas"; import { $canvas } from "@thi.ng/rdom-canvas";
import { reactive, sync } from "@thi.ng/rstream"; import { reactive, sync } from "@thi.ng/rstream";
import { $compile, $input, $inputNum } from '@thi.ng/rdom' import { $compile, $input, $inputNum } from '@thi.ng/rdom'
import { cycle, comp, range, iterator, map, mapcat, reverse, zip } from "@thi.ng/transducers" import { cycle, comp, range, iterator, map, mapcat, reverse, zip, filter } from "@thi.ng/transducers"
// initial settings // initial settings
@ -50,10 +50,10 @@ const secondaryColor = getComputedStyle(document.documentElement).getPropertyVal
const stitches = (seed: number, maxCoord: number) => const stitches = (seed: number, maxCoord: number) =>
iterator( iterator(
mapcat((cell) => (cell + seed) % 2 === 0 // start at the beginning or skip it comp(
? [vec2(cell, cell + 1)] filter((cell) => (cell + seed) % 2 === 0), // start at the beginning or skip it
: null) map((cell) => vec2(cell, cell + 1))
, ),
range(maxCoord) range(maxCoord)
) )
@ -62,18 +62,19 @@ const yLineGen = (coord: number, [p1, p2]: Vec2) => line([coord, p1], [coord, p2
const generateLines = (maxCoordOnAxis: number, maxCoordPerpendicular: number, seeds: Iterable<number>, lineGen: (coord: number, l: Vec2) => Line) => const generateLines = (maxCoordOnAxis: number, maxCoordPerpendicular: number, seeds: Iterable<number>, lineGen: (coord: number, l: Vec2) => Line) =>
iterator( iterator(
mapcat(([coord, seed]) => { comp(
map(([coord, seed]) => [coord, stitches(seed, maxCoordPerpendicular)] as const),
// optimization for quicker drawing: when we're in an odd row / col, // optimization for quicker drawing: when we're in an odd row / col,
// reverse drawing direction // reverse drawing direction
const onEvenCoord = coord % 2 === 0 mapcat(([coord, stitches]) =>
return iterator( map(
comp( l => lineGen(coord, l),
map(l => lineGen(coord, l)), coord % 2 === 0
map(l => onEvenCoord ? l : flip(l)) ? stitches
), : reverse(map(([p1, p2]) => vec2(p2, p1), stitches))
onEvenCoord ? stitches(seed, maxCoordPerpendicular) : reverse(stitches(seed, maxCoordPerpendicular))
) )
}), )
),
zip(range(maxCoordOnAxis + 1), seeds) zip(range(maxCoordOnAxis + 1), seeds)
) )