Tut­o­r­i­a­l 7: Dra­w­i­n­g with Fun­c­t­i­o­n­s

by Lisa Deng
  • Show Sketch
/** @peep sketchcode */
 
size(200, 200); 
smooth(); 
fill(0); 
face(width/5,height*4/5, width/4); 
 
void face(int x, int y, int gap) { 
  int x=random(width/2);
  int y=random(y*4/5);
  int gap=random(width/3); 
  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 
}

Comments

    Lisa Deng about a year ago
    • Show Code
    /** @peep sketch */
     
    // Define some variables to use to draw a star
    size(200, 200);
    background(204);
    fill(255);
    stroke(0);
    strokeWeight(3);
    star();
     
    //float x=100; float y= 100
    void star(float x, float y, float inner, float outer) { 
      //the parameters inside the brackets have to be declared when function is called
      //SETTING VARIABLES
      int points = 5;
      //default variable values
      /*float x = 100;
      float y = 100;
      float inner = 40;
      float outer = 80;
      int points = 5;*/
      // 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);
    }
     
    //CALL STAR!!!!
    //star(20,20,5,8);
     
    for (y = 20; y < height; y += 20) { 
      for (x = 20; x < width; x += 20) { 
        strokeWeight(1);
        star(x, y, 4, 8); 
      } 
    }
    Lisa Deng about a year ago
    • Show Sketch
    /** @peep sketchcode */
     
    size(200, 200); 
    smooth(); 
    fill(0); 
    crowd (20,20, width-30, height-40,5);
     
     
    void face(int faceX, int faceY, int faceWidth, int faceHeight, int noseX, int noseY, int noseWidth) { 
      fill(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, 5, 5); // Left eye 
      ellipse(faceX+noseX+eyeSeparation, faceY+eyeY, 5, 5); // Right eye  
    }
     
    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);
      }
    }
    Last Updated: 23-Mar-2016 12:53 PM
    Lisa Deng about a year ago
    • Show Code
    /** @peep sketch */
    size(200, 200);
    squareRecursion(0, 0, width, height, random(255), random(255), random(255));
     
    void squareRecursion(float x, float y, float w, float h, int r, int g, int bl) {
      // Draw a rectangle
      noStroke(0);
      rect(x, y, w, h);
     
      // Test to see if we should recurse...
      if ((random(100) < 80) && (w > 10 || h > 10)) {
        // Set a border to go around sub-rectangles
        float bx = 3;
        float by = 3;
        // Calculatue the width and height of sub-rectangles
        w = (w - 3*bx) / 2;
        h = (h - 3*by) / 2;
        // Call the same function to draw the sub-rectangles
        squareRecursion(x + bx, y + by, w, h);
        noStroke()
        fill(random(50,200),random(50,230),random(50,225),random(50,100))
        squareRecursion(x + bx + w + bx, y + by, w, h);
        squareRecursion(x + bx, y + by + h + by, w, h);
        squareRecursion(x + bx + w + bx, y + by + h + by, w, h);
      }
    }