xxxxxxxxxx
209
let leaves = [];
let patterns = [];
let currentPatternIndex = 0;
function setup() {
createCanvas(600, 600);
let a = createVector(width / 2, height);
let b = createVector(width / 2, height - 150);
let root = new Branch(a, b);
patterns.push(new FractalTree(width / 2, height));
// Generate some flowers
for (let i = 0; i < 7; i++) {
let flower = new KoreanFlower(random(width), random(height));
patterns.push(flower);
}
}
function mousePressed() {
if (currentPatternIndex === 0) {
let flower = new KoreanFlower(mouseX, mouseY);
patterns.push(flower);
} else if (currentPatternIndex === 1) {
let tree = new FractalTree(mouseX, mouseY);
patterns.push(tree);
}
}
function draw() {
background(0);
// Attract leaves towards the average position of flowers
let attractionForce = createVector(0, 0);
let flowerCount = 0;
for (let i = 0; i < patterns.length; i++) {
patterns[i].display();
patterns[i].grow();
if (patterns[i] instanceof KoreanFlower) {
attractionForce.add(createVector(patterns[i].x, patterns[i].y));
flowerCount++;
}
}
if (flowerCount > 0) {
attractionForce.div(flowerCount);
for (let i = 0; i < leaves.length; i++) {
let direction = p5.Vector.sub(attractionForce, leaves[i]);
direction.normalize();
direction.mult(0.5); // Adjust the attraction force as needed
leaves[i].add(direction);
fill(255, 0, 100, 100);
noStroke();
ellipse(leaves[i].x, leaves[i].y, 8, 8);
leaves[i].y -= random(0, 2); // Change to move leaves upward
}
}
}
function mousePressed() {
if (currentPatternIndex === 0) {
let flower = new KoreanFlower(mouseX, mouseY);
patterns.push(flower);
} else if (currentPatternIndex === 1) {
let tree = new FractalTree(mouseX, mouseY);
patterns.push(tree);
// Generate additional flowers around the tree
for (let i = 0; i < 10; i++) {
let flower = new KoreanFlower(tree.x + random(-50, 50));
patterns.push(flower);
}
}
}
// Rest of the code remains unchanged
class KoreanFlower {
constructor(x, y) {
this.x = x;
this.y = y;
this.size = 10;
this.outerColor = color(random(255), random(255), random(255), 150);
this.innerColor = color(random(255), random(255), random(255), 150);
this.centerColor = color(random(255), random(255), random(255), 150);
// Generate random size limits
this.minSize = random(5, 10); // Set your desired minimum size
this.maxSize = random(20, 50); // Set your desired maximum size
}
grow() {
if (this.size < this.maxSize) {
this.size += 1;
}
}
display() {
noStroke();
// Outer petals
fill(this.outerColor);
for (let i = 0; i < 6; i++) {
let angle = radians(60 * i);
let petalX = this.x + this.size * cos(angle);
let petalY = this.y + this.size * sin(angle);
ellipse(petalX, petalY, this.size * 1.5, this.size * 1.5);
}
// Inner petals
fill(this.innerColor);
for (let i = 0; i < 6; i++) {
let angle = radians(60 * i + 30);
let petalX = this.x + this.size * 0.7 * cos(angle);
let petalY = this.y + this.size * 0.7 * sin(angle);
ellipse(petalX, petalY, this.size, this.size);
}
// Flower center
fill(this.centerColor);
ellipse(this.x, this.y, this.size * 0.6, this.size * 0.6);
}
}
// Fractal Tree class
class FractalTree {
constructor(x, y) {
this.tree = [];
let a = createVector(x, y);
let b = createVector(x, y - 150);
let root = new Branch(a, b);
this.tree.push(root);
this.branchCount = 0;
this.branchLimit = 200; // Adjust the branch limit here for more leaves
this.leavesFallen = false;
}
grow() {
if (this.branchCount < this.branchLimit) {
for (let i = this.tree.length - 1; i >= 0; i--) {
if (!this.tree[i].finished) {
this.tree.push(this.tree[i].branchA());
this.tree.push(this.tree[i].branchB());
this.branchCount += 2; // Increment the counter for each new branch
}
this.tree[i].finished = true;
}
} else if (!this.leavesFallen) {
for (let i = 0; i < this.tree.length; i++) {
if (!this.tree[i].finished) {
let leaf = this.tree[i].end.copy();
leaves.push(leaf);
}
}
this.leavesFallen = true;
}
}
display() {
for (let i = 0; i < this.tree.length; i++) {
this.tree[i].show();
this.tree[i].jitter();
}
}
}
// Branch class (used by Fractal Tree)
class Branch {
constructor(begin, end) {
this.begin = begin;
this.end = end;
this.finished = false;
}
show() {
stroke(255);
strokeWeight(3); // Increase stroke weight
line(this.begin.x, this.begin.y, this.end.x, this.end.y);
}
branchA() {
let dir = p5.Vector.sub(this.end, this.begin);
dir.rotate(PI / 4);
dir.mult(0.67);
let newEnd = p5.Vector.add(this.end, dir);
let b = new Branch(this.end, newEnd);
return b;
}
branchB() {
let dir = p5.Vector.sub(this.end, this.begin);
dir.rotate(-PI / 6);
dir.mult(0.67);
let newEnd = p5.Vector.add(this.end, dir);
let b = new Branch(this.end, newEnd);
return b;
}
jitter() {
this.end.x += random(-1, 1);
this.end.y += random(-1, 1);
}
}