这个Python + Pygame程序绘制了4条衰减正弦波的轨迹,每条轴2条,彩虹色。 它会生成一系列随机谐波图(harmonographs)。 |
harmonograph是通常在科学博物馆中看到的机械设备,它有两个或多个带有笔的摆锤,可以在一张纸上画画。 摆笔运动,笔在纸上画出漂亮的图案。 通过绘制一起作用在绘图点上的正交正弦波,可以在计算机程序中轻松模拟这一点。 这会生成利萨如图形(Lissajous-Figure),这些图形会被衰减以形成令人愉悦的“平行”线嵌套,就像您在钞票上看到的那样。
它的速度很快,并且可以根据需要将其设置为更快(或更慢)。 提示:将显示窗口设置为全屏。 MIT许可证; 从GitHub下载
#!/usr/bin/python ''' Spectral Harmonographs Copyright 2014 Alan Richmond (Tuxar.uk) ''' print("Quit: q key, Screenshot: spacebar") import pygame, sys, random as r from pygame.locals import * from math import pi, sin, cos, exp # EDIT THESE: width,height=1280,720 # YouTube HD width,height=1920,1080 # my left monitor width,height=1280,1024 # my right monitor width,height=1680,1050 # Lucy's monitor width,height=1200,800 #width,height=2560,1440 # YT channel art dd=0.99995 # decay factor dt=0.02 # time increment speed=200 # yes, speed hui=57*2 # Hue increment hue,sat,val,aaa=0,100,100,0 sd=0.005 # frequency spread (from integer) mx=4 # max range for amplitudes & frequencies print("Hit SPACE to save") def check_event(): global save for event in pygame.event.get(): if event.type == QUIT: sys.exit() elif event.type == KEYDOWN and event.key == K_q: sys.exit() elif event.type == KEYDOWN and event.key == K_SPACE: save=True print("Saving when finished...") def scale(length): while True: a1,a2=r.randint(-mx,mx),r.randint(-mx,mx) max=abs(a1)+abs(a2) if max>0: break return a1,a2,length/(2*max) steps=0 pygame.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=False while True: # Amplitudes & scales ax1,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.0 t=0.0 # angle for sin first=True while dec>0.015: # calculate next x,y point along line x = xscale * dec * (ax1*sin(t * fx1 + px1) + ax2*sin(t * fx2 + px2)) + width/2 y = yscale * dec * (ay1*sin(t * fy1 + py1) + ay2*sin(t * fy2 + py2)) + height/2 dec*=dd # decay if not first: # ignore any complaint about prev_x,y being undefined # fg.hsva=(hue,sat,val,aaa) # hue = (hue + dt*hui) % 360 # cycle hue pygame.draw.aaline(screen, fg, (x, y), (prev_x, prev_y), 1) else: first=False prev_x = x # save x,y for next line segment start prev_y = y if steps%speed==0: pygame.display.update() steps+=1 t+=dt # increment angle for sin check_event() if save: # parameters are encoded into filename pars='shg-{0}_{1}-{2}_{3}-{4}_{5}'.format(ax1,ax2,fx1,fx2,px1,px2) pygame.image.save(screen, pars+'.jpg') print("Saved as "+pars+'.jpg') save=False screen.fill((0,0,0)) # screen.fill((255,255,255))
例如在Ubuntu/Debian版的Linux上,你可能需要安装python和/或pygame:
sudo apt-get install pygame sudo apt-get install python
进入你保存它的目录,让它可执行,然后运行它:
chmod +x harmonograph.py ./harmonograph.py
效果图:
来源:oschina
链接:https://my.oschina.net/u/4331421/blog/3160095