xxxxxxxxxx
123
let sketch = function(p) {
let number_of_trees = 49;
let trees = [];
let radius = 400;
let initial_boundary_size = 25;
let number_of_tries = 12;
let terminated = false;
let colors;
p.setup = function() {
p.createCanvas(1000,1000);
p.noStroke();
p.fill(255,100,90);
p.background("#25252500");
p.stroke(0);
p.strokeWeight(2);
colors = [
p.color("#FF6F5A"),
p.color("#89F5FD"),
p.color("#FAD283"),
p.color("#CE92EE"),
p.color("#B2FC82")
];
for (var i = 0; i < number_of_trees; i++) {
let tree = [
{
//pos: p.createVector(p.random(p.width), p.height / 2 + p.random(-150,150)),
pos: p.createVector((i % 7 + .5) * 100 + 150, (p.floor(i / 7) + .5) * 100 + 150),
//pos: p.createVector(p.randomGaussian(p.width/2, 150), p.randomGaussian(p.height/2, 150)),
parent: 0,
boundary: initial_boundary_size,
exhausted: false
}
];
trees.push(tree);
}
}
p.draw = function() {
if (!terminated) {
terminated = grow_all();
trees.forEach(display);
}
}
function grow_all () {
return trees.map(grow).every((x)=>x);
}
function grow (tree) {
//Breadth first
//for (var index = 0; index < tree.length; index++) {
//Depth first
for (var index = tree.length-1; index >= 0; index--) {
let current = tree[index];
if(!current.exhausted) {
let u = create_neighbour(current.pos, current.boundary);
for (var t = 0; t < number_of_tries; t++) {
let new_node = { pos: u, parent: index, boundary: current.boundary * 0.95, exhausted: current.boundary <= 3}
if (ok_position(new_node)) {
//if (p.random() < .3) current.exhausted = true;
tree.push(new_node);
return false;
}
u = create_neighbour(current.pos, current.boundary);
}
current.exhausted = true;
}
}
return true;
}
function display (tree, col) {
let last_index = tree.length - 1;
let v = tree[last_index];
let u = tree[v.parent];
p.stroke(colors[col % colors.length]);
p.strokeWeight(.5 + v.boundary / 4);
p.line(v.pos.x, v.pos.y, u.pos.x, u.pos.y);
}
function too_close_to_vertex (v,u) {
return p5.Vector.dist(v.pos,u.pos) < p.max(v.boundary,u.boundary);
}
function too_close_to_trees (new_node) {
return trees.some(function(tree) {
return tree.some(function(t) {
return too_close_to_vertex(new_node,t)})
});
}
function outside_radius (v) {
return p.dist(v.x, v.y, p.width / 2, p.height / 2) > radius;
}
function inside_canvas (v) {
return v.x > 0 && v.x < p.width && v.y > 0 && v.y < p.height;
}
function ok_position (node) {
return !too_close_to_trees(node) && inside_canvas(node.pos);
}
function create_neighbour (v, dist) {
let r = p.random(p.TWO_PI);
let x = v.x + (p.cos(r) * (dist + 1));
let y = v.y + (p.sin(r) * (dist + 1));
return p.createVector(x,y);
}
p.keyPressed = function () {
if (p.keyCode === 80) {
p.saveCanvas("roses", "jpeg");
}
}
}
new p5(sketch);