漂亮的频谱谐波图必备 Python + Pygame
harmonograph是通常在科学博物馆中看到的机械设备,它有两个或多个带有笔的摆锤,可以在一张纸上画画。 摆笔运动,笔在纸上画出漂亮的图案。 通过绘制一起作用在绘图点上的正交正弦波,可以在计算机程序中轻松模拟这一点。 这会生成利萨如图形(Lissajous-Figure),这些图形会被衰减以形成令人愉悦的“平行”线嵌套,就像您在钞票上看到的那样。
它的速度很快,并且可以根据需要将其设置为更快(或更慢)。 提示:将显示窗口设置为全屏。 MIT许可证; 从GitHub下载
#!/usr/bin/python'''SpectralHarmonographsCopyright2014AlanRichmond(Tuxar.uk)'''print("Quit:qkey,Screenshot:spacebar")importpygame,sys,randomasrfrompygame.localsimport*frommathimportpi,sin,cos,exp#EDITTHESE:width,height=1280,720#YouTubeHDwidth,height=1920,1080#myleftmonitorwidth,height=1280,1024#myrightmonitorwidth,height=1680,1050#Lucy'smonitorwidth,height=1200,800#width,height=2560,1440#YTchannelartdd=0.99995#decayfactordt=0.02#timeincrementspeed=200#yes,speedhui=57*2#Hueincrementhue,sat,val,aaa=0,100,100,0sd=0.005#frequencyspread(frominteger)mx=4#maxrangeforamplitudes&frequenciesprint("HitSPACEtosave")defcheck_event():globalsaveforeventinpygame.event.get():ifevent.type==QUIT:sys.exit()elifevent.type==KEYDOWNandevent.key==K_q:sys.exit()elifevent.type==KEYDOWNandevent.key==K_SPACE:save=Trueprint("Savingwhenfinished...")defscale(length):whileTrue:a1,a2=r.randint(-mx,mx),r.randint(-mx,mx)max=abs(a1)+abs(a2)ifmax>0:breakreturna1,a2,length/(2*max)steps=0pygame.init()pygame.event.set_allowed([QUIT,KEYDOWN])screen=pygame.display.set_mode((width,height),DOUBLEBUF)screen.set_alpha(None)#fg=pygame.Color(0,0,0,0)#fg=(0,0,0)fg=(255,255,255)save=FalsewhileTrue:#Amplitudes&scalesax1,ax2,xscale=scale(width)ay1,ay2,yscale=scale(height)fx1,fx2=r.randint(1,mx)+r.gauss(0,sd),r.randint(1,mx)+r.gauss(0,sd)fy1,fy2=r.randint(1,mx)+r.gauss(0,sd),r.randint(1,mx)+r.gauss(0,sd)px1,px2=r.uniform(0,2*pi),r.uniform(0,2*pi)py1,py2=r.uniform(0,2*pi),r.uniform(0,2*pi)print(ax1,ax2,ay1,ay2)print(fx1,fx2,fy1,fy2)print(px1,px2,py1,py2)dec=1.0t=0.0#angleforsinfirst=Truewhiledec>0.015:#calculatenextx,ypointalonglinex=xscale*dec*(ax1*sin(t*fx1+px1)+ax2*sin(t*fx2+px2))+width/2y=yscale*dec*(ay1*sin(t*fy1+py1)+ay2*sin(t*fy2+py2))+height/2dec*=dd#decayifnotfirst:#ignoreanycomplaintaboutprev_x,ybeingundefined#fg.hsva=(hue,sat,val,aaa)#hue=(hue+dt*hui)%360#cyclehuepygame.draw.aaline(screen,fg,(x,y),(prev_x,prev_y),1)else:first=Falseprev_x=x#savex,yfornextlinesegmentstartprev_y=yifsteps%speed==0:pygame.display.update()steps+=1t+=dt#incrementangleforsincheck_event()ifsave:#parametersareencodedintofilenamepars='shg-{0}_{1}-{2}_{3}-{4}_{5}'.format(ax1,ax2,fx1,fx2,px1,px2)pygame.image.save(screen,pars+'.jpg')print("Savedas"+pars+'.jpg')save=Falsescreen.fill((0,0,0))#screen.fill((255,255,255))
例如在Ubuntu/Debian版的Linux上,你可能需要安装python和/或pygame:
sudoapt-getinstallpygamesudoapt-getinstallpython
进入你保存它的目录,让它可执行,然后运行它:
chmod+xharmonograph.py./harmonograph.py
效果图:
原文来自:https://www.linuxprobe.com/python-pygame.html
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。