> show canvas only <


/* built with Studio Sketchpad: 
 *   https://sketchpad.cc
 * 
 * observe the evolution of this sketch: 
 *   https://studio.sketchpad.cc/sp/pad/view/ro.pSjK7sxId58/rev.0
 * 
 * authors: 
 *   (Unnamed author)
 *   Ari Bader-Natal

 * 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 Sarah & [unnamed author]
// http://sketchpad.cc/sp/pad/view/ro.vk5vWYf4rQf/rev.1699


// Pressing Control-R will render this sketch.

void setup() {  // this is run once.   
    
    // set the background color
    background(255);
    
    // canvas size (Integers only, please.)
    size(600, 600); 
      
    // smooth edges
    smooth();
    
    // limit the number of frames per second
    frameRate(30);
    
    // set the width of the line. 
    strokeWeight(2);
    
    // Don't loop the draw function
    noLoop();
} 

void draw() {
    
    int depth = 0;
    
    float triWidth = width - 20;
    // Go Pythagorus!
    float triHeight = sqrt( pow(triWidth, 2) - pow(triWidth/2, 2) );
    
    float leftX = 10;
    float leftY = height-10;
    
    float topX = width/2;
    float topY = leftY-triHeight;
    
    float rightX = width-10;
    float rightY = leftY;

    drawTriangles(topX, topY, leftX, leftY, rightX, rightY, 0);
}

void drawTriangles(float topX, float topY, float leftX, float leftY, float rightX, float rightY, float depth) {   

    // Only draw triangles if we're not yet at end depth
    if (depth < 7) {
        
        // set the color
        stroke(random(255), random(255), random(255), 200);
        
        // Draw the triangle         
        triangle(topX, topY, leftX, leftY, rightX, rightY);

        // Midpoint of the triangle's left side
        float leftMidX = topX - (topX - leftX)/2;
        float leftMidY = leftY - (leftY - topY)/2;
        
        // Midpoint of the triangle's right side
        float rightMidX = rightX - (rightX - topX)/2;
        float rightMidY = leftMidY;
        
        // Midpoint of the triangle's bottom
        float bottomMidX = topX;
        float bottomMidY = leftY;
        
        // Draw the top triangle
        drawTriangles(topX, topY, leftMidX, leftMidY, rightMidX, rightMidY, depth+1);
        
        // Draw the left triangle
        drawTriangles(leftMidX, leftMidY, leftX, leftY, bottomMidX, bottomMidY, depth+1);
        
        // Draw the right triangle
        drawTriangles(rightMidX, rightMidY, bottomMidX, bottomMidY, rightX, rightY, depth+1);
        
    }
}