# This program is part of a set of animations and demonstrations that encourage students
# to pose vaguely mathematical questions. This particular program draws a spiral whose
# radius is proportional to t^(1/10), as t slowly increases from 0. Hopefully this prompts
# questions related to functions, rates, or limits, such as "does it grow forever," "is it
# still growing," etc.

# History:
#
#   August 2017 -- Created by Doug Baldwin from a previous Pyglet test program.
#
#   July 2016 -- Pyglet test created by Doug Baldwin.




import pyglet
from pyglet.gl import *
from math import sin, cos




# This list collects points along the spiral; every time the program has to update its
# display it draws lines connecting these points. Each point is a 2-tuple containing an
# x and a y coordinate. The spiral is a parametric curve, defined by the equations
#   x(t) = t^(1/10) cos(t)
#   y(t) = t^(1/10) sin(t)
# The t parameter slowly increases from 0 as the program runs. As t increases, I add more
# points to the spiral.

spiral = []
t = 0

def growSpiral( dt ) :						# The callback function that grows the spiral
	global t, spiral
	radius = t ** ( 1.0 / 10.0 )
	spiral.append( ( radius * cos(t), radius * sin(t) ) )
	t += 0.04


# Create the window.

mainWindow = pyglet.window.Window( width = 512, height = 512 )


# Give the window a display callback

@mainWindow.event
def on_draw() :

	glClearColor( 0.8, 0.8, 0.8, 1.0 )
	glClear( GL_COLOR_BUFFER_BIT )
	glMatrixMode( GL_PROJECTION )
	glLoadIdentity()
	glOrtho( -2, 2, -2, 2, -1, 1 )
	glMatrixMode( GL_MODELVIEW )
	glLoadIdentity()
	
	glColor3f( 1, 0, 0 )
	glLineWidth( 2.0 )
		
	glBegin( GL_LINE_STRIP )
	for pt in spiral :
		glVertex2f( pt[0], pt[1] )
	glEnd()


# Schedule the function that increases t and adds points to the spiral to run periodically.

pyglet.clock.schedule_interval( growSpiral, 0.15 )


# Run until the user closes the window.

pyglet.app.run()