For loops, integers & condition experiments

For loops are a repetition function I leant from the plethora tutorials found at http://www.plethora-project.com/education/2011/09/12/processing-tutorials/

A for loop is a structure that allows you to repeat a command any number of times.

Screen Shot 2016-02-01 at 19.11.11

You can ‘nest’ a for loop in another one to create a grid like repeat pattern.

Screen Shot 2016-02-01 at 19.09.26.png

I experimented with this new technique along with my previously learnt knowledge of integers and colours.

 

int x = 0;
int y = 0;
void setup() {
size(600, 600);
 smooth();
}
void draw() {
background(255,204,204);
x++; //this part is shorthand for writing x = x + 1;
y++;
 for (int i = 0; i <50; i++) {
 for (int j = 0; j <100; j++) { //these are the for loop parts which 
                                  create the repeat pattern by adding
                                  on 1 to the integers i & j  
fill(255,204,255);
 stroke(204,153,255);
 ellipse(i * 20, j * 20, x, y);
 }
 }
}

In this experiment it adds a value of 1 each frame which creates some cool patterns however it will keep adding it on forever so I wanted to see if I could make it stop and start minusing 1. For this I looked into condition ‘if’ statements.

int x = 0;
int y = 0;
void setup() {
size(600, 600);
 smooth();
}
void draw() {
background(255, 204, 204);
 x++;
 y++;
if (x>40) {
 x= x * -1;
 }
if (y>40) {
 y= y * -1;        //this if statement means that once x and y have
 }                   reached 40 it will start multiplying them by -1
                     which means it will get smaller.
                     
 for (int i = 0; i <50; i++) {
 for (int j = 0; j <100; j++) {
 fill(100, 204, 255);
 stroke(204, 50, 255);
 ellipse(i * 20, j * 20, x, y);
 }
 }
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s