Last active
August 12, 2017 07:35
-
-
Save abhikpal/bd654cb900c87d2c3fcf54cea98990c1 to your computer and use it in GitHub Desktop.
Profiling code for p5.
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
# | |
# Additive Wave | |
# by Daniel Shiffman | |
# | |
# Python port: Abhik Pal | |
# | |
# Create a more complex wave by adding two waves together. | |
# | |
import cProfile | |
from p5 import * | |
frames = [] | |
# How far apart should each horizontal location be spaced. | |
xspacing = 8 | |
# width of the entire wave | |
wave_width = None | |
# total number of waves to add | |
max_waves = 4 | |
theta = 0 | |
# height of wave | |
amplitude = [] | |
# values for incrementing X, to be calculated as a function of period | |
# and `xspacing`. | |
dx = [] | |
# using an array to store height values for the wave (not entirely | |
# necessay) | |
yvalues = [] | |
def setup(): | |
global wave_width | |
size(640, 360) | |
title("Additive Wave") | |
wave_width = width + 16 | |
for i in range(max_waves): | |
amplitude.append(random_uniform(10, 30)) | |
period = random_uniform(100, 300) | |
dx.append((TWO_PI / period) * xspacing) | |
def draw(): | |
background(0) | |
calculate_wave() | |
render_wave() | |
frames.append(str(frame_rate)) | |
if frame_count == 60 * 10: | |
exit() | |
def calculate_wave(): | |
"""(Re)calculate the yvalues for the wave | |
""" | |
global theta, yvalues | |
# increment theta (try different values of 'angular velocity' | |
# here.) | |
theta += 0.08 | |
yvalues = [0] * int(wave_width / xspacing) | |
for j in range(max_waves): | |
x = theta | |
for i in range(int(wave_width / xspacing)): | |
if j % 2 == 0: | |
yvalues[i] += cos(x) + amplitude[j] | |
else: | |
yvalues[i] += cos(x) * amplitude[j] | |
x += dx[j] | |
def render_wave(): | |
"""Render the wave""" | |
no_stroke() | |
fill(255, 127) | |
for x, yval in enumerate(yvalues): | |
location = x * xspacing, (height / 2) + yval | |
circle(location, 16, mode='CENTER') | |
if __name__ == '__main__': | |
cProfile.run('run()', 'additive_wave_stats') | |
with open('additive_wave_frames.txt', 'w+') as f: | |
f.write('\n'.join(frames)) |
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
# | |
# Sine | |
# | |
# Smoothly scaling size with the sin() function. | |
import cProfile | |
from p5 import * | |
frames = [] | |
diameter = None | |
angle = 0 | |
def setup(): | |
global diameter | |
size(640, 360) | |
title("Sine") | |
diameter = height - 10 | |
no_stroke() | |
fill(255, 204, 0) | |
def draw(): | |
global angle | |
background(0) | |
d1 = 10 + (sin(angle) * diameter / 2) + diameter / 2; | |
d2 = 10 + (sin(angle + PI / 2) * diameter / 2) + diameter / 2; | |
d3 = 10 + (sin(angle + PI) * diameter / 2) + diameter / 2; | |
circle((0, height / 2), d1) | |
circle((width / 2, height / 2), d2) | |
circle((width, height / 2), d3) | |
angle += 0.04 | |
frames.append(str(frame_rate)) | |
if frame_count == 600: | |
exit() | |
if __name__ == '__main__': | |
cProfile.run('run()', 'sine_stats') | |
with open('sine_frames.txt', 'w+') as f: | |
f.write('\n'.join(frames)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment