5
5
* Using sin/cos lookup tables, blends colors, and draws a series of
6
6
* rotating arcs on the screen.
7
7
*/
8
-
9
- // Trig lookup tables borrowed from Toxi; cryptic but effective.
10
- float sinLUT[];
11
- float cosLUT[];
12
- float SINCOS_PRECISION = 1.0 ;
13
- int SINCOS_LENGTH = int ((360.0 / SINCOS_PRECISION ));
14
-
15
- // System data
16
- boolean dosave= false ;
17
- int num;
18
- float pt[];
19
- int style[];
8
+
9
+ final int COUNT = 150 ;
10
+
11
+ float [] pt;
12
+ int [] style;
20
13
21
14
22
15
void setup () {
23
16
size (1024 , 768 , P3D );
24
17
background (255 );
18
+ randomSeed (100 );
25
19
26
- // Fill the tables
27
- sinLUT= new float [SINCOS_LENGTH ];
28
- cosLUT= new float [SINCOS_LENGTH ];
29
- for (int i = 0 ; i < SINCOS_LENGTH ; i++ ) {
30
- sinLUT[i]= (float )Math . sin(i* DEG_TO_RAD * SINCOS_PRECISION );
31
- cosLUT[i]= (float )Math . cos(i* DEG_TO_RAD * SINCOS_PRECISION );
32
- }
33
-
34
- num = 150 ;
35
- pt = new float [6 * num]; // rotx, roty, deg, rad, w, speed
36
- style = new int [2 * num]; // color, render style
20
+ pt = new float [6 * COUNT ]; // rotx, roty, deg, rad, w, speed
21
+ style = new int [2 * COUNT ]; // color, render style
37
22
38
23
// Set up arc shapes
39
- int index= 0 ;
40
- float prob;
41
- for (int i= 0 ; i< num; i++ ) {
42
- pt[index++ ] = random (PI * 2 ); // Random X axis rotation
43
- pt[index++ ] = random (PI * 2 ); // Random Y axis rotation
24
+ int index = 0 ;
25
+ for (int i = 0 ; i < COUNT ; i++ ) {
26
+ pt[index++ ] = random (TAU ); // Random X axis rotation
27
+ pt[index++ ] = random (TAU ); // Random Y axis rotation
44
28
45
29
pt[index++ ] = random (60 ,80 ); // Short to quarter-circle arcs
46
- if (random (100 )> 90 ) pt[index]= ( int ) random (8 ,27 )* 10 ;
30
+ if (random (100 )> 90 ) pt[index] = floor ( random (8 ,27 )) * 10 ;
47
31
48
32
pt[index++ ] = int (random (2 ,50 )* 5 ); // Radius. Space them out nicely
49
33
@@ -53,30 +37,40 @@ void setup() {
53
37
pt[index++ ] = radians (random (5 ,30 ))/ 5 ; // Speed of rotation
54
38
55
39
// get colors
56
- prob = random (100 );
57
- if (prob< 30 ) style[i* 2 ]= colorBlended(random (1 ), 255 ,0 ,100 , 255 ,0 ,0 , 210 );
58
- else if (prob< 70 ) style[i* 2 ]= colorBlended(random (1 ), 0 ,153 ,255 , 170 ,225 ,255 , 210 );
59
- else if (prob< 90 ) style[i* 2 ]= colorBlended(random (1 ), 200 ,255 ,0 , 150 ,255 ,0 , 210 );
60
- else style[i* 2 ]= color (255 ,255 ,255 , 220 );
40
+ float prob = random (100 );
41
+
42
+ // if (prob < 30) {
43
+ // style[i*2] = colorBlended(random(1), 255,0,100, 255,0,0, 210);
44
+ // } else if (prob < 70) {
45
+ // style[i*2] = colorBlended(random(1), 0,153,255, 170,225,255, 210);
46
+ // } else if (prob < 90) {
47
+ // style[i*2] = colorBlended(random(1), 200,255,0, 150,255,0, 210);
48
+ // } else {
49
+ // style[i*2] = color(255,255,255, 220);
50
+ // }
61
51
62
- if (prob< 50 ) style[i* 2 ]= colorBlended(random (1 ), 200 ,255 ,0 , 50 ,120 ,0 , 210 );
63
- else if (prob< 90 ) style[i* 2 ]= colorBlended(random (1 ), 255 ,100 ,0 , 255 ,255 ,0 , 210 );
64
- else style[i* 2 ]= color (255 ,255 ,255 , 220 );
52
+ if (prob < 50 ) {
53
+ style[i* 2 ] = colorBlended(random (1 ), 200 ,255 ,0 , 50 ,120 ,0 , 210 );
54
+ } else if (prob < 90 ) {
55
+ style[i* 2 ] = colorBlended(random (1 ), 255 ,100 ,0 , 255 ,255 ,0 , 210 );
56
+ } else {
57
+ style[i* 2 ] = color (255 ,255 ,255 , 220 );
58
+ }
65
59
66
- style[i* 2 + 1 ]= ( int )( random (100 ))% 3 ;
60
+ style[i* 2 + 1 ] = floor ( random (100 )) % 3 ;
67
61
}
68
62
}
69
-
70
- void draw () {
71
-
63
+
64
+
65
+ void draw () {
72
66
background (0 );
73
67
74
68
int index= 0 ;
75
69
translate (width / 2 , height / 2 , 0 );
76
70
rotateX (PI / 6 );
77
71
rotateY (PI / 6 );
78
72
79
- for (int i = 0 ; i < num ; i++ ) {
73
+ for (int i = 0 ; i < COUNT ; i++ ) {
80
74
pushMatrix ();
81
75
82
76
rotateX (pt[index++ ]);
@@ -100,63 +94,87 @@ void draw() {
100
94
}
101
95
102
96
// increase rotation
103
- pt[index- 5 ]+= pt[index]/ 10 ;
104
- pt[index- 4 ]+= pt[index++ ]/ 20 ;
97
+ pt[index- 5 ] += pt[index]/ 10 ;
98
+ pt[index- 4 ] += pt[index++ ]/ 20 ;
105
99
106
100
popMatrix ();
107
101
}
102
+ if (frameCount == 100 ) {
103
+ saveFrame (" newer.png" );
104
+ exit ();
105
+ }
108
106
}
109
-
110
-
107
+
108
+
111
109
// Get blend of two colors
112
- int colorBlended(float fract,
113
- float r, float g, float b,
114
- float r2, float g2, float b2, float a) {
115
-
110
+ int colorBlended(float fract,
111
+ float r, float g, float b,
112
+ float r2, float g2, float b2, float a) {
116
113
r2 = (r2 - r);
117
114
g2 = (g2 - g);
118
115
b2 = (b2 - b);
116
+
119
117
return color (r + r2 * fract, g + g2 * fract, b + b2 * fract, a);
120
118
}
121
-
119
+
122
120
123
121
// Draw arc line
124
- void arcLine (float x ,float y ,float deg ,float rad ,float w ) {
125
- int a= (int )(min (deg/ SINCOS_PRECISION ,SINCOS_LENGTH - 1 ));
122
+ void arcLine (float x ,float y ,float degrees ,float radius ,float w ) {
123
+ // int a=(int)(min (deg/SINCOS_PRECISION,SINCOS_LENGTH-1));
126
124
int numlines= (int )(w/ 2 );
127
125
128
126
for (int j= 0 ; j< numlines; j++ ) {
129
127
beginShape ();
130
- for (int i= 0 ; i< a; i++ ) {
131
- vertex (cosLUT[i]* rad+ x,sinLUT[i]* rad+ y);
128
+ for (int i = 0 ; i < degrees; i++ ) { // one step for each degree
129
+ float angle = radians (i);
130
+ // vertex(cosLUT[i]*rad+x,sinLUT[i]*rad+y);
131
+ vertex (x + radius* cos (angle), y + radius* sin (angle));
132
132
}
133
133
endShape ();
134
- rad += 2 ;
134
+ radius += 2 ;
135
135
}
136
136
}
137
+
137
138
138
139
// Draw arc line with bars
139
- void arcLineBars (float x ,float y ,float deg , float rad , float w ) {
140
- int a = int ((min (deg/ SINCOS_PRECISION ,SINCOS_LENGTH - 1 )));
141
- a /= 4 ;
140
+ void arcLineBars (float x , float y ,float degrees , float radius , float w ) {
141
+ // int a = int((min (deg/SINCOS_PRECISION,SINCOS_LENGTH-1)));
142
+ // a /= 4;
142
143
143
144
beginShape (QUADS );
144
- for (int i= 0 ; i< a; i+= 4 ) {
145
- vertex (cosLUT[i]* (rad)+ x,sinLUT[i]* (rad)+ y);
146
- vertex (cosLUT[i]* (rad+ w)+ x,sinLUT[i]* (rad+ w)+ y);
147
- vertex (cosLUT[i+ 2 ]* (rad+ w)+ x,sinLUT[i+ 2 ]* (rad+ w)+ y);
148
- vertex (cosLUT[i+ 2 ]* (rad)+ x,sinLUT[i+ 2 ]* (rad)+ y);
145
+ // for (int i=0; i<a; i+=4) {
146
+ for (int i = 0 ; i < degrees/ 4 ; i += 4 ) { // degrees, but in steps of 4
147
+ float angle = radians (i);
148
+ // vertex(cosLUT[i]*(rad)+x,sinLUT[i]*(rad)+y);
149
+ vertex (x + radius* cos (angle),
150
+ y + radius* sin (angle));
151
+ // vertex(cosLUT[i]*(rad+w)+x,sinLUT[i]*(rad+w)+y);
152
+ vertex (x + (radius+ w) * cos (angle),
153
+ y + (radius+ w) * sin (angle));
154
+ angle = radians (i+ 2 );
155
+ // vertex(cosLUT[i+2]*(rad+w)+x,sinLUT[i+2]*(rad+w)+y);
156
+ vertex (x + (radius+ w) * cos (angle),
157
+ y + (radius+ w) * sin (angle));
158
+ // vertex(cosLUT[i+2]*(rad)+x,sinLUT[i+2]*(rad)+y);
159
+ vertex (x + radius* cos (angle),
160
+ y + radius* sin (angle));
149
161
}
150
162
endShape ();
151
163
}
164
+
152
165
153
166
// Draw solid arc
154
- void arc (float x ,float y ,float deg , float rad , float w ) {
155
- int a = int (min (deg/ SINCOS_PRECISION ,SINCOS_LENGTH - 1 ));
167
+ void arc (float x , float y , float degrees , float radius , float w ) {
168
+ // int a = int(min (deg/SINCOS_PRECISION,SINCOS_LENGTH-1));
156
169
beginShape (QUAD_STRIP );
157
- for (int i = 0 ; i < a; i++ ) {
158
- vertex (cosLUT[i]* (rad)+ x,sinLUT[i]* (rad)+ y);
159
- vertex (cosLUT[i]* (rad+ w)+ x,sinLUT[i]* (rad+ w)+ y);
170
+ for (int i = 0 ; i < degrees; i++ ) {
171
+ float angle = radians (i);
172
+ // vertex(cosLUT[i]*(rad)+x,sinLUT[i]*(rad)+y);
173
+ vertex (x + radius* cos (angle),
174
+ y + radius* sin (angle));
175
+ // vertex(cosLUT[i]*(rad+w)+x,sinLUT[i]*(rad+w)+y);
176
+ vertex (x + (radius+ w)* cos (angle),
177
+ y + (radius+ w)* sin (angle));
160
178
}
161
179
endShape ();
162
180
}
0 commit comments