################################ tgraphlibx.py ############################
# A simple library for making xy graphs
# for an example of how to use it, look at cubic_tgraphlib.py
# The "x" version has an added feature:  It optionally creates an xgraph compatible file
# with the graph.

import turtle

#Old way of finding the smallest element.  Not used any more.
#def smallest_element(values):
#  smallest=1.0E99
#  for i in range(0,len(values)):
#    if values[i] < smallest:
#      smallest = values[i]
#  return smallest    

#def biggest_element(values):
#  biggest=1.0E-99
#  for i in range(0,len(values)):
#    if values[i] > biggest:
#      biggest = values[i]
#  return biggest    

def graph_xy(x_values,y_values):
  print "Making a x-y graph..."
  t = turtle.Pen()                # "Pen" must start with a capital P!
  t.speed(0)                      # 0 is fastest
  t.hideturtle()
  window_max_x = t.window_width()/2.1
  window_max_y = t.window_height()/2.1
  #print "window_max_x",window_max_x
  #print "window_max_y",window_max_y
  biggest_x=max(x_values)          #New way to find maximum.  Old way:  biggest_x=biggest_element(x_values)
  biggest_y=max(y_values)          #Old way: biggest_y=biggest_element(y_values)
  print "Biggest x value=",biggest_x
  print "Biggest y value=",biggest_y
  smallest_x=min(x_values)         #Old way: smallest_x=smallest_element(x_values)
  smallest_y=min(y_values)         #Old way: smallest_y=smallest_element(y_values)
  print "Smallest x value=",smallest_x
  print "Smallest y value=",smallest_y
  t.penup()
  for i in range(0,len(x_values)):
    x=1.0*(-smallest_x+x_values[i])/(biggest_x-smallest_x)  #Make x be tween 0 and 1
    #print "norm x=",x
    y=1.0*(-smallest_y+y_values[i])/(biggest_y-smallest_y)  #Make y be tween 0 and 1
    #print "norm y=",y
    x=-window_max_x+(x*2.0*window_max_x)  #Make x have the full window width range
    y=-window_max_y+(y*2.0*window_max_y)  #Make x have the full window height range
    t.goto(x,y)
    if i==0:          #You want to draw after the turtle is at the first point location
      t.pendown()
  # Draw a box
  t.penup()
  t.goto(-window_max_x,-window_max_y)
  t.pendown()
  t.goto(-window_max_x,window_max_y)
  t.goto(window_max_x,window_max_y)
  t.goto(window_max_x,-window_max_y)
  t.goto(-window_max_x,-window_max_y)  
  
  # Draw x and y axes
  # Draw the X axis
  t.penup()
  y=1.0*(-smallest_y+0)/(biggest_y-smallest_y)  #Make y be tween 0 and 1
  y=-window_max_y+(y*2.0*window_max_y)  #Make y have the full window width range
  t.goto(-window_max_x,y)
  t.pendown()
  t.goto(window_max_x,y)      #X axis
  t.write("x",align="right",font=("Arial", 18, "normal"))

  # Draw the Y axis
  t.penup()
  x=1.0*(-smallest_x+0)/(biggest_x-smallest_x)  #Make x be tween 0 and 1
  x=-window_max_x+(x*2.0*window_max_x)  #Make x have the full window width range
  t.goto(x,-window_max_y)
  t.pendown()
  t.goto(x,window_max_y)      #Y axis
  t.penup()
  t.goto(x+window_max_x*0.025,0.92*window_max_y)
  t.write("y",align="left",font=("Arial", 18, "normal"))
  file_name = raw_input('''Enter a file name ending with ".dat" to make a xgraph or Press Enter to finish: ''')
  
  # Make the xgraph file
  if file_name != "":
    title = raw_input("Enter the title of the graph. (Or just press Enter): ")
    data_file=open(file_name,"w")
    if title != "":
      data_file.write("Title =")
      data_file.write(title)
      data_file.write("\n")
    data_file.write("thickness = 2\n")
    data_file.write("title_x = x\n")
    data_file.write("title_y = y\n")
    for i in range(0,len(x_values)):
      data_file.write(str(x_values[i])+" "+str(y_values[i])+"\n")
    data_file.close()
