xxxxxxxxxx
// Setup canvas and variables
function setup() {
createCanvas(800, 600); // Canvas size
background(245); // Light background color
noLoop(); // Draw once
}
// Draw plants with branches
function draw() {
let numPlants = 10; // Number of plants
let plantSpacing = width / numPlants; // Space between plants
// Loop through each plant position
for (let i = 0; i < numPlants; i++) {
let x = plantSpacing * i + plantSpacing / 2; // X position of each plant
let y = height; // Start from the bottom of the canvas
drawPlant(x, y, -PI / 2, 100); // Draw each plant starting upwards
}
}
// Recursive function to draw a plant with branches
function drawPlant(x, y, angle, length) {
if (length < 5) return; // Stop recursion when length is small
// Calculate new endpoint for the branch
let xEnd = x + cos(angle) * length;
let yEnd = y + sin(angle) * length;
// Draw the main branch segment
stroke(0, 128, 0); // Green color for branches
strokeWeight(map(length, 5, 100, 1, 3)); // Thickness decreases with length
line(x, y, xEnd, yEnd); // Draw the branch
// Create two smaller branches from the endpoint
let newLength = length * 0.7; // Reduce length for smaller branches
let randomAngleOffset = PI / 6; // Angle variation for randomness
// Left branch
drawPlant(xEnd, yEnd, angle - random(randomAngleOffset), newLength);
// Right branch
drawPlant(xEnd, yEnd, angle + random(randomAngleOffset), newLength);
}