// Homework 8 // Name: chongho lee // Section: A // E-Mail: chonghol@andrew.cmu.edu // Global Variables 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 }; int horizontalScale; int highIndex = findHighIndex(); int lowIndex = findLowIndex(); int x; PFont f1; void setup( ) { size( 500, 300 ); background( 0, 0, 255); horizontalScale = width/yValues.length; f1 = loadFont("f1.vlw"); textFont( f1 ); fill( 255 ); //text( "Nothing is working yet.", width*.4, 50); translate(0, height*0.8); strokeWeight( 1 ); stroke(255, 0, 0); line(0, 0, width, 0); //println(yValues); // call your methods here noStroke(); drawPoints(); placeFonts(); drawLines(); //int highIndex = findHighIndex(); //println("The index with the hightest value is " + highIndex); //println("Highest Index value is " + yValues[highIndex]); //int lowIndex = findLowIndex(); //println("The index with the lowest value is " + lowIndex); //println("Lowest Index value is " + yValues[lowIndex]); drawFlag(); drawPlaneCircle(); int sumRise = findSumRise(); int sumFall = findSumFall(); fill (255); text( "Total Rise of the graphs is " + sumRise , width*0.05, height*0.15); text( "Total Fall of the graphs is " + sumFall , width*0.6, height*0.15); } // Define your methods here void drawPoints() { fill(255, 230, 3); rectMode(CENTER); x = 10; for (int i=0; i <= yValues.length-1; i++) { rect(x, -yValues[i], width*0.015, height*0.015); x = x + horizontalScale; } } void placeFonts() { fill(255); x = 10; for (int i=0; i <= yValues.length-1; i++) { text(yValues[i], x-width*0.01, -yValues[i]+height*0.05); x = x + horizontalScale; } } void drawLines() { stroke(255, 0, 0); x = 10; for (int i=0; i < yValues.length-1; i++) { line(x, -yValues[i], x + horizontalScale, -yValues[i+1]); x = x + horizontalScale; } } int findHighIndex() { int highIndex = 0; for (int i = 1; i < yValues.length; i++) { if ( yValues[i] > yValues[highIndex]) { highIndex = i; } } return highIndex; } int findLowIndex() { int lowIndex = 0; for (int i = 1; i < yValues.length; i++) { if ( yValues[i] < yValues[lowIndex]) { lowIndex = i; } } return lowIndex; } void drawFlag() { x = 10; for (int i = 0; i < yValues.length; i++) { if (yValues[i] == yValues[highIndex]) { stroke(0, 255, 0); line(horizontalScale*highIndex+x, -yValues[highIndex], horizontalScale*highIndex+x, -yValues[highIndex]-20); fill(0, 255, 0); triangle(horizontalScale*highIndex+x, -yValues[highIndex]-20, horizontalScale*highIndex+x, -yValues[highIndex]-40, horizontalScale*highIndex+x+10, -yValues[highIndex]-30); } if (yValues[i] == yValues[lowIndex]) { stroke(255, 0, 0); line(horizontalScale*lowIndex+x, -yValues[lowIndex], horizontalScale*lowIndex+x, -yValues[lowIndex]-20); fill(255, 0, 0); triangle(horizontalScale*lowIndex+x, -yValues[lowIndex]-20, horizontalScale*lowIndex+x, -yValues[lowIndex]-40, horizontalScale*lowIndex+x+10, -yValues[lowIndex]-30); } } } void drawPlaneCircle() { x = 10; noStroke(); for (int i = 0; i < yValues.length-1; i++) { if (yValues[i] == yValues[i+1]) { fill(255, 255, 0); ellipse(((horizontalScale*i)+x)+(horizontalScale/2), -yValues[i]-10, 20, 20); } } } int findSumRise() { int sumRise = 0; for (int i = 0; i < yValues.length-1; i++) { if ( yValues[i] < yValues[i+1]) { sumRise = sumRise + yValues[i+1]-yValues[i]; } } return sumRise; } int findSumFall() { int sumFall = 0; for (int i = 0; i < yValues.length-1; i++) { if ( yValues[i] > yValues[i+1]) { sumFall = sumFall + yValues[i]-yValues[i+1]; } } return sumFall; }