Processing=003=for loop

Page 1

Processing

03 For_Loop 迴圈


For_Loop 迴圈


Moving


int x=250; int y=250; int speed=2; void setup(){ size(500,500); smooth(); } void draw(){ background(0); noStroke(); ellipse(x,y,40,40); x=x+speed; }

Void draw() is a FOR LOOP What we gonna use A LOT!!!


If you are in the condition, you are in the loop 如果你在條件中 你就在輪迴中


INCEPTION




For_Loop 迴圈


condition

for (start;costrain;step){ Do the thing; } close

open


It is easier to code than to talk this time 寫的比說的簡單


void setup(){ size(500,500); smooth(); } void draw(){ background(0); noStroke();

for (int i =0; i<500; i=i+25){ stroke(255); strokeWeight(20); point( i, 250); } } i starts from Next time Next time Next time

“0” “0 + 25” “0+25+25” “0+25+25+25”

point(0,250); point(25,250); point(50,250); point(75,250);



void setup(){ size(500,500); smooth(); } void draw(){ background(0); noStroke();

for (int i =0; i<500; i=i+25){ stroke(255); strokeWeight(20); point( i, i); } } i starts from Next time Next time Next time

“0” “0 + 25” “0+25+25” “0+25+25+25”

point(0,0); point(25,25); point(50,50); point(75,75);



void setup(){ size(500,500); smooth(); } void draw(){ background(0); stroke(255); strokeWeight(2);

for (int i =0; i<500; i=i+25){ line( i, 0, i, 500); } } Let’s draw lines line(x1,y1,x2,y2); 畫線八



How to do this? Use For_Loop Twice

如何用兩次迴圈 來完成


void setup(){ size(500,500); smooth(); } void draw(){ background(0); stroke(255);

for (int i =0; i<500; i=i+25){ strokeWeight(i/50); line( i, 0, i, 500); } for (int i =0; i<500; i=i+25){ strokeWeight(i/50); line( 0, i, 500, i); } } How about control the 用 i 改變筆寬 strokeWeight with i 何如?



Nested For_Loop 很髒的迴圈中的迴圈


For loop Inside For loop


void setup(){ size(500,500); background(0); smooth(); } void draw(){ for(int i =0; i<=500; i=i+20){ ellipse( i , 10 ,20,20); } }


Imagine there is a TYPEWRITER

想像有台 打字機


OK, PLOTTER

抱歉 列印機


i starts from Next time Next time Next time …. Next time

“0” ellipse(0,10,20,20); “0 + 20” ellipse (20,10,20,20); “0+20+20” ellipse(40,10,20,20); “0+20+20+20” ellipse(60,10,20,20); …. …. “0+20+20…+20” ellipse(500,10,20,20);


void setup(){ size(500,500); smooth(); } void draw(){ background(0); for(int i =0; i<500; i=i+20){ for (int j=0; j<500; j=j+20){ ellipse( i , j ,20,20); } } }



i starts from “0” j starts from “0” “0 + 20” “0+20+20” “0+20+20+20” …… “0+20+20+20”

ellipse(0, 0, 20,20); ellipse (0, 20, 20,20); ellipse(0, 40, 20,20); ellipse(0, 60, 20,20); ….. ellipse(0, 480, 20,20);

Next time “i+20=20” j starts from “0” “0 + 20” “0+20+20” …… “0+20+20+20” ………………………………..

ellipse(20, 0, 20,20); ellipse (20, 20, 20,20); ellipse(20, 40, 20,20); ….. ellipse(20, 480, 20,20); ………………………..

Next time “i+20+….20=480” j starts from “0” “0 + 20” …… “0+20+20+20”

ellipse(480, 0, 20,20); ellipse (480, 20, 20,20); ….. ellipse(480, 480, 20,20);


void setup(){ size(500,500); smooth(); } void draw(){ background(0); for(int i =0; i<500; i=i+20){ for (int j=0; j<500; j=j+20){ ellipse( i+10 , j+10 ,20,20); } } }


rect( locX, locY,w,h);


void setup(){ size(500,500); smooth(); } void draw(){ background(0); strokeWeight(5); for(int i =0; i<500; i=i+20){ for (int j=0; j<500; j=j+20){ rect( i , j ,20,20); } } }


void setup(){ size(500,500); smooth(); } void draw(){ background(0); strokeWeight(5); for(int i =0; i<500; i=i+20){ for (int j=0; j<500; j=j+20){ fill( i , j , i+j /10); rect( i , j ,20,20); } } }


void setup(){ size(500,500); smooth(); } void draw(){ background(0); strokeWeight(5); for(int i =0; i<500; i=i+20){ for (int j=0; j<500; j=j+20){ fill(mouseX,mouseY,(i+j)/10); rect( i , j ,20,20); } } }



5 / 3 = 1 ….2 5%3=2


void setup(){ size(500,500); smooth(); } void draw(){ background(0); strokeWeight(5); for(int i =0; i<500; i=i+20){ for (int j=0; j<500; j=j+20){ fill( i , j , i+j /10); if( (i+j) % 3 ==1){ rect( i , j ,20,20); } else{ ellipse ( i +10, j+10 ,20,20); } } } }


Random!!! 亂數


random(min,max)


void setup(){ size(500,500); smooth(); noLoop();//stop looping the code } void draw(){ background(0); strokeWeight(5); for(int i =0; i<500; i=i+20){ for (int j=0; j<500; j=j+20){ fill(random(0,255) ,0 ,0); rect( i , j ,20,20); } } }


void setup(){ size(500,500); smooth(); } void draw(){ background(0); for(int i =0; i<500; i=i+50){ for (int j=0; j<500; j=j+50){ stroke(255); line(random(0,width),0,random(0,width),height); } } }



Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.