-
Notifications
You must be signed in to change notification settings - Fork 1
/
UnknownPleasures.pde
120 lines (92 loc) · 3.79 KB
/
UnknownPleasures.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import processing.pdf.*;
boolean record;
void setup() {
// Set up graphics
size(800, 800);
}
void draw() {
// How many lines ("waves") are there in the final image (i.e. vertical count)
int WAVE_COUNT = 80;
// How many data points each wave has, horizontally
int VALUES_PER_WAVE = 100;
// The dimension (both width and height) of the drawing as a percentage of sketch width
float DRAWING_DIMENSION = 0.50;
// The height of each wave, as a percentage of pixel dimensions of the drawing (see the dimension
// variable, below).
float WAVE_HEIGHT = 0.17;
// The width of the "middle part" of each wave
float ENVELOPE_SPREAD = 0.20;
// The noise coefficient to use when generating wave values.
float NOISE_COEFFICIENT = 0.180;
// Sketch background color
color backgroundColor = color(10, 10, 10);
// Wave color
color strokeColor = color(240, 240, 240);
// --------------------
// --- DRAWING CODE ---
// --------------------
// If the record key was pressed, start recording the current draw() call into a PDF file
if (record) {
beginRecord(PDF, "unknown-pleasures-" +
year() + month() + day() + hour() + minute() + second() +
"-####.pdf");
}
// Set up colors
background(backgroundColor);
stroke(strokeColor);
// We're not using fills in this sketch, but it's here for good measure
fill(backgroundColor);
// Calculate pixel dimensions of the drawing (width and height are the same)
float dimension = width * DRAWING_DIMENSION;
// Reset the coordinate system origin to be at the top-left corner of the wave drawing
translate(width / 2 - dimension / 2, height / 2 - dimension / 2);
// Calculate the vertical pixel spacing of waves based on the number of waves and drawing pixel
// dimensions
float waveSpacing = dimension / WAVE_COUNT;
// Calculate the pixel height of each wave
float waveHeight = dimension * WAVE_HEIGHT;
// Start drawing waves
for (int i = 0; i < WAVE_COUNT; i++) {
// Draw one wave
drawWave(i, VALUES_PER_WAVE, dimension, waveHeight, ENVELOPE_SPREAD, NOISE_COEFFICIENT);
// Move the coordinate system origin down, so that it's ready for the next wave
translate(0, waveSpacing);
}
// If we were recording into the PDF, we need to call endRecord() explicitly so the file gets generated
if (record) {
endRecord();
record = false;
}
}
void keyPressed() {
if (key == 'p' || key == 'P') {
// Record a new PDF file when the P key is pressed
record = true;
}
}
void drawWave(int row, int valueCount, float widthPixels, float heightPixels, float spread,
float noiseCoefficient) {
// The distance between each curve point
float horizontalSpacing = widthPixels / (valueCount - 1);
// We're drawing each wave as a Processing shape
beginShape();
// Start drawing individual values within the wave
for (int i = 1; i < valueCount; i++) {
// The "envelope" is a common term denoting a function which is "wrapping" around another
// function. In this case, the envelope is the gaussian ("bell") function, which is used to
// scale the values of Processing's noise() function so that the middle part is more prominent.
float envelope = gaussian(i, 1.0, valueCount / 2.0, spread * valueCount / 2);
float value = envelope * noise(row, i * noiseCoefficient);
// Define a new point in the shape we're currently drawing
vertex(i * horizontalSpacing, - value * heightPixels);
}
// We're done drawing this wave
endShape();
}
float gaussian(float x, float a, float b, float c) {
// We're using the normalized Gaussian function as the envelope for each wave.
// Read more about the function here: https://en.wikipedia.org/wiki/Gaussian_function
float e = 2.7182818285;
float exponent = -0.5 * pow((x - b) / c, 2);
return a * pow(e, exponent);
}