-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
62 lines (55 loc) · 1.43 KB
/
sketch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
////////////////////////////////////////////////////////
// Reference: https://inconvergent.net/generative/
// https://openprocessing.org/sketch/871349
////////////////////////////////////////////////////////
let nodes = []
const r = 7
setup = () => {
createCanvas(windowWidth, windowHeight)
fill('lightgrey')
noStroke()
nodes.length = 0
for (let i = 3; i--;) {
const r = random(TWO_PI)
nodes[i] = createVector(cos(r) * 20 + width / 2, sin(r) * 5 + height / 2)
}
}
draw = () => {
background('white')
splits()
reject()
beginShape()
for (const p of nodes) curveVertex(p.x, p.y)
endShape(CLOSE)
}
reject = () => {
for (const p of nodes) {
force = createVector(0)
for (const q of nodes) {
if (p === q) continue;
const d = distance(p, q)
if (d < 2 * r) {
delta = p5.Vector.sub(p, q)
force.add(delta.mult((2 * r - d) * .025))
}
}
p.add(force.limit(1))
p.x = constrain(p.x, 0, width)
p.y = constrain(p.y, 0, height)
}
}
distance = (u, v) => sqrt((u.x - v.x) ** 2 + (u.y - v.y) ** 2)
splits = () => {
let next = []
for (let i = 0; i < nodes.length; i++) {
current = nodes[i]
next.push(current)
neighbor = nodes[(i + 1) % nodes.length]
const d = distance(current, neighbor)
if (d > 2 * r && nodes.length < 1000) {
next.push(p5.Vector.add(current, neighbor).mult(1 / 2))
}
}
nodes = next
}
mouseClicked = () => setup()