> show canvas only <


/* built with Studio Sketchpad: 
 *   https://sketchpad.cc
 * 
 * observe the evolution of this sketch: 
 *   https://studio.sketchpad.cc/sp/pad/view/ro.wUZ-b4Pvkuu/rev.1712
 * 
 * authors: 
 *   Sebastian Mellor
 *   
 *   

 * license (unless otherwise specified): 
 *   creative commons attribution-share alike 3.0 license.
 *   https://creativecommons.org/licenses/by-sa/3.0/ 
 */ 



// This sketch builds on a prior work, "Untitled Sketch", created by Sebastian Mellor
// http://sketchpad.cc/sp/pad/view/ro.Da67I3ov4U1/rev.72

int imagePadding = 10;

// Pressing Control-Shift-R will render this sketch.

void setup(){
    size(600, 600);
    background(255);
    fill(64);
    stroke(64);
    noLoop();
}

void draw(){
    
    tree([5,2,3], true);
    //tree([5,2,3], false);
}

void tree(int[] branches, bool rotateSteps){
    
    println("We have " + branches.length() + " branches.");
    println("Lengths: " + branches + ".");
    
    // determine branch lengths
    int avail = width - 2*imagePadding;
    double branchLength = avail/(branches.length()+1)/2;
    //println(branchLength);
    
    int oldprod = 1;
    int prod = 1;
    double antiRotate = 0;
    double antiRotate_prev = 0;
    for (int i=0; i<branches.length(); i++){
        
        antiRotate_prev = antiRotate;
        antiRotate = antiRotate - (branches[i]-1)/2 * 2*PI/prod/branches[i];
        
        for (int j=0; j<prod; j++){
            for (int k=0; k<branches[i]; k++){
                
                int out = j*branches[i]+k;
                double a1 = j*2*PI/prod;
                double a2 = out*2*PI/prod/branches[i];
                if (rotateSteps) {
                    a1 = a1 + antiRotate_prev;
                    a2 = a2 + antiRotate;
                }
                radialLine(i*branchLength, a1, (i+1)*branchLength, a2);
            }
        }
        prod = prod * branches[i];
    }
}

void radialLine(double r1, double a1, double r2, double a2){
    
    double x1 = width/2  + cos(a1)*r1;
    double y1 = height/2 + sin(a1)*r1;
    double x2 = width/2  + cos(a2)*r2;
    double y2 = height/2 + sin(a2)*r2;
    
    line(x1, y1, x2, y2);
    fill(255);
    ellipse(x1,y1,3,3);
    ellipse(x2,y2,3,3);
    
    //println([r1,a1,r2,a2]);
}