55 * Create a more complex wave by adding two waves together.
66 */
77
8- int xspacing = 8 ; // How far apart should each horizontal location be spaced
9- int w ; // Width of entire wave
10- int maxwaves = 4 ; // total # of waves to add together
8+ var xspacing = 8 ; // How far apart should each horizontal location be spaced
9+ var w ; // Width of entire wave
10+ var maxwaves = 4 ; // total # of waves to add together
1111
12- float theta = 0.0 ;
13- float [ ] amplitude = new float [ maxwaves ] ; // Height of wave
14- float [ ] dx = new float [ maxwaves ] ; // Value for incrementing X, to be calculated as a function of period and xspacing
15- float [ ] yvalues ; // Using an array to store height values for the wave (not entirely necessary)
12+ var theta = 0.0 ;
13+ var amplitude = new Array ( maxwaves ) ; // Height of wave
14+ var dx = new Array ( maxwaves ) ; // Value for incrementing X, to be calculated as a function of period and xspacing
15+ var yvalues ; // Using an array to store height values for the wave (not entirely necessary)
1616
17- void setup ( ) {
18- size ( 640 , 360 ) ;
17+ function setup ( ) {
18+ createCanvas ( 640 , 360 ) ;
1919 frameRate ( 30 ) ;
2020 colorMode ( RGB , 255 , 255 , 255 , 100 ) ;
2121 w = width + 16 ;
2222
23- for ( int i = 0 ; i < maxwaves ; i ++ ) {
23+ for ( var i = 0 ; i < maxwaves ; i ++ ) {
2424 amplitude [ i ] = random ( 10 , 30 ) ;
25- float period = random ( 100 , 300 ) ; // How many pixels before the wave repeats
25+ var period = random ( 100 , 300 ) ; // How many pixels before the wave repeats
2626 dx [ i ] = ( TWO_PI / period ) * xspacing ;
2727 }
2828
29- yvalues = new float [ w / xspacing ] ;
29+ yvalues = new Array ( w / xspacing ) ;
3030}
3131
32- void draw ( ) {
32+ function draw ( ) {
3333 background ( 0 ) ;
3434 calcWave ( ) ;
3535 renderWave ( ) ;
3636}
3737
38- void calcWave ( ) {
38+ function calcWave ( ) {
3939 // Increment theta (try different values for 'angular velocity' here
4040 theta += 0.02 ;
4141
4242 // Set all height values to zero
43- for ( int i = 0 ; i < yvalues . length ; i ++ ) {
43+ for ( var i = 0 ; i < yvalues . length ; i ++ ) {
4444 yvalues [ i ] = 0 ;
4545 }
4646
4747 // Accumulate wave height values
48- for ( int j = 0 ; j < maxwaves ; j ++ ) {
49- float x = theta ;
50- for ( int i = 0 ; i < yvalues . length ; i ++ ) {
48+ for ( var j = 0 ; j < maxwaves ; j ++ ) {
49+ var x = theta ;
50+ for ( var i = 0 ; i < yvalues . length ; i ++ ) {
5151 // Every other wave is cosine instead of sine
5252 if ( j % 2 == 0 ) yvalues [ i ] += sin ( x ) * amplitude [ j ] ;
5353 else yvalues [ i ] += cos ( x ) * amplitude [ j ] ;
@@ -56,13 +56,13 @@ void calcWave() {
5656 }
5757}
5858
59- void renderWave ( ) {
59+ function renderWave ( ) {
6060 // A simple way to draw the wave with an ellipse at each location
6161 noStroke ( ) ;
6262 fill ( 255 , 50 ) ;
6363 ellipseMode ( CENTER ) ;
64- for ( int x = 0 ; x < yvalues . length ; x ++ ) {
65- ellipse ( x * xspacing , width / 2 + yvalues [ x ] , 16 , 16 ) ;
64+ for ( var x = 0 ; x < yvalues . length ; x ++ ) {
65+ ellipse ( x * xspacing , height / 2 + yvalues [ x ] , 16 , 16 ) ;
6666 }
6767}
6868
0 commit comments