CS 115 Lab 5, Part C: Compare consecutive points

[Back to lab instructions]


Introduction

In order for us to find the peaks and the valleys of this graph, we first will have to determine if the consecutive points in the sequence are increasing or decreasing. Once we have this information, we can find out if the graph is turning. That is, suppose we have the following three consecutive y-values:

20
30
15

When we see y=20 followed by y=30, we know that the y-coordinate has increased. With that information, when we see y=15, we can tell that the graph is turning. That is, the second point is a valley. You will learn more about that later.

For now, after having seen y=20 followed by y=30, we want to print a statement to indicate that the graph is increasing. When we see y=15, we want to print a statement to specify that the graph is decreasing.

We will base the "increasing" or "decreasing" on the original Cartesian coordinates, not the ones we used to draw the graph.


Instructions

  1. Create a new Python source code file named lab05c.py:
    """
    Program: CS 115 Lab 5c
    Author: Your name
    Description: This program draws a graph and determines whether
       consecutive points are increasing or decreasing.
    """
    from graphics import *
    
    
    def main():
        window_height = 600
        window = GraphWin('Graph', 800, window_height)
    
        # Open the input file and read the number of points
        pointsfile = open("points-test.txt", "r")
        num_points = int(pointsfile.readline())
    
        x = 20
        first_y = int(pointsfile.readline())   # get the first y-coordinate
        first_point = Point(x, window_height - first_y)
    
        for i in range(1, num_points):   # we already have the first point, so, start with 1.
            # Read the next point and update x
            second_y = int(pointsfile.readline())
            x += 10
            second_point = Point(x, window_height - second_y)
    
            ###### Print first_y and second_y
    
            ###### Complete this if-statement
            if second_y is larger than first_y:
                the graph is increasing
            else: 
                the graph is decreasing
    
            ##### Copy this code from Part B
            draw the line between first_point and second_point
            draw a circle centered at first_point
    
            # second_point becomes the first point of the next line
            first_y = second_y
            first_point = second_point
    
        ###### Copy this code from Part B
        draw a circle centered at first_point
        
        window.getMouse()
        window.close()
    
    
    main()
    
  2. Complete this code so that it produces the following output:
    y of first point = 420, y of second point = 280
    decreasing
    y of first point = 280, y of second point = 230
    decreasing
    y of first point = 230, y of second point = 330
    increasing
    y of first point = 330, y of second point = 230
    decreasing
    y of first point = 230, y of second point = 140
    decreasing
    y of first point = 140, y of second point = 190
    increasing
    y of first point = 190, y of second point = 580
    increasing
    y of first point = 580, y of second point = 210
    decreasing
    y of first point = 210, y of second point = 580
    increasing
    y of first point = 580, y of second point = 500
    decreasing
    y of first point = 500, y of second point = 350
    decreasing
    y of first point = 350, y of second point = 460
    increasing
    y of first point = 460, y of second point = 550
    increasing
    y of first point = 550, y of second point = 420
    decreasing
    y of first point = 420, y of second point = 510
    increasing
    y of first point = 510, y of second point = 360
    decreasing
    y of first point = 360, y of second point = 330
    decreasing
    
  3. Demo your solution for a lab instructor, and then continue to Part D.