// Homework 8 // Claire Castleman // Section D // ccastlem@andrew.cmu.edu //int [ ] yValues = { 40, 20, 60, 70, 40, 40, 30, 80, 50, 120, 40, 80, 80, 10, 50, 70, 90 }; int [ ] yValues = { 40, 20, 60, 70, 10, 10, 10, 80, 50, 100 }; //int [ ] yValues = { 40, 20, 60, 70, 40, 10 }; PFont f1; void setup( ) { size( 500, 300 ); background( 0, 0, 255); f1 = loadFont("f1.vlw"); textFont( f1 ); fill( 255 ); DrawGraph(); Label(); GreenFlags(); RedFlags(); YellowCircle(); Stats(); } void DrawGraph() { strokeWeight(3); stroke(255, 0, 0); line(0, height - 50, width, height - 50); stroke(255); for(int i = 0; i < yValues.length; i++) { ellipse( ( ((width)*(i+1)) / ( yValues.length + 1 ) ), height - yValues[i] - 50, 1, 1); } strokeWeight(1); stroke(0, 255, 0); for(int j = 0; j < yValues.length - 1; j++) { line( ( ((width)*(j+1)) / ( yValues.length + 1 ) ), height - yValues[j] - 50, ( ((width)*(j+2)) / ( yValues.length + 1 ) ) , height - yValues[j+1] - 50); } } void Label() { fill(255); for( int k = 0; k < yValues.length; k++) { text( yValues[k], (((width)*(k+1)) / ( yValues.length + 1 )) - 6, height - yValues[k] - 25); } } void GreenFlags() { int highIndex = 0; for(int m = 1; m < yValues.length; m ++) { if (yValues[m] > yValues[highIndex]) { highIndex = m; } } int x1 = ((width)*(highIndex+1)) / ( yValues.length + 1 ), y1 = height - yValues[highIndex] - 65; stroke(0, 255, 0); fill(0, 255, 0); triangle( x1, y1, x1+10, y1-5, x1, y1-10); line(x1, y1, x1, y1+15); } void RedFlags() { int lowIndex = 0; for(int m = 1; m < yValues.length; m ++) { if (yValues[m] < yValues[lowIndex]) { lowIndex = m; } } for(int n = 0; n < yValues.length; n++) { if (yValues[lowIndex] == yValues[n]) { int x2 = ((width)*(n+1)) / ( yValues.length + 1 ), y2 = height - yValues[n] - 65; stroke(255, 0, 0); fill(255, 0 , 0); triangle( x2, y2, x2+10, y2-5, x2, y2-10); line(x2, y2, x2, y2+15); } } } void YellowCircle() { for(int p = 1; p < yValues.length -1; p++) { if(yValues[p] == yValues[p+1]) { int x3 = ((width)*(p+1)) / ( yValues.length + 1 ); int x4 = ((width)*(p+2)) / ( yValues.length + 1 ); int y3 = height - yValues[p] - 60; stroke(255, 255, 0); fill(255, 255, 0); ellipse((x3+x4)/2, y3, 8, 8); } } } void Stats() { int totalRise = 0; int totalFall = 0; for( int q = 1; q < yValues.length; q++) { if (yValues[q] > yValues[q-1]) { totalRise+= (yValues[q] - yValues[q-1]); } } for( int q = 1; q < yValues.length; q++) { if (yValues[q] < yValues[q-1]) { totalFall+= (yValues[q-1] - yValues[q]); } } fill(255); text("The total rise of the graph is " + totalRise + ".", 15, height - 15); text("The total fall of the graph is " + totalFall + ".", (width/2) + 30, height - 15); }