CPSC 120: Lab Assignment 6

Due by 11:59 pm on Friday, 04/07/2017

Home | Calendar | Announcements | Assignments | Example Code | Notes

Overview

The goal of this assignment is to solidify your skill with defining methods and effectively using calls to those methods in various contexts. You will construct a surprisingly substantial program, based on the bubbles.pde program from the Example Code and the two sketches your made for Lab 3.

Begin by studying the Tutorial on Method Definitions, which you'll find in the Notes section of our class website. It will give you the framework you need to successfully complete the exercises.

Your Job

Begin with this sketch, which draws a large, randomly-chose number of bubbles that fill the screen:

void setup() {
  size(800, 800);
  background(0);
  frameRate(20);
  
  //randomSeed(42);  // uncomment this line:  what happens?

  int minSz = (int) random(1, 50);
  int maxSz = (int) random(minSz, 200);

  int small = (int) random(1, 50);
  int large = (int) random(small, 100);

  int numBubbles = (int) random(50, 1000);
  float probA = 0.3;
  int minR = small;
  int maxR = large;

  int rA = 255, gA = 0, bA = 0;  // "color A"
  int rB = 0, gB = 0, bB = 255;  // "color B"

  int i = 0;
  while (i < numBubbles) {
    int transp = (int) random(50, 230);

    // Bernoulli trial:  we first "flip a coin"
    // If probA is 0.5, it's just like a regular coin flip
    // Otherwise, the result is biased according to probA
    float p = random(0, 1);
    if (p < probA) {          
      fill(rA, gA, bA, transp);
    } 
    else {
      fill(rB, gB, bB, transp);
    }

    // Now generate (x,y) coordinates and a radius for this bubble
    int x = (int) random(0, width);
    int y = (int) random(0, height);
    int rad = (int) random(minR, maxR);

    ellipse(x, y, 2*rad, 2*rad);

    i = i + 1;
  } // end of while loop
}

You are to write a program with a method: bubbles. This method should take as parameters four int values. A call to bubbles(x1,y1,x2,y2) draws your the bubbles picture given by the code above, but in the rectangle bounded by (x1,y1) and (x2,y2), with all components drawn proportional to this rectangle.

Save your sketch in the lab6 folder of your turn-in directory.


Turn In

Make a folder in your turn in directory named "lab6". Copy both of your solutions in to this folder. Consult the Linux tutorial from Lab #0 if you need a reminder of the details.


Standards

John H. E. Lasseter