|
1 | | -/** |
2 | | - * Additive Wave |
3 | | - * by Daniel Shiffman. |
4 | | - * |
5 | | - * Create a more complex wave by adding two waves together. |
6 | | - */ |
7 | | - |
8 | | -int xspacing = 8; |
9 | | -int w; |
10 | | -int maxwaves = 4; |
11 | | - |
12 | | -float theta = 0.0; |
13 | | -float amplitude[4]; |
14 | | -float dx[4]; |
15 | | -float* yvalues; |
16 | | - |
17 | | -void setup() { |
18 | | - size(640, 360); |
19 | | - frameRate(30); |
20 | | - colorMode(RGB, 255, 255, 255, 100); |
21 | | - w = width + 16; |
22 | | - for (int i = 0; i < maxwaves; i++) { |
23 | | - amplitude[i] = random(10, 30); |
24 | | - float period = random(100, 300); |
25 | | - dx[i] = (TWO_PI / period) * xspacing; |
26 | | - } |
27 | | - yvalues = new float[w / xspacing]; |
| 1 | +let xspacing = 8; |
| 2 | +let w; |
| 3 | +let maxwaves = 4; |
| 4 | + |
| 5 | +let theta = 0.0; |
| 6 | +let amplitude = []; |
| 7 | +let dx = []; |
| 8 | +let yvalues = []; |
| 9 | + |
| 10 | +function setup() { |
| 11 | + createCanvas(640, 360); |
| 12 | + frameRate(30); |
| 13 | + |
| 14 | + colorMode(RGB, 255, 255, 255, 100); |
| 15 | + |
| 16 | + w = width + 16; |
| 17 | + |
| 18 | + for (let i = 0; i < maxwaves; i++) { |
| 19 | + amplitude[i] = random(10, 30); |
| 20 | + |
| 21 | + let period = random(100, 300); |
| 22 | + dx[i] = (TWO_PI / period) * xspacing; |
| 23 | + } |
| 24 | + |
| 25 | + yvalues = new Array(floor(w / xspacing)); |
28 | 26 | } |
29 | 27 |
|
30 | | -void calcWave() { |
31 | | - theta += 0.02; |
32 | | - int len = w / xspacing; |
33 | | - for (int i = 0; i < len; i++) yvalues[i] = 0; |
34 | | - for (int j = 0; j < maxwaves; j++) { |
35 | | - float x = theta; |
36 | | - for (int i = 0; i < len; i++) { |
37 | | - if (j % 2 == 0) yvalues[i] += sin(x) * amplitude[j]; |
38 | | - else yvalues[i] += cos(x) * amplitude[j]; |
39 | | - x += dx[j]; |
40 | | - } |
| 28 | +function calcWave() { |
| 29 | + theta += 0.02; |
| 30 | + |
| 31 | + let len = floor(w / xspacing); |
| 32 | + |
| 33 | + for (let i = 0; i < len; i++) { |
| 34 | + yvalues[i] = 0; |
| 35 | + } |
| 36 | + |
| 37 | + for (let j = 0; j < maxwaves; j++) { |
| 38 | + let x = theta; |
| 39 | + |
| 40 | + for (let i = 0; i < len; i++) { |
| 41 | + if (j % 2 === 0) { |
| 42 | + yvalues[i] += sin(x) * amplitude[j]; |
| 43 | + } else { |
| 44 | + yvalues[i] += cos(x) * amplitude[j]; |
| 45 | + } |
| 46 | + |
| 47 | + x += dx[j]; |
41 | 48 | } |
| 49 | + } |
42 | 50 | } |
43 | 51 |
|
44 | | -void renderWave() { |
45 | | - noStroke(); |
46 | | - fill(255, 50); |
47 | | - ellipseMode(CENTER); |
48 | | - int len = w / xspacing; |
49 | | - for (int x = 0; x < len; x++) { |
50 | | - ellipse(x * xspacing, height/2 + yvalues[x], 16, 16); |
51 | | - } |
| 52 | +function renderWave() { |
| 53 | + noStroke(); |
| 54 | + fill(255, 50); |
| 55 | + |
| 56 | + ellipseMode(CENTER); |
| 57 | + |
| 58 | + let len = floor(w / xspacing); |
| 59 | + |
| 60 | + for (let x = 0; x < len; x++) { |
| 61 | + ellipse( |
| 62 | + x * xspacing, |
| 63 | + height / 2 + yvalues[x], |
| 64 | + 16, |
| 65 | + 16 |
| 66 | + ); |
| 67 | + } |
52 | 68 | } |
53 | 69 |
|
54 | | -void draw() { |
55 | | - background(0); |
56 | | - calcWave(); |
57 | | - renderWave(); |
| 70 | +function draw() { |
| 71 | + background(0); |
| 72 | + |
| 73 | + calcWave(); |
| 74 | + renderWave(); |
58 | 75 | } |
0 commit comments