Excercise 6 – 2

Using a FOR Loop to draw a series of lines

code


size(200,200);
background(255);

for (int y = 0; y <= height; y+=10) // this for loop initialized y as 0 // and as long as y is less than or equal to the height // it increments y by 10 pixels on each cycle through the "for" loop { stroke(0); // set stroke to black line(0,y,width,y); // make a line that is the width of the canvas print("cycle "); // print cycle below so we can see what's happening } // end of "for" loop, then go and cycle again until the conditions change

Excercise 6-8

Code


int sqWidth = 20; // dims of the square

// row stuff
int y = 0; // row

// colors
float R = 0;
float G = 0;
float B = 0;

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

void draw() {
for (int rowNum=0; rowNum < height; rowNum+=sqWidth) { y=rowNum; for (int sqStart=0; sqStart < width; sqStart+=sqWidth) { noStroke(); // pick some random colors R = random(0,255); G = random(0,255); B = random(0,255); fill(R,G,B); rect(sqStart,y,sqWidth,y+sqWidth); } } }

Snowbot

The Code


// declare variables
//colors
float r;
float g;
float b;
float a;

//construction
float circW = width/2;
float circH = height;
float x;
float y;

void setup() {
rectMode(CENTER);
size(400,400);
background(200);
smooth();
frameRate(30);
}
void draw() {

// snow
for (int i=0; i<400; i++) { noStroke(); // pick a random color r = random(240,255); g = random(240,255); b = random(240,255); a = random(20,255); circW = random(1,3); circH = random(1,3); x = random(width); y = random(height); // create a random colored line of random dimensions fill(r,g,b,a); ellipse(x,y,circW,circH); } } // if the mouse is pressed, redraw the background void mousePressed() { // body and arms stroke(255); fill(150); // make it grey rect(mouseX,mouseY,50,30); //body is 50x30 ellipse(mouseX-30,mouseY-5,10,10); ellipse(mouseX+30,mouseY-5,10,10); // legs rect(mouseX-10,mouseY+20,10,10); rect(mouseX+10,mouseY+20,10,10); // head rect(mouseX,mouseY-22.5,40,15); // head is 40x15 // eyes fill(255); // make the eyes white ellipse(mouseX-9,mouseY-24.5,5,5); ellipse(mouseX+9,mouseY-24.5,5,5); // mouth rect(mouseX,mouseY-18.5,20,3); }