Created
April 29, 2017 21:50
-
-
Save GeorgeBekh/a9f72ac95a9df487bb1bcc62ba2dcf5f to your computer and use it in GitHub Desktop.
Draws Lissajous curves
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
from PIL import Image, ImageFont, ImageDraw | |
from random import randint | |
import math | |
import gifmaker | |
import copy | |
isAnimation=False | |
SIZE_RATIO = 4 #for antializing purposes :) | |
sideSize = 2000; | |
size = (sideSize, sideSize) | |
def getFreq(): | |
freq = randint(4, 6) | |
print('freq:') | |
print(freq) | |
return freq | |
def getAmplitude(): | |
ampl = randint(int(sideSize * 0.1), int(sideSize * 0.2)) * SIZE_RATIO | |
print('ampl:') | |
print(ampl) | |
return ampl | |
def getDamping(): | |
damping = randint(10, 200) / 10000 | |
print('damping:') | |
print(damping) | |
return damping | |
def getPhase(): | |
phase = randint(0, 10000) | |
print('phase:') | |
print(phase) | |
return phase | |
AMPLITUDE_1=getAmplitude() | |
FREQUENCY_1=getFreq() | |
DAMPING_1=getDamping() | |
PHASE_1 = getPhase() | |
AMPLITUDE_2=getAmplitude() | |
FREQUENCY_2=getFreq() | |
DAMPING_2=getDamping() | |
PHASE_2 = getPhase() | |
AMPLITUDE_3=getAmplitude() | |
FREQUENCY_3=getFreq() | |
DAMPING_3=getDamping() | |
PHASE_3 = getPhase() | |
AMPLITUDE_4=getAmplitude() | |
FREQUENCY_4=getFreq() | |
DAMPING_4=getDamping() | |
PHASE_4 = getPhase() | |
AMPLITUDE_5=getAmplitude() | |
FREQUENCY_5=getFreq() | |
DAMPING_5=getDamping() | |
PHASE_5 = getPhase() | |
ITERATIONS_COUNT=17000; | |
def save( im, index ): | |
if index % 70 != 0: | |
return | |
im = copy.copy(im) | |
print('saving ' + str(index) + ' frame...') | |
im.thumbnail(size, Image.ANTIALIAS) | |
im.save('Animation/' + str(index) + ".jpg", "JPEG", quality=80, optimize=True) | |
def getX( time ): | |
return AMPLITUDE_1 * math.sin(time * FREQUENCY_1 + PHASE_1) * math.pow(math.e, (-1) * DAMPING_1 * time) + AMPLITUDE_2 * math.sin(time * FREQUENCY_2 + PHASE_2) * math.pow(math.e, (-1) * DAMPING_2 * time) + AMPLITUDE_5 * math.sin(time * FREQUENCY_5 + PHASE_5) * math.pow(math.e, (-1) * DAMPING_5 * time) | |
def getY( time ): | |
return AMPLITUDE_3 * math.sin(time * FREQUENCY_3 + PHASE_3) * math.pow(math.e, (-1) * DAMPING_3 * time) + AMPLITUDE_4 * math.sin(time * FREQUENCY_4 + PHASE_4) * math.pow(math.e, (-1) * DAMPING_4 * time) + AMPLITUDE_5 * math.sin(time * FREQUENCY_5 + PHASE_5) * math.pow(math.e, (-1) * DAMPING_5 * time) | |
im = Image.new('RGB', list(map(lambda x: x*SIZE_RATIO, size)), (255,255,255)) | |
dr = ImageDraw.Draw(im) | |
width, height = im.size | |
previousX = getX(0) + width/2 | |
previousY = getY(0) + height/2 | |
for i in range(0, ITERATIONS_COUNT): | |
currentX = getX(i/70) + width/2 | |
currentY = getY(i/70) + height/2 | |
dr.line([(previousX, previousY), (currentX, currentY)], 'black') | |
if isAnimation: | |
save(im, i) | |
previousX = currentX | |
previousY = currentY | |
if not isAnimation: | |
im.thumbnail(size, Image.ANTIALIAS) | |
im.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment