|
CS 100 Spring 2014
Lab 5 |
More on Conditionals
|
Due Monday March, 31th
Using the Processing language/environment answer the following questions.
1. [4 pts] Consider this new version of the snow flakes program where the flakes fall within two vertical strips. Before running the program
- read carefully the loop execution and
- draw on paper the two regions in which the flakes will appear due to the use of the conditional structures.
size(300, 200);
println("w : " + width + " h: " + height);
background(200);
int snowFlakeSize = 5;
smooth();
noStroke();
float xVal = 0;
float yVal = 0;
float sizeNoise = 0;
for (int i=0; i<400; i=i+1) {
xVal = random(width);
yVal = random(height);
sizeNoise = random(5);
if (xVal>50) {
if (xVal<100) {
ellipse(xVal, yVal,
snowFlakeSize+sizeNoise, snowFlakeSize+sizeNoise);
}
}
if (xVal>200) {
if (xVal<250) {
ellipse(xVal, yVal,
snowFlakeSize+sizeNoise, snowFlakeSize+sizeNoise);
}
}
println("i = " + i + " xVal " + xVal + " yVal " + yVal);
}
Now paste the code in a file constrainedSnowFlakes.pde and do the following.
- Run the program many times and check if the output corresponds to your prediction.
- Add two function calls so that one strip contains red filled flakes while the other uses green.
- Read and study the Processing reference for && (logical AND): make sure you understand the example code. If you have any question, please ask the course staff.
- Change each of the two nested
ifs (one at the time) to use
a singleifwith two conditions using an AND logical operator, i.e.&&.
Submit this code version.
2. [2 pts] Read this alternate program, which instead uses
the random function passing
two arguments, low and high, in between each the random number is generated.
Answer the following questions in the comment box when you submit the lab
- Is
constrainedSnowFlakesB.pdesimilar, i.e. the snow flakes are constrained in the same regions, toconstrainedSnowFlakes.pde? - Which of the two programs draw more snow flakes? Why?
3. [4 pts] Write a program horizontallyConstrainedSnowFlakes.pde that creates an output similar to this image.
Hint: Within a window 300x200 the snow is either
- between the horizontal 20 to 80 or
- between 120 to 180
Bonus
Please read the hints at the bottom of the page.
- Write a program that constrains dots of random colors and sizes to be
placed at random locations within a rectangle as shown below.
- Write a program that constrains dots of random colors and sizes
to be placed at random locations within a circle as shown below.
Note:
- It is fine if dots touch the boundary.
- In order to constrain the random placement to be within the circle Pythagorean theorem is required this time...
- The function
sqrtevaluates the square root of the float argument passed to it.