processing模拟三角级数合成方波过程
代码
1: int radius = 2;
2: int[] accumys;
3: int times = 0;
4:
5: float scale = 1;
6: int origin = 400;
7:
8: float factor = 1;
9:
10: public void setup() {
11: size(displayWidth, displayHeight);
12: background(0);
13: frameRate(2);
14:
15: accumys = new int[displayWidth];
16:
17: for(int i=0;i<displayWidth;i++)
18: {
19: accumys[i] = 0;
20: }
21:
22: scale = 1;
23: origin = displayHeight / 2;
24:
25: factor = (PI * 3/ displayWidth );
26: }
27:
28: public void draw() {
29:
30: fill(0,0,0,50);
31: rect(-1,-1,displayWidth + 1, displayHeight+1);
32:
33: float k = ++times;
34: int last_y = 0;
35: for (int i=0;i<displayWidth;i++)
36: {
37: float t = i;
38: float up = sin((2 * k - 1) * factor * t);
39: float down = (2 * k - 1) * factor;
40:
41: accumys[i] += (int) ((float)4 * up / (PI * down));
42:
43: if (i >= 1)
44: {
45: colorMode(HSB, 255);
46: stroke(90, 255, 255);
47: line(i-1, last_y * scale + origin, i, accumys[i] * scale + origin);
48: line(i, last_y * scale + origin, i+1, accumys[i] * scale + origin);
49: }
50:
51: last_y = accumys[i];
52: }
53:
54: }
截图