Processing=003=for loop

Preview:

DESCRIPTION

A_Little_Bit_Processing_Tutorial

Citation preview

Processing

For_Loop

03迴圈

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;

}What we

gonna use

A LOT!!!

Void

draw()

is a

FOR

LOOP

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

INCEPTION

For_Loop迴圈

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

open

close

condition

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 “0” point(0,250);

Next time “0 + 25” point(25,250);

Next time “0+25+25” point(50,250);

Next time “0+25+25+25” 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 “0” point(0,0);

Next time “0 + 25” point(25,25);

Next time “0+25+25” point(50,50);

Next time “0+25+25+25” 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

strokeWeight with i

用 i 改變筆寬何如?

Nested For_Loop很髒的迴圈中的迴圈

For loopInsideFor 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 “0” ellipse(0,10,20,20);

Next time “0 + 20” ellipse (20,10,20,20);

Next time “0+20+20” ellipse(40,10,20,20);

Next time “0+20+20+20” ellipse(60,10,20,20);

…. …. ….

Next time “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” ellipse(0, 0, 20,20);

“0 + 20” ellipse (0, 20, 20,20);

“0+20+20” ellipse(0, 40, 20,20);

“0+20+20+20” ellipse(0, 60, 20,20);

…… …..

“0+20+20+20” ellipse(0, 480, 20,20);

Next time “i+20=20”

j starts from “0” ellipse(20, 0, 20,20);

“0 + 20” ellipse (20, 20, 20,20);

“0+20+20” ellipse(20, 40, 20,20);

…… …..

“0+20+20+20” ellipse(20, 480, 20,20);

……………………………….. ………………………..

Next time “i+20+….20=480”

j starts from “0” ellipse(480, 0, 20,20);

“0 + 20” ellipse (480, 20, 20,20);

…… …..

“0+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);

}

}

}

Recommended