Code hoofdstuk 4

Pythagoras

terug

function setup(){ createCanvas(800, 500); } function draw() { background(255); stroke(0); noFill(); noLoop(); side=100; translate((width-side)/2,height-40); scale(1.0,-1.0); rect(0,0,side,side); translate(0,side); pyth(side); } function pyth(side){ if(side>2){ //draw two rectangles pythagoraswisely push(); //first rectangle let newside=sin(QUARTER_PI)*side; rotate(QUARTER_PI); rect(0,0,newside,newside); translate(0,newside); pyth(newside); //recursive call of the function pop(); //second rectangle translate(side,0); scale(-1,1); rotate(QUARTER_PI); rect(0,0,newside,newside); translate(0,newside); pyth(newside); //recursive call of the function side=newside; } }

terug