# -*- coding: cp1252 -*-
from Tkinter import *

import math

master = Tk()

def graf():
    #==Initiation of variables==
    timeMax=3000.0 #time for simulation in miliseconds (1/1000 second)
    timeCountMax=5000 #timeframe for each timemark on graf
    x1=50.0 #x start position
    y1=450.0 #y start position
    vinkel=float(vinkel1.get()) #vinkel for start
    vstart=float(vstart1.get()) #start hastighed
    timestep=0.001
    
    time=0.0 #should allways start at 0
    timeCount=0 #should allways start at 0
    x2=x1 # x2 is the previous value of x1
    y2=y1 # y2 is the precious value of y1
    
    #==Creation of drawing area==
    master = Tk()
    w = Canvas(master, width=900, height=500)
    w.pack()
    w.create_rectangle(0, 0, 900, 500, fill="#ffffff")
    
    #--1.Creating horisontal lines--
    w.create_line(0,450,900,450,fill="#00ff00") #floor
    
    w.create_line(0,50,900,50,fill="#00ffff")
    w.create_line(0,100,900,100,fill="#00ffff")
    w.create_line(0,150,900,150,fill="#00ffff")
    w.create_line(0,200,900,200,fill="#00ffff")
    w.create_line(0,250,900,250,fill="#00ffff")
    w.create_line(0,300,900,300,fill="#00ffff")
    w.create_line(0,350,900,350,fill="#00ffff")
    w.create_line(0,400,900,400,fill="#00ffff")
    
    #--2.Creating vertical lines--
    w.create_line(50,0,50,500,fill="#00ff00")
    w.create_line(100,0,100,500,fill="#00ffff")
    w.create_line(150,0,150,500,fill="#00ffff")
    w.create_line(200,0,200,500,fill="#00ffff")
    w.create_line(250,0,250,500,fill="#00ffff")
    w.create_line(300,0,300,500,fill="#00ffff")
    w.create_line(350,0,350,500,fill="#00ffff")
    w.create_line(400,0,400,500,fill="#00ffff")
    w.create_line(450,0,450,500,fill="#00ffff")
    w.create_line(500,0,500,500,fill="#00ffff")
    w.create_line(550,0,550,500,fill="#00ffff")
    w.create_line(600,0,600,500,fill="#00ffff")
    w.create_line(650,0,650,500,fill="#00ffff")
    w.create_line(700,0,700,500,fill="#00ffff")
    w.create_line(750,0,750,500,fill="#00ffff")
    w.create_line(800,0,800,500,fill="#00ffff")
    w.create_line(850,0,850,500,fill="#00ffff")
    
    #--3.Creating horisontal axis labels--
    w.create_text(50,460,anchor="n",text="0")
    w.create_text(100,460,anchor="n",text="50")
    w.create_text(150,460,anchor="n",text="100")
    w.create_text(200,460,anchor="n",text="150")
    w.create_text(250,460,anchor="n",text="200")
    w.create_text(300,460,anchor="n",text="250")
    w.create_text(350,460,anchor="n",text="300")
    w.create_text(400,460,anchor="n",text="350")
    w.create_text(450,460,anchor="n",text="400")
    w.create_text(500,460,anchor="n",text="450")
    w.create_text(550,460,anchor="n",text="500")
    w.create_text(600,460,anchor="n",text="550")
    w.create_text(650,460,anchor="n",text="600")
    w.create_text(700,460,anchor="n",text="650")
    w.create_text(750,460,anchor="n",text="700")
    w.create_text(800,460,anchor="n",text="750")
    w.create_text(850,460,anchor="n",text="800")
    
    #--4.Creating vertical axis labels--
    w.create_text(20,400,anchor="se",text="50")
    w.create_text(20,350,anchor="se",text="100")
    w.create_text(20,300,anchor="se",text="150")
    w.create_text(20,250,anchor="se",text="200")
    w.create_text(20,200,anchor="se",text="250")
    w.create_text(20,150,anchor="se",text="300")
    w.create_text(20,100,anchor="se",text="350")
    w.create_text(20,50,anchor="se",text="400")
    
    #==simulation==
    while(time<timeMax):
        time+=timestep
    
        #--5.Draw Graf--
        w.create_line(x1,y1,x2,y2,fill="#000000")
        x1=x2
        y1=y2
        
        #--7.Gravity--
        vy=-(vstart*math.sin(math.radians(vinkel))-9.82*time)
        vx=vstart*math.cos(math.radians(vinkel))
       
        #--8.Collision with floor-- FIND UD AF, DET VIRKER IKKE
        if y1>450 and vy>0:
            w.create_text(x1,y1,anchor="s",text="Xmax= " + str(x2-50))
            time=timeMax

        #--6.Converting movement into position--
        x2=x1+vx*timestep
        y2=y1+vy*timestep

        #--11.Draw TimeCounter--
        timeCount=timeCount+1
        if timeCount == timeCountMax:
            w.create_text(x1,y1,anchor="nw",text=str(time/10) + " sec")
            timeCount=0
    
def hjaelp():
    print "Skriv starthastigheden og vinklen ind i de to felter."
    print " "
    print "Skriv '.' (punktum) i stedet for ',' (komma) hvis du oensker at angive et tal med flere decimaler."
    print " "
    print "Tryk 'DRAW' (tegn) når du er klar."
    print " "
    print "Nu vil et nyt vindue dukke op med en model af det skraa kast, hvis informationer du skrev ind foer."

def xmaks():
    vinkel=float(vinkel1.get())
    vstart=float(vstart1.get())
    print "Xmax = " , float(vstart*vstart*(math.sin(math.radians(2*vinkel)))/9.82)

text=Label (master, text="vinkel!")
text.pack()
vinkel1=Entry (master, width=25)
vinkel1.insert (0, int(45))
vinkel1.pack()

text=Label (master, text="starthastighed!")
text.pack()
vstart1=Entry (master, width=25)
vstart1.insert (0, int(75))
vstart1.pack()

text=Label (master, text=" ")
text.pack()

knap1=Button (master, text="DRAW", command=graf)
knap1.pack()

knap3=Button (master, text="Xmax", command=xmaks)
knap3.pack()

knap2=Button (master, text="HELP", command=hjaelp)
knap2.pack()

knap5=Button (master, text="Luk program", command=quit)
knap5.pack()

text=Label (master, text=" ")
text.pack()

master.mainloop()

