asdfasdf
from kandinsky import * from time import * from math import * mass_points=[] springs=[] target_fps=24 fps=0 def draw_line(x0,y0,x1,y1, color): x0=round(x0) y0=round(y0) x1=round(x1) y1=round(y1) dx=round(abs(x1-x0)) dy=round(abs(y1-y0)) sx=-1 if x0>x1 else 1 sy=-1 if y0>y1 else 1 err=dx-dy #draw_string("p1: ("+str(x0)+","+str(y0)+")",0,100) #draw_string("p2: ("+str(x1)+","+str(y1)+")",0,120) while True: set_pixel(x0,y0,color) if x0==x1 and y0==y1: break e2=2*err if e2 > -dy: err-=dy x0+=sx if e2 < dx: err+=dx y0+=sy def reach_fps(current_f): global fps draw_string(str(round(fps)),0,0) target_f=1/target_fps if current_f<target_f: sleep(target_f-current_f) fps=1/(monotonic()-begin) class Mass_Point(): def __init__(self, x, y,): mass_points.append(self) self.x=x self.y=y self.vx=0 self.vy=0 self.fx=0 self.fy=0 self.mass=1 def update(self): # Set force to zero # Recalculate force (Gravity, Spring Force, etc.) self.fy+=0.981*self.mass # Gravity # Update Velocity self.vx+=self.fx/self.mass self.vy+=self.fy/self.mass # Update Position self.x+=self.vx self.y+=self.vy if self.y>=225: self.vy*=-1 self.y=225 def draw(self): fill_rect(round(self.x)-1, round(self.y)-1, 3, 3, (255,0,0)) class Spring(): def __init__(self, A, B): springs.append(self) self.A = A self.B = B self.stiffness = 0.1 # Adjust stiffness value as needed self.rest_length = sqrt((self.B.x - self.A.x) ** 2 + (self.B.y - self.A.y) ** 2) self.damping_factor = 0.05 # Adjust damping factor as needed def update(self): displacement = sqrt((self.B.x - self.A.x) ** 2 + (self.B.y - self.A.y) ** 2) if displacement != 0: current_length = displacement stretch = current_length - self.rest_length # Calculate force due to Hooke's Law spring_force = self.stiffness * stretch # Calculate damping force damping_force = self.damping_factor * (self.B.vx - self.A.vx) # Decompose forces into x and y components total_force = spring_force - damping_force Fx = (total_force * (self.B.x - self.A.x)) / displacement Fy = (total_force * (self.B.y - self.A.y)) / displacement # Apply forces to masses self.A.fx += Fx self.A.fy += Fy self.B.fx -= Fx # Opposite force on the other mass self.B.fy -= Fy def draw(self): draw_line(self.A.x, self.A.y, self.B.x, self.B.y, (240, 242, 243)) m=Mass_Point(50,50) m2=Mass_Point(100,50) m3=Mass_Point(100,100) m4=Mass_Point(50,100) s=Spring(m,m2) s2=Spring(m2,m3) s3=Spring(m3,m4) s4=Spring(m4,m) while 1: begin=monotonic() fill_rect(0,0,320,225,(24,32,48)) for spring in springs: spring.update() spring.draw() for mass_point in mass_points: mass_point.update() mass_point.draw() reach_fps(monotonic()-begin)