# Plot a sin wave and its (approximate) derivative,
# using a function to do the drawing.

import ecs10graphics07 as gr
import math

width = 600
height = 200

def drawList(inList):
    for point in inList:   
        # Draw the circle using the graphics library
        gr.circle(point[0],point[1],4,filled=True)



# Define graphics window
gr.begin_graphics(width,height,gr.Color.pink,"The Wave")

# Pick up purple crayon
gr.set_Color(gr.Color.purple)

pointList = []
angle = 0
angleList = []

while angle < 2*math.pi:

    # Save list of angles
    angleList = angleList + [angle]
    
    # Define a point for that value
    # x corresponds to angle, scaled so 2*pi is the width of window
    x = width * angle/(2*math.pi)

    # y corresponds to height, scaled so that 1 = height of window
    y = (height-20)/2 * math.sin(angle) + height/2
    
    point = [x,y]
    pointList = pointList + [point]

    # Increase the angle by a little bit each time
    angle = angle+2*math.pi/50

# Call the drawing function
drawList(pointList)

pointList2 = [] 
lastAngle = None
for angle in angleList:
    if lastAngle != None:

        # Get difference of this angle the last angle
        dangle = angle - lastAngle
        dsin = math.sin(angle) - math.sin(lastAngle)
        der = dsin/dangle
    

        # Define a point for that value
        # x corresponds to angle, scaled so 2*pi is the width of window
        x = width * angle/(2*math.pi)

        # y corresponds to height, scaled so that 1 = height of window
        y = (height-20)/2 * der + height/2        

        point = [x,y]
        pointList2 = pointList2 + [point]
    lastAngle = angle

# Pick up green crayon
gr.set_Color(gr.Color.green)

drawList(pointList2)


# Pause to allow graphics window to process commands, window
# move, window kill, all that stuff. 
# After every pause, alive will be True if the graphics window
# is still there, False after it gets killed. 
alive = True
while alive:
    alive = gr.sleep(.05)

gr.end_graphics()
