導(dǎo)讀 | 這個(gè)Python + Pygame程序繪制了4條衰減正弦波的軌跡,每條軸2條,彩虹色。 它會生成一系列隨機(jī)諧波圖(harmonographs)。 |
harmonograph是通常在科學(xué)博物館中看到的機(jī)械設(shè)備,它有兩個(gè)或多個(gè)帶有筆的擺錘,可以在一張紙上畫畫。 擺筆運(yùn)動,筆在紙上畫出漂亮的圖案。 通過繪制一起作用在繪圖點(diǎn)上的正交正弦波,可以在計(jì)算機(jī)程序中輕松模擬這一點(diǎn)。 這會生成利薩如圖形(Lissajous-Figure),這些圖形會被衰減以形成令人愉悅的“平行”線嵌套,就像您在鈔票上看到的那樣。
創(chuàng)新互聯(lián)公司堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的芝罘網(wǎng)站設(shè)計(jì)、移動媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!它的速度很快,并且可以根據(jù)需要將其設(shè)置為更快(或更慢)。 提示:將顯示窗口設(shè)置為全屏。 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
進(jìn)入你保存它的目錄,讓它可執(zhí)行,然后運(yùn)行它:
chmod +x harmonograph.py ./harmonograph.py
效果圖:
原文來自: https://www.linuxprobe.com/python-pygame.html
當(dāng)前文章:漂亮的頻譜諧波圖必備Python+Pygame-創(chuàng)新互聯(lián)
網(wǎng)站鏈接:http://vcdvsql.cn/article26/ddpjcg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、面包屑導(dǎo)航、網(wǎng)站設(shè)計(jì)、定制網(wǎng)站、網(wǎng)站維護(hù)、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容