// Homework 6 // Amy Nichols // Section D // aenichol@andrew.cmu.edu PImage flower; PFont font1; void setup( ) { size( 600, 600 ); background( 255 ); println("Please press one of the following:"); println(" [w] to see the while loop demonstration. "); println(" [d] to see the do loop demonstration. "); println(" [f] to see the for loop demonstration. "); println(" [c] to clear the screen. "); flower = loadImage("flower.jpg"); font1 = loadFont("font1.vlw"); } void draw( ) { displayChoices( ); if (key == 'w' ) { showWhile( ); } else if ( key == 'd' ) { showDo( ); } else if ( key == 'f' ) { showFor( ); } else if ( key == 'c' ) { clearScreen( ); } else if (frameCount > 1 ) { displayError( ); } noLoop( ); } void keyPressed( ) { loop( ); } void displayChoices( ) { textFont( font1, 20); fill( 0 ); text( "Please press one of the following:", 10, 25 ); text( " [w] to see the while loop demonstration. ", 10, 45 ); text( " [d] to see the do loop demonstration. ", 10, 65 ); text( " [f] to see the for loop demonstration. ", 10, 85 ); text( " [c] to clear the screen. ", 10, 105 ); } void showWhile( ) { int i = 0; float a = 0.0; float inc = PI/8; while( i < width ) { text("A", i, (.3*height)+2.5*sin(a)*30.0, width*.1, width*.1); i = i + 10; a = a + inc; } textFont( font1, 30); fill( 255, 0, 0 ); text( "bonus!(sin)", width*.7, height*.10); } void showDo( ) { int j = 0; float a = 0.0; float inc = PI/5; do { image(flower, j, (.7*height)+cos(a)*80.0, width*.1, width*.1); j = j + 15; a = a + inc; } while( j < width ); textFont( font1, 30); fill( 255, 0, 0 ); text( "bonus!(cos)", width*.7, height*.15); } void showFor( ) { for( int k = 0 ; k < width ; k = k + 50 ) { drawInitials( k, k, 50, 50); } } void clearScreen( ) { background( 255); displayChoices( ); } void displayError( ) { textFont( font1, 20); fill( 255, 0, 0 ); text("You have pressed an incorrect key.", width/12, height*.6 ); } void drawInitials(float x, float y, float initialWidth, float initialHeight) { stroke(#FCC11C); strokeWeight(initialWidth/12); // made this elastic too! noFill(); // A curve beginShape(); curveVertex(x - initialWidth/12, y + initialHeight + initialHeight/10); curveVertex(x, y + initialHeight); curveVertex(x + initialWidth/6, y); curveVertex(x + initialWidth/3, y + initialHeight); curveVertex(x + .42*initialWidth, y + initialHeight + initialHeight/10); endShape(); // A "line" ellipse(x + initialWidth/6, y + .50*initialHeight, .17*initialWidth, .05*initialHeight); stroke(#980BD6); // E top curve beginShape(); curveVertex(x + .63*initialWidth, y + initialHeight + .30*initialHeight); curveVertex(x + .63*initialWidth, y + .25*initialHeight); curveVertex(x + .50*initialWidth, y); curveVertex(x + .38*initialWidth, y + .25*initialHeight); curveVertex(x + .50*initialWidth, y + .50*initialHeight); curveVertex(x + .58*initialWidth, y - .30*initialHeight); endShape(); // E bottom curve beginShape(); curveVertex(x + .63*initialWidth, y - .30*initialHeight); curveVertex(x + .63*initialWidth, y + .75*initialHeight); curveVertex(x + .50*initialWidth, y + initialHeight); curveVertex(x + .38*initialWidth, y + .75*initialHeight); curveVertex(x + .50*initialWidth, y + .50*initialHeight); curveVertex(x + .58*initialWidth, y + initialHeight + .30*initialHeight); endShape(); stroke(#E01B43); // N left curve beginShape(); curveVertex(x + .67*initialWidth, y - .10*initialHeight); curveVertex(x + .71*initialWidth, y); curveVertex(x + .75*initialWidth, y + .50*initialHeight); curveVertex(x + .71*initialWidth, y + initialHeight); curveVertex(x + .67*initialWidth, y + initialHeight + .10*initialHeight); endShape(); // N right curve beginShape(); curveVertex(x + initialWidth, y - .10*initialHeight); curveVertex(x + (initialWidth - .04*initialWidth), y); curveVertex(x + (initialWidth - .08*initialWidth), y + .5*initialHeight); curveVertex(x + (initialWidth - .04*initialWidth), y + initialHeight); curveVertex(x + initialWidth, y + initialHeight + .10*initialHeight); endShape(); // N connector line(x + .71*initialWidth, y, x + (initialWidth - .04*initialWidth), y + initialHeight); }