xxxxxxxxxx
205
let flowers = [];
let song;
let cells;
let cellSize = 20;
let tree;
let leaves = [];
let count = 0;
class FractalTree {
constructor(x, y, length, angle, depth) {
this.start = createVector(x, y);
this.end = createVector(x, y - length);
this.angle = angle;
this.depth = depth;
this.finished = false;
}
branchA() {
let dir = p5.Vector.sub(this.end, this.start);
dir.rotate(this.angle);
dir.mult(0.67);
let newEnd = p5.Vector.add(this.end, dir);
let right = new FractalTree(this.end.x, this.end.y, dir.mag(), this.angle, this.depth - 1);
return right;
}
branchB() {
let dir = p5.Vector.sub(this.end, this.start);
dir.rotate(-this.angle);
dir.mult(0.67);
let newEnd = p5.Vector.add(this.end, dir);
let left = new FractalTree(this.end.x, this.end.y, dir.mag(), this.angle, this.depth - 1);
return left;
}
show() {
stroke(255);
line(this.start.x, this.start.y, this.end.x, this.end.y);
}
jitter() {
this.end.x += random(-1, 1);
this.end.y += random(-1, 1);
}
update() {
if (this.depth > 0) {
if (!this.finished) {
this.finished = true;
return [this.branchA(), this.branchB()];
}
}
return [];
}
}
function drawTree(branch) {
if (branch.depth > 0) {
branch.show();
branch.jitter();
let branches = [branch.branchA(), branch.branchB()];
for (let b of branches) {
drawTree(b);
}
}
}
function preload() {
// Load an audio file
song = loadSound('sampleaudio.mp3');
}
function setup() {
createCanvas(800, 600);
// Initialize flower array with fewer flowers
for (let i = 0; i < width / cellSize / 2; i++) {
flowers[i] = {
petals: floor(random(5, 15)),
size: random(5, 20),
position: createVector(i * cellSize * 2, -30),
color: color(255, random(100, 255), random(100, 200)),
speed: random(0.5, 1.5),
};
}
// Initialize cellular automaton
cells = Array(floor(width / cellSize)).fill(0);
// Initialize fractal tree
tree = new FractalTree(width / 2, height, 100, PI / 4, 8);
}
function mousePressed() {
if (count < 6) {
let newTree = [];
for (let i = tree.length - 1; i >= 0; i--) {
if (!tree[i].finished) {
let branchA = tree[i].branchA();
let branchB = tree[i].branchB();
newTree.push(branchA);
newTree.push(branchB);
}
tree[i].finished = true;
}
tree = newTree;
count++;
}
if (count === 6) {
for (let i = 0; i < tree.length; i++) {
if (!tree[i].finished) {
let leaf = tree[i].end.copy();
leaves.push(leaf);
}
}
}
}
function draw() {
background(255);
// Draw fractal tree
drawTree(tree);
// Draw flowers
for (let i = 0; i < flowers.length; i++) {
animateFlower(flowers[i]);
drawFlower(flowers[i]);
}
// Update cellular automaton rules when the mouse is pressed
if (mouseIsPressed) {
updateAutomaton();
updateFlowerProperties();
if (!song.isPlaying()) {
song.play();
}
} else {
// Pause audio when the mouse is not pressed
if (song.isPlaying()) {
song.pause();
}
}
// Draw falling leaves
for (let i = 0; i < leaves.length; i++) {
fill(255, 0, 100, 100);
noStroke();
ellipse(leaves[i].x, leaves[i].y, 8, 8);
leaves[i].y += random(0, 2);
}
}
function drawFlower(flower) {
fill(flower.color);
noStroke();
// Draw flower petals
for (let j = 0; j < flower.petals; j++) {
let angle = map(j, 0, flower.petals, 0, TWO_PI);
let x = flower.position.x + cos(angle) * flower.size;
let y = flower.position.y + sin(angle) * flower.size;
ellipse(x, y, flower.size, flower.size);
}
// Draw flower center
fill(255, 200, 0);
ellipse(flower.position.x, flower.position.y, flower.size * 0.5, flower.size * 0.5);
}
function animateFlower(flower) {
// Move the flower downwards
flower.position.y += flower.speed;
// Reset the flower if it goes below the canvas
if (flower.position.y > height + flower.size) {
flower.position.y = -30;
}
}
function updateAutomaton() {
let nextGen = [cells];
for (let i = 1; i < cells.length - 1; i++) {
// Apply custom rules for pattern evolution
if (cells[i] === 1 && random() < 0.1) {
// Allow living cells to die with a certain probability
nextGen[i] = 0;
} else if (cells[i] === 0 && random() < 0.05) {
// Create new living cells with a certain probability
nextGen[i] = 1;
}
}
cells = nextGen;
}
function updateFlowerProperties() {
// Update flower properties based on cellular automaton state
for (let i = 0; i < flowers.length; i++) {
flowers[i].petals = floor(map(cells[i], 0, 1, 5, 15));
flowers[i].color = color(255, random(100, 255), random(100, 200));
}
}