xxxxxxxxxx
157
let leaves = [];
let count = 0;
let patterns = [];
let currentPatternIndex = 0;
function setup() {
createCanvas(600, 600);
let a = createVector(width / 2, height);
let b = createVector(width / 2, height - 100);
let root = new Branch(a, b);
patterns.push(new FractalTree(width / 2, height)); // Initialize with a FractalTree
}
function mousePressed() {
if (currentPatternIndex === 0) {
// Create a Korean Flower
let flower = new KoreanFlower(mouseX, mouseY);
patterns.push(flower);
} else if (currentPatternIndex === 1) {
// Create a Fractal Tree
let tree = new FractalTree(mouseX, mouseY);
patterns.push(tree);
}
}
function draw() {
background(51);
for (let i = 0; i < patterns.length; i++) {
patterns[i].display();
patterns[i].grow();
}
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 keyPressed() {
// Toggle between patterns when the spacebar is pressed
if (key === ' ') {
currentPatternIndex = (currentPatternIndex + 1) % 2;
}
}
// Korean Flower class
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);
}
grow() {
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 = []; // Change from branches to tree
let a = createVector(x, y);
let b = createVector(x, y - 100);
let root = new Branch(a, b);
this.tree.push(root);
}
grow() {
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.tree[i].finished = 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);
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);
}
}