xxxxxxxxxx
// For the #WCCCHallenge "branching"
// Just a random walker in polar coordinates. Sometimes, it randomly branches into a new walker
let cw, ch
let agents
function setup() {
cw = windowWidth
ch = windowHeight
createCanvas(cw, ch)
background('#E2E2E2')
angleMode(DEGREES)
drawingContext.shadowOffsetX = 5
drawingContext.shadowOffsetY = 5
drawingContext.shadowBlur = 10
drawingContext.shadowColor = 'rgba(0,0,0,0.2)'
agents = []
for (let i = 0; i < 4; i++) {
agents.push({
r: 0,
a: 90 * i,
dir: 1,
color: random(0, 160)
})
}
}
function draw() {
translate(cw/2, ch/2)
noFill()
for (let agent of agents) {
stroke(agent.color)
strokeWeight(map(agent.r, 0, min(cw, ch), 0.5, 4))
// If agent is out of bounds, mark it for deletion
let x = agent.r * cos(agent.a)
let y = agent.r * sin(agent.a)
if (x > cw/2 || x < -cw/2 || y > ch/2 || y < -ch/2) {
agent.die = true
continue
}
let d = map(agent.r, 0, min(cw, ch), 5, 30)
if (agent.target) {
let newAngle = agent.a + random(1, 2) * agent.dir
// Ensure that the arcs are clockwise
if (agent.dir === 1) {
arc(0, 0, agent.r*2, agent.r*2, agent.a, newAngle)
} else {
arc(0, 0, agent.r*2, agent.r*2, newAngle, agent.a)
}
// If we've reached the target angle, reset it
if (abs(newAngle - agent.target) < 1) {
agent.target = null
}
agent.a = newAngle
} else {
if (random() < 0.2) {
// Assign a new target and direction
agent.dir = random([-1, 1])
agent.target = agent.a + random(10, 30) * agent.dir
} else {
agent.r += d * random(0.8, 1.5)
let nx = agent.r * cos(agent.a)
let ny = agent.r * sin(agent.a)
line(x, y, nx, ny)
}
}
// Finally, there's a random chance that the agent will split into a new agent
if (random() < 0.01) {
agents.push({
r: agent.r,
a: agent.a,
dir: agent.dir,
color: random(0, 160)
})
}
}
// Remove agents that are out of bounds
agents = agents.filter(agent => !agent.die)
}
function mousePressed() {
background('#E2E2E2')
agents = []
for (let i = 0; i < 4; i++) {
agents.push({
r: 0,
a: 90 * i,
dir: 1,
color: random(0, 160)
})
}
}