Created
December 24, 2019 14:41
-
-
Save Nekodigi/f0a4e5262ea9eb8f01c09dbdd49fd716 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Coding Rainbow | |
// Daniel Shiffman | |
// http://patreon.com/codingtrain | |
// Code for: https://youtu.be/JcopTKXt8L8 | |
int leavesCount = 10000; | |
float pointSize = 10; | |
float lineWeight = 4; | |
float max_dist = 100; | |
float min_dist = 10; | |
float b_size = 100/2; | |
Tree tree; | |
void setup(){ | |
fullScreen(); | |
tree = new Tree(); | |
} | |
void draw(){ | |
background(0); | |
strokeWeight(lineWeight); | |
if(mousePressed){ | |
PVector pos = new PVector(random(mouseX-b_size, mouseX+b_size), random(mouseY-b_size, mouseY+b_size)); | |
tree.leaves.add(new Leaf(pos)); | |
} | |
tree.show(); | |
tree.grow(); | |
} | |
void keyPressed(){ | |
if(key=='r'){ | |
tree.reset(); | |
} | |
} | |
class Branch { | |
Branch parent; | |
PVector pos; | |
PVector dir; | |
int count = 0; | |
PVector saveDir; | |
float len = 5; | |
Branch(PVector v, PVector d) { | |
parent = null; | |
pos = v.copy(); | |
dir = d.copy(); | |
saveDir = dir.copy(); | |
} | |
Branch(Branch p) { | |
parent = p; | |
pos = parent.next(); | |
dir = parent.dir.copy(); | |
saveDir = dir.copy(); | |
} | |
void reset() { | |
count = 0; | |
dir = saveDir.copy(); | |
} | |
PVector next() { | |
PVector v = PVector.mult(dir, len); | |
PVector next = PVector.add(pos, v); | |
return next; | |
} | |
} | |
class Leaf { | |
PVector pos; | |
boolean reached = false; | |
Leaf() { | |
pos = PVector.random2D(); | |
pos.mult(random(width/2)); | |
pos.x += width/2; | |
pos.y += height/2; | |
//pos = new PVector(random(10, width-10), random(10, height-40)); | |
} | |
Leaf(PVector pos) { | |
this.pos = pos; | |
//pos = new PVector(random(10, width-10), random(10, height-40)); | |
} | |
void reached() { | |
reached = true; | |
} | |
void show() { | |
fill(255); | |
noStroke(); | |
ellipse(pos.x, pos.y, pointSize, pointSize); | |
} | |
} | |
class Tree { | |
ArrayList<Branch> branches; | |
ArrayList<Leaf> leaves; | |
Tree() { | |
reset(); | |
} | |
boolean closeEnough(Branch b) { | |
for (Leaf l : leaves) { | |
float d = PVector.dist(b.pos, l.pos); | |
if (d < max_dist) { | |
return true; | |
} | |
} | |
return false; | |
} | |
void grow() { | |
for (Leaf l : leaves) { | |
Branch closest = null; | |
PVector closestDir = null; | |
float record = -1; | |
for (Branch b : branches) { | |
PVector dir = PVector.sub(l.pos, b.pos); | |
float d = dir.mag(); | |
if (d < min_dist) { | |
l.reached(); | |
closest = null; | |
break; | |
} else if (d > max_dist) { | |
} else if (closest == null || d < record) { | |
closest = b; | |
closestDir = dir; | |
record = d; | |
} | |
} | |
if (closest != null) { | |
closestDir.normalize(); | |
closest.dir.add(closestDir); | |
closest.count++; | |
} | |
} | |
for (int i = leaves.size()-1; i >= 0; i--) { | |
if (leaves.get(i).reached) { | |
leaves.remove(i); | |
} | |
} | |
for (int i = branches.size()-1; i >= 0; i--) { | |
Branch b = branches.get(i); | |
if (b.count > 0) { | |
b.dir.div(b.count); | |
b.dir.normalize(); | |
Branch newB = new Branch(b); | |
branches.add(newB); | |
b.reset(); | |
} | |
} | |
} | |
void show() { | |
for (Leaf l : leaves) { | |
l.show(); | |
} | |
int i = 0; | |
for (Branch b : branches) { | |
if (b.parent != null) { | |
stroke(255); | |
strokeWeight(map(i, 0, branches.size(), lineWeight, 0)); | |
line(b.pos.x, b.pos.y, b.parent.pos.x, b.parent.pos.y); | |
} | |
i++; | |
} | |
} | |
void reset(){ | |
branches = new ArrayList<Branch>(); | |
leaves = new ArrayList<Leaf>(); | |
Branch root = new Branch(new PVector(width/2, height), new PVector(0, -1)); | |
branches.add(root); | |
Branch current = new Branch(root); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment