140 likes | 255 Views
COMP1610/DGC1330. Lecture 6: Supplementary material For your own interest. Looping. More complex example – Nested FOR Loop. size(450,450); smooth(); strokeWeight(5); for ( int j=0 ; j<10 ; j=j+1 ) { for ( int i=0 ; i<10 ; i=i+1 ) {
E N D
COMP1610/DGC1330 Lecture 6: Supplementary material For your own interest
Looping More complex example – Nested FOR Loop size(450,450); smooth(); strokeWeight(5); for ( int j=0 ; j<10 ; j=j+1 ) { for ( int i=0 ; i<10 ; i=i+1 ) { ellipse ( i*50 , j*50 , 50 , 50); } }
Looping Let’s see step by step for ( int j=0 ; j<10 ; j=j+1 ) { for ( inti=0 ; i<10 ; i=i+1 ) { ellipse ( i*50 , j*50 , 50 , 50); } }
Looping Let’s see step by step
Looping Let’s see step by step
Looping Let’s see step by step
Looping One FOR Loop: for ( inti=0 ; i<10 ; i=i+1 ) { ellipse ( i*50, 100,50,50); }
Looping X10 for ( inti=0 ; i<10 ; i=i+1 ) { ellipse ( i*50, 100,50,50); } for ( inti=0 ; i<10 ; i=i+1 ) { ellipse ( i*50, 100,50,50); } for ( inti=0 ; i<10 ; i=i+1 ) { ellipse ( i*50, 100,50,50); } for ( inti=0 ; i<10 ; i=i+1 ) { ellipse ( i*50, 100,50,50); } for ( inti=0 ; i<10 ; i=i+1 ) { ellipse ( i*50, 100,50,50); } for ( inti=0 ; i<10 ; i=i+1 ) { ellipse ( i*50, 100,50,50); } for ( inti=0 ; i<10 ; i=i+1 ) { ellipse ( i*50, 100,50,50); } for ( inti=0 ; i<10 ; i=i+1 ) { ellipse ( i*50, 100,50,50); } for ( inti=0 ; i<10 ; i=i+1 ) { ellipse ( i*50, 100,50,50); } for ( inti=0 ; i<10 ; i=i+1 ) { ellipse ( i*50, 450,50,50); } X10 X10
Looping So, for ( intj=0 ; j<10 ; j=j+1 ) { } for ( inti=0 ; i<10 ; i=i+1 ) { ellipse ( i*50 , j*50 , 50 , 50); } for ( inti=0 ; i<10 ; i=i+1 ) { ellipse ( i*50, 100,50,50); } for ( inti=0 ; i<10 ; i=i+1 ) { ellipse ( i*50, 100,50,50); } for ( inti=0 ; i<10 ; i=i+1 ) { ellipse ( i*50, 100,50,50); } for ( inti=0 ; i<10 ; i=i+1 ) { ellipse ( i*50, 100,50,50); } for ( inti=0 ; i<10 ; i=i+1 ) { ellipse ( i*50, 100,50,50); } for ( inti=0 ; i<10 ; i=i+1 ) { ellipse ( i*50, 100,50,50); } for ( inti=0 ; i<10 ; i=i+1 ) { ellipse ( i*50, 100,50,50); } for ( inti=0 ; i<10 ; i=i+1 ) { ellipse ( i*50, 100,50,50); } for ( inti=0 ; i<10 ; i=i+1 ) { ellipse ( i*50, 100,50,50); } for ( inti=0 ; i<10 ; i=i+1 ) { ellipse ( i*50, 450,50,50); } X10
Looping + Random Another example: Now, we use these five squares to tile the following graphic randomly.
Looping + Random How can we do that? • Looping • Loop to tile squares horizontally and vertically. • Random • Choose one color each time to tile.
Looping + Random • Let’s see how to do it: • First load five images: • img1, img2, img3, img4, img5 • Then start to tile the image: for (int x=50 ; x<600 ; x+=50) { for (int y=50 ; y<600 ; y+=50) {
Looping + Random • Randomly choose one image: • Tile that image: int ran = round(random(1, 5)); if (ran == 1) { image(img1, x, y); } if (ran == 2) { image(img2, x, y); } if (ran == 3) { image(img3, x, y); } if (ran == 4) { image(img4, x, y); } if (ran == 5) { image(img5, x, y); }