Skip to content

Instantly share code, notes, and snippets.

@pLabarta
Created April 18, 2021 15:29
Show Gist options
  • Save pLabarta/ae4b4a41d1dd4ff6f6ee5ef250f7f7de to your computer and use it in GitHub Desktop.
Save pLabarta/ae4b4a41d1dd4ff6f6ee5ef250f7f7de to your computer and use it in GitHub Desktop.
Actividad clase 3
float grow = 0.1;
float angle = (0.3 * 90) * (3.1415 / 180);
color treeColor = color(140, 170, 50);
float branchLength;
void setup () {
size(1000, 1000);
frameRate(40);
}
void draw() {
background(230, 230, 170);
// Hacer crecer el arbol
grow = grow + 0.005;
// Si es muy grande, se reinicia
if (grow > 1) {
grow = 0.05;
}
Tree arbol1 = new Tree(250, height, 200, treeColor, angle);
arbol1.drawTree(grow);
Tree arbol2 = new Tree(500, 750, 200, treeColor, angle);
arbol2.drawTree(grow);
Tree arbol3 = new Tree(750, height, 200, treeColor, angle);
arbol3.drawTree(grow);
}
class Tree {
float h ;
float w ;
float posX;
float posY ;
color colorRamas;
float angulo;
Tree (float posicionX, float posicionY, float ramasLargo, color ramasColor, float ramasAngulo) {
h = ramasLargo;
w = ramasLargo / 10;
posX = posicionX;
posY = posicionY;
colorRamas = ramasColor;
angulo = ramasAngulo;
}
public void drawTree(float grow) {
push();
// Dibujar rama principal
strokeWeight(w * grow);
translate(posX, posY);
stroke(colorRamas);
line(0, 0, 0, -h * grow);
// Se posiciona al final de la rama principal
translate(0, -h * grow);
// Dibujar las ramas
branch(h * grow, w * grow);
pop();
}
}
void branch (float altura, float ancho) {
// La rama primero achica los parametros que recibe
float h = altura * 0.7;
float w = ancho * 0.7;
strokeWeight(w);
// Solo se dibuja si tiene un largo minimo
if (h > 5) {
// Guarda la posicion en la que esta
push();
// Rota la rama para la derecha
rotate(angle);
// Dibuja la rama
line(0, 0, 0, -h);
// Se posiciona al final de la rama
translate(0, -h);
// Se llama a si misma para dibujar las ramitas
branch(h, w);
// Recupera la posicion guardada
pop();
// Guarda la posicion en la que esta
push();
// Rota la rama para la izquierda
rotate(-angle);
// Dibuja la rama
line(0, 0, 0, -h);
// Se posiciona al final de la rama
translate(0, -h);
// Se llama a si misma para dibujar las ramitas
branch(h, w);
// Recupera la posicion guardada
pop();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment