WEEK 5: Tut 7, 8

by Sarah Shultz

Tut 7: Drawing with Functions

  1. Using Functions
  • Show Sketch
/** @peep sketchcode */
size(200, 200); 
smooth(); 
fill(0); 
face(random(5,width), random(5,height), random(10,50)); 
 
void face(int x, int y, int gap) { 
  line(x, 0, x, y);               // Nose Bridge 
  line(x, y, x+gap, y);           // Nose 
  line(x+gap, y, x+gap, height); 
  int mouthY = (height+y)/2;
  line(x, mouthY, x+gap, mouthY); // Mouth 
  ellipse(x-gap/2, y/2, 5, 5);    // Left eye 
  ellipse(x+gap, y/2, 5, 5);      // Right eye 
}
  1. Adding Parameters: added parameters to control the number of poitns on a star
  • Show Sketch
/** @peep sketchcode */
 
size(200, 200);
background(204);
strokeWeight(4);
star(100, 100, 20, 40, random(1,6));
star(30, 80, 20, 30, random(1,6));
star(150, 50, 15, 40, random(1,6));
 
void star(float x, float y, float inner, float outer, int points) {
  // Define some variables to use to draw a star
 
  // Draw the star shape... 
  beginShape();
  float delta = radians(360/points);
  for (int i = 0; i < points; i++) {
    // Draw the outer point
    float ox = x + outer * cos(i*delta);
    float oy = y + outer * sin(i*delta);
    vertex(ox, oy);
    // Draw the inner point
    float ix = x + inner * cos(i*delta + delta/2);
    float iy = y + inner * sin(i*delta + delta/2);
    vertex(ix, iy);
  }
  endShape(CLOSE);
}
  1. Functions calling functions: Random colours, number of faces, and background colour
  • Show Code
/** @peep sketch */
void setup() {
  size(200, 200);
  background(random(1,255));
  crowd(20, 20, width-40, height-40, random(40,60));
}
 
void crowd(int crowdX, int crowdY, int crowdWidth, int crowdHeight, int numFaces) {
  for (int f = 0; f < numFaces; f++) {
    int faceWidth = int(random(crowdWidth/12, crowdWidth/6));
    int faceHeight = int(random(crowdHeight/12, crowdHeight/6));
    int faceX = int(random(crowdX, crowdX+crowdWidth)) - faceWidth/2;
    int faceY = int(random(crowdY, crowdY+crowdHeight)) - faceHeight/2;
    int noseX = int(random(faceWidth/8, 7*faceWidth/8));
    int noseY = int(random(faceHeight/4, 3*faceHeight/4));
    int noseWidth = int(random(faceWidth/6, faceWidth/3));
    face(faceX, faceY, faceWidth, faceHeight, noseX, noseY, noseWidth);
  }
}
 
 
void face(int faceX, int faceY, int faceWidth, int faceHeight, int noseX, int noseY, int noseWidth) { 
  fill(random(1,255), random(1,255), random(1,255));
  rect(faceX, faceY, faceWidth, faceHeight);
  line(faceX+noseX, faceY, faceX+noseX, faceY+noseY); // Nose Bridge 
  line(faceX+noseX, faceY+noseY, faceX+noseX+noseWidth, faceY+noseY); // Nose 
  line(faceX+noseX+noseWidth, faceY+noseY, faceX+noseX+noseWidth, faceY+faceHeight); 
  int mouthY = (faceHeight+noseY)/2;
  int mouseWidth = noseWidth;
  line(faceX+noseX, faceY+mouthY, faceX+noseX+noseWidth, faceY+mouthY); // Mouth 
  int eyeY = noseY/2;
  int eyeSeparation = noseWidth/2;
  fill(0);
  ellipse(faceX+noseX-eyeSeparation, faceY+eyeY, faceWidth/20, faceWidth/20); // Left eye 
  ellipse(faceX+noseX+eyeSeparation, faceY+eyeY, faceWidth/20, faceWidth/20); // Right eye 
}
  1. Functions calling themselves: added randoum colour and opacity, and took out the stroke to match the tuts
  • Show Code
/** @peep sketch */
 
void setup() {
  size(200, 200);
  background(204);
  for (int i = 0; i < 50; i++) {
    pushMatrix();
    translate(random(width), random(height));
    float scaleFactor = random(0.15, 0.25);
    scale(scaleFactor);
    strokeWeight(1.0/scaleFactor);
    rotate(radians(random(-10, 10)));
    int fw = random(140, 200);
    int fh = random(180, 200);
    int fx = random(fw/6, 5*fw/6);
    int fy = random(fh/6, 4*fh/6);
    int fgap = random((fw-fx)/5, (fw-fx)/3);
    face(fw, fh, fx, fy, fgap);
    popMatrix();
  }
}
 
void face(int w, int h, int x, int y, int gap) {
  pushStyle();
  pushMatrix();
  translate(-w/2, -h/2);
  fill(random(0,255),random(0,255),random(0,255),random(100,255));
  rect(0, 0, w, h);
  line(x, 0, x, y);               // Nose Bridge 
  line(x, y, x+gap, y);           // Nose 
  line(x+gap, y, x+gap, h); 
  int mouthY = (h+y)/2;
  line(x, mouthY, x+gap, mouthY); // Mouth 
  fill(0);
  ellipse(x-gap/2, y/2, 5, 5);    // Left eye 
  ellipse(x+gap, y/2, 5, 5);      // Right eye 
  popMatrix();
  popStyle();
}

Comments

Nobody has said anything yet.