Skip to content

Instantly share code, notes, and snippets.

@Ephraim-Bryski
Created June 5, 2018 15:03
Show Gist options
  • Save Ephraim-Bryski/e33678c1e6cfba0173e1e876b3080382 to your computer and use it in GitHub Desktop.
Save Ephraim-Bryski/e33678c1e6cfba0173e1e876b3080382 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=400
canvas=Canvas(tk,width=WIDTH,height=HEIGHT)
tk.title('Mesh')
canvas.pack()
LENGTH=70
DIMEN=120
ROWS=1
COLUMNS=1
k=300
dt=.001
class Particles:
def __init__(self,pos):
self.shape=canvas.create_rectangle(DIMEN*pos[0],DIMEN*pos[1],DIMEN*pos[0]+10,DIMEN*pos[1]+10,fill='blue')
self.vel=[0,0]
def move(self,above,below,left,right):
rel_pos=[above,below,left,right]
acc=[0,0]
for i in rel_pos:
if i=='none':
acc_i=[0,0]
else:
vec=[i[0]-pos[0],i[1]-pos[1]]
acc_i=(np.dot(((np.linalg.norm(vec)-LENGTH)*k/np.linalg.norm(vec)),(np.dot(vec,(1,0)),np.dot(vec,(0,1)))))
acc=[acc[0]+acc_i[0],acc[1]+acc_i[1]]
self.vel+=np.dot(acc,dt)
canvas.move(self.shape,self.vel[0]*dt,self.vel[1]*dt)
tk.update()
time.sleep(.01)
particles=[]
for x in range(COLUMNS):
particles.append([])
for y in range(ROWS):
particles[x].append(Particles([x,y]))
for t in range(100):
for column in range (COLUMNS):
for particle in range (ROWS):
if particle==0:
above='none'
else:
above=canvas.coords(particles[column][particle-1].shape)
above=[above[0],above[1]]
if particle==ROWS-1:
below='none'
else:
below=canvas.coords(particles[column][particle+1].shape)
below=[below[0],below[1]]
if column==0:
left='none'
else:
left=canvas.coords(particles[column-1][particle].shape)
left=[left[0],left[1]]
if column==COLUMNS-1:
right='none'
else:
right=canvas.coords(particles[column+1][particle].shape)
right=[right[0],right[1]]
pos=canvas.coords(particles[column][particle].shape)
pos=[pos[0],pos[1]]
particles[column][particle].move(above,below,left,right)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment