Published
Edited
Nov 27, 2020
3 forks
15 stars
Insert cell
Insert cell
Insert cell
Insert cell
segLength = 70
Insert cell
deltaAngle = 0.3
Insert cell
N = 3500
Insert cell
svg`<svg width="${w}" height="${h}">

${(() => {
const margin = 10
const walls = [
new Segment(PVector(-margin, -margin), PVector(w + margin, -margin)),
new Segment(PVector(w + margin, -margin), PVector(w + margin, h + margin)),
new Segment(PVector(w + margin, h + margin), PVector(-margin, h + margin)),
new Segment(PVector(-margin, h + margin), PVector(-margin, -margin))
]
const segments = []
// add a first segment
const A = PVector(w, h)
const s = new Segment(A, PVector.add(A, PVector.fromAngle(random(-PI, -3*PI/4)).setMag(segLength)))
segments.push(s)
while (segments.length < N) {
// const index = randInt(segments.length)
const index = expRand(0, 1, 0.5) * (segments.length-1) | 0
const prevSeg = segments[index]
const A = prevSeg.pointAt(random())
// const l = segLength
// const l = randInt(10, segLength)
const l = map(segments.length, 0, N, segLength, 10)
const currSeg = new Segment(A, PVector.add(A, PVector.sub(prevSeg.b, prevSeg.a).setMag(l).rotateBy(random(-deltaAngle, deltaAngle))))

const isPtOut = a => (a.x < 0 || a.x > w) && (a.y < 0 || a.y > h);
const isSegOut = s => isPtOut(s.a) && isPtOut(s.b)
if(!isSegOut(currSeg) && !walls.find(w => segSegIntersection(w, currSeg)) && !segments.filter((seg, i) => i !== index).find(s => segSegIntersection(s, currSeg))) {
segments.push(currSeg)
}
}
return segments.map((seg, i) => `<line
opacity="0.5"
stroke="black"
stroke="hsl(${map(seg.getLength(), 10, segLength, 0, 360)},50%,50%)"
x1="${seg.a.x}" y1="${seg.a.y}"
x2="${seg.b.x}" y2="${seg.b.y}"
/>`).join('\n')
})()}
</svg>`
Insert cell
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more