Skip to content

Instantly share code, notes, and snippets.

@Ephraim-Bryski
Created June 5, 2018 15:02
Show Gist options
  • Save Ephraim-Bryski/4e05234120dad6dcc197cb39ba0384be to your computer and use it in GitHub Desktop.
Save Ephraim-Bryski/4e05234120dad6dcc197cb39ba0384be to your computer and use it in GitHub Desktop.
from tkinter import *
import time
import numpy as np
import math
tk=Tk()
WIDTH=500
HEIGHT=500
canvas=Canvas(tk,width=WIDTH,height=HEIGHT)
tk.title('Viscosity')
canvas.pack()
SIDE=200
LENGTH=200
k=100
dt=.0003
vell=[0,100]
velr=[100,-1000]
TL=canvas.create_rectangle(200,300,210,310,fill='blue')
print(canvas.coords(TL))
TR=canvas.create_rectangle(200+SIDE,300,210+SIDE,310,fill='blue')
#BL=canvas.create_rectangle(200,100+SIDE,210,110+SIDE,fill='blue')
#BR=canvas.create_rectangle(200+SIDE,100+SIDE,210+SIDE,110+SIDE,fill='blue')
while True:
TLTR=(canvas.coords(TR)[0]-canvas.coords(TL)[0],canvas.coords(TL)[1]-canvas.coords(TR)[1])
accl=np.dot(((np.linalg.norm(TLTR)-LENGTH)*k/np.linalg.norm(TLTR)),(np.dot(TLTR,(1,0)),np.dot(TLTR,(0,-1))))
accr=-accl
vell+=accl*dt
velr+=accr*dt
canvas.move(TL,vell[0]*dt,vell[1]*dt)
canvas.move(TR,velr[0]*dt,velr[1]*dt)
if canvas.coords(TL)[0]<0 or canvas.coords(TL)[2]>WIDTH:
vell[0]=-vell[0]
if canvas.coords(TR)[0]<0 or canvas.coords(TR)[2]>WIDTH:
velr[0]=-velr[0]
if canvas.coords(TL)[1]<0 or canvas.coords(TL)[3]>HEIGHT:
vell[1]=-vell[1]
if canvas.coords(TR)[1]<0 or canvas.coords(TR)[3]>HEIGHT:
velr[1]=-velr[1]
tk.update()
print(vell)
tk.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment