北京那个医院治疗白癜风啊 https://wapjbk.39.net/yiyuanzaixian/bjzkbdfyy/ffxbdf/爱心1效果代码
爱心1
效果
代码
fromtkinterimport*frommathimportlog,sin,cos,piimportrandom#新建画布尺寸大小CANVAS_WIDTH=CANVAS_HEIGHT=CANVAS_CENTER_X=CANVAS_WIDTH/2CANVAS_CENTER_Y=CANVAS_HEIGHT/2IMAGE_ENLARGE=8#定义心形函数defheart_function(t)=16*sin(t)**3y=-(13*cos(t)-5*cos(2*t)-2*cos(3*t)-cos(4*t))y*=IMAGE_ENLARGEx*=IMAGE_ENLARGEx+=CANVAS_CENTER_Xy+=CANVAS_CENTER_Yreturnint(x),int(y)defscatter_inside(x,y,beta=0.15):ratiox=-beta*log(random.random())ratioy=-beta*log(random.random())dx=ratiox*(x-CANVAS_CENTER_X)dy=ratioy*(y-CANVAS_CENTER_Y)returnx-dx,y-dy#定义扰动函数defshrink(x,y,ratio)orce=1/((x-CANVAS_CENTER_X)**2+(y-CANVAS_CENTER_Y)**2)**0.5dx=ratio*force*(x-CANVAS_CENTER_X)dy=ratio*force*(y-CANVAS_CENTER_Y)returnx-dx,y-dy#定义画心函数classHeart定义selfdef__init__(self):self._points=set()self._extra_points=set()self._inside=set()self.all_points={}self.calc_position=shrinkself.build()#创建心形点defbuild(self,number)or_inrange(number):t=random.uniform(0,2*pi)x,y=heart_function(t)self._points.add((int(x),int(y)))forxx,yyinlist(self._points)or_inrange(3),y=scatter_inside(xx,yy,0.05)self._extra_points.add((x,y))#inside点point_list=list(self._points)for_inrange(),y=random.choice(point_list)x,y=scatter_inside(x,y)self._inside.add((int(x),int(y)))#计算心形点defcalc_position(self,x,y,ratio)orce=1/(((x-CANVAS_CENTER_X)**2+(y-CANVAS_CENTER_Y)**2)**0.5)dx=ratio*force*(x-CANVAS_CENTER_X)+random.randint(-1,1)dy=ratio*force*(y-CANVAS_CENTER_Y)+random.randint(-1,1)returnx-dx,y-dydefcalc(self,frame):calc_position=self.calc_positionratio=10*sin(frame/10*pi)all_points=[]#outline心形点forx,yinself._points,y=calc_position(x,y,ratio)size=random.randint(1,3)all_points.append((x,y,size))#inner内圈点forx,yinself._extra_points,y=calc_position(x,y,ratio)size=random.randint(1,2)all_points.append((x,y,size))forx,yinself._inside:x,y=calc_position(x,y,ratio)size=random.randint(1,2)all_points.append((x,y,size))self.all_points[frame]=all_points#画出心形点defrender(self,canvas,frame)orx,y,sizeinself.all_points[frame%20]:canvas.create_rectangle(x,y,x+size,y+size,width=0,fill="#ff")#定义动画函数defdraw(rootk,canvas:Canvas,heart:Heart,frame=0):canvas.delete(all)#heart.render(canvas)heart.render(canvas,frame)root.after(30,draw,root,canvas,heart,frame+1)#主程序调用所有函数if__name__==__main__:root=Tk()canvas=Canvas(root,bg=black,height=CANVAS_HEIGHT,width=CANVAS_WIDTH)#创建字体canvas.pack()heart=Heart()forframeinrange(20):heart.calc(frame)draw(root,canvas,heart)root.mainloop()