Lesson Notes 1X

— golan @ 2:20 pm

For Loop Example:

size (300, 300);
background (255,200,200);
strokeWeight (3);
stroke(0,0,0);

float foo = 164;
float mySpacing = 16;
int   nLines = 12;

for (int i=1; i<=nLines; i=i+1) {
  
  float greyVal = map(i, 1,nLines, 0,255); 
  stroke (greyVal);
  
  line (10, i*mySpacing, foo, i*mySpacing);
  ellipse (foo, i*mySpacing, 10,10);
}

For Loop with Random numbers:

size (300, 300);
background (255,200,200);
strokeWeight (2);

for (int i=0; i<10; i++) {
  float rx = i*30 + 15;
  float ry = random(140, 160);

  float rGray = random(0, 255); 
  fill(rGray); 
  ellipse (rx, ry, 40, 40) ;
}

Live interaction with iteration (for loop)

void setup() {
  size (400, 400);
}

void draw() {
  background(255, 200, 200);
  strokeWeight(2); 
  smooth( );

  for (int i=1; i<40; i++) {
    
    if (i == 17){ // equality test. Note the double-equals !!!!
      stroke(255,0,0);
      line (i*10, 0, mouseX, mouseY);
    } else {
      stroke (0,0,0);
      ellipse(i*10, mouseY, 10,10); 
    }
    
  }
}

Conditional testing: Equality testing in a for loop

void setup() {
  size (400, 400);
}

void draw() {
  background(255, 200, 200);
  strokeWeight(2); 
  smooth( );

  for (int i=1; i<40; i++) {
    
    if (i == 17){ // equality test. Note the double-equals !!!!
      stroke(255,0,0);
      line (i*10, 0, mouseX, mouseY);
    } else {
      stroke (0,0,0);
      ellipse(i*10, mouseY, 10,10); 
    }
    
  }
}

Embedded iteration

size (400, 400); 
background(255, 200, 200);
fill(255, 0, 0); 

for (int x=1; x<20; x=x+1) {
    for (int y=1; y<20; y=y+1) {  
      fill (x*20, y*20, 0);
      ellipse (x*20, y*20, 10, 10);  
    }
}

0 Comments »

No comments yet.

RSS feed for comments on this post.

Leave a comment

You must be logged in to post a comment.

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.
(c) 2019 CMU Electronic Media Studio II, Fall 2011, Section A | powered by WordPress with Barecity