Skip to content

Instantly share code, notes, and snippets.

@Marileal
Last active October 14, 2020 02:03
Show Gist options
  • Save Marileal/ad101b92b89ff4ca931d5d20c1d2b860 to your computer and use it in GitHub Desktop.
Save Marileal/ad101b92b89ff4ca931d5d20c1d2b860 to your computer and use it in GitHub Desktop.
spaceColonization_myMouse
// based on the code of the coding challenge
// by Daniel Shiffman, accessed on:
// https://youtu.be/kKT0v3qhIQY
// needs to be run on Processing (https://processing.org/)
Tree tree;
float min_dist = 5;
float max_dist = 800;
boolean arboriza = false;
PImage imagem;
void setup() {
size(1000, 500);
background(255);
rectMode(CENTER);
strokeJoin(ROUND);
}
void draw() {
if (mousePressed){
pushMatrix();
translate(mouseX, mouseY);
rotate(QUARTER_PI);
fill(0);
rect(0, 0, 5, 80);
popMatrix();
}
if (keyPressed && key == 'i'){
tree = new Tree();
arboriza = true;
}
if (arboriza == true) {
background(-1);
tree.show();
tree.grow();
}
if (arboriza == true && frameCount % 10 == 0) saveFrame("raiz######.png");
if (keyPressed && key == 's') saveFrame("raizInstante######.png");
}
class Tree {
ArrayList<Branch> branches = new ArrayList<Branch>();
ArrayList<Leaf> leaves = new ArrayList<Leaf>();
Tree() {
loadPixels();
//caso inicie a posição aqui, tem que passá-la como argumento para o construtor
for (int xindex = 0; xindex < width; xindex ++){
for (int yindex = 0; yindex < height; yindex ++) {
int sorteio = int((random(1) * 10));
color corPixel = pixels[xindex + yindex * width];
if (brightness(corPixel) < 128 && sorteio % 5 == 0) {
leaves.add(new Leaf(xindex, yindex));
}
}
}
Branch root = new Branch(new PVector(width/2, height), new PVector(0, -1));
branches.add(root);
Branch current = new Branch(root);
while (!closeEnough(current)) {
Branch trunk = new Branch(current);
branches.add(trunk);
current = trunk;
}
}
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();
}
for (Branch b : branches) {
if (b.parent != null) {
strokeWeight(1);
stroke(0);
line(b.pos.x, b.pos.y, b.parent.pos.x, b.parent.pos.y);
}
}
}
}
class Leaf {
PVector pos;
boolean reached = false;
//PImage img;
Leaf(int posx, int posy) {
pos = new PVector(posx, posy);
}
void reached() {
reached = true;
}
void show() {
fill(255);
noStroke();
ellipse(pos.x, pos.y, 5, 5);
}
}
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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment