Search code examples
javaimage-processingrecursionprocessingfractals

Drawing a Fractal Tree by Processing


I want to draw a fractal tree in processing, but I don't know how to make it. And my idea is to draw a binary tree. But the tree must have more branches. And I don't know how to code it. Please help me, thanks.Now i have drawn a 2D Tree, but my boss ask me to change it to 3D.And this is my code,and i don't know how to make it.And i'm a fresh man in Processing programing.Please help me.

void tree(float treeLength, float strokeWeight, int currentTreeStep)
{
    if (currentTreeStep < maxTreeSteps) {

        if(currentTreeStep >= 8 && currentTreeStep <=10)
            stroke(#558351); //set color light green
        else{
            if(currentTreeStep >10 && currentTreeStep <=20)
                stroke(#199B0E); //set hard green        
            else{
                stroke(#311919);//set branch color
            }
        }
        strokeCap(PROJECT);
        strokeWeight(strokeWeight);
        line(0, 0, 0, -treeLength);  
        translate(0, -treeLength);

        strokeWeight *= 0.5;
        treeLength *= 0.75;

        if (treeLength > 1) {
            pushMatrix();
            rotate(radians(branchAngle));
            tree(treeLength, strokeWeight, currentTreeStep + 1);
            popMatrix();

            pushMatrix();
            rotate(-radians(branchAngle));
            tree(treeLength, strokeWeight, currentTreeStep + 1);
            popMatrix();
        }  
    }
}

Solution

  • Generally it's not as intuitive as one might to move from 2D(trigonometry mostly) to 3D(linear algebra: vector and matrix multiplication).

    Have a look at this example from one of the courses I did.

    enter image description here

    Hopefully you'll also see the 3D matrix multiplication part in action as well