CPSC 120: Lab Assignment 2

Due at 2:00 pm on Thursday, 02/13/2014

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

Overview

This assignment has three goals:

Reading

Exercises:

Your solutions to the following problems should be placed in a folder named "lab2". When you have finished the assignment, copy this folder and its contents to your turn-in directory.

  1. Consider the following drawing:
    void setup() {
      size(500,400);          
      background(187,100,75); 
      
      strokeWeight(10);     
    
      point(450,50);
      point(437,68);
      point(424,86);
      point(411,104);
      point(398,122);
      point(385,140);
      point(372,158);
      point(359,176);
      point(346,194);
      point(333,212);
    }
    

    Write a version of this program that draws exactly the same thing, using a loop for the points.

  2. How many times does a loop run? What effect will each iteration of the loop have? In this exercise you'll gain practice with direct analysis of code, in order to better answer such questions. We learned how to trace a program's execution in Monday's class (February 3rd): write down each variable and its current value. Then mark the changes to variables as each statement executes. For example, with the loop
  3. int x = 50;
    int i = 0;
    
    while (i < 5) {
       point(x,100);
       x = x + 50;
       i = i + 1;
    }
    

    our trace would look like this:

    x: 50 100 150 200 250  300
    i: 0 1 2 3 4 5
    

    This process allows us to visualize program execution as it happens, seeing, for example that point will be drawn from left to right, and that the loop runs exactly 5 times.

    Trace the following program, and draw what you think will be displayed when it is run. Do not run the program until you've completed this! If you find that what you expect is not what you observe, don't throw away the failed attempt! Rather start over, with the corrections you need to make, and turn in both traces, numbering them in the order of attempt.

    int d = 32;
    int x = 50;
    int y = 50;
    int dx = 40;
    int dy = 40;
    stroke(0,0,255);
    
    int j = 0;
    
    while (j < (d / 4) - 1) {
       point(x,y);
       
       if (j > 3) {
          stroke(255,0,0);
          dy = -40;
       }
       x = x + dx;
       y = y + dy;
       j = j + 1;
    }
    
  4. Consider the stipple_line4 examples from class. Modify the program to draw two additional lines, one from (33, 330) to (150,50) and the other from (23, 34) to (303, 255).
  5. Modify the makeLine method so that it draws a line with 20 dots, instead of 5.
  6. Modify the both the definition of makeLine and the calls to it in setup() method so that the number of dots used to draw a line can be given as an argument. For example, if we want a 34-dot line, we could use the call
    makeLine(50,400,250,300,34);

Turn In

Make a folder in your turn in directory named "lab2". Copy all five 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