CSc 101: Section 11 – More functions, boolean logic

In this section, you are going to work on a few problems that continue to give you practice using functions, as well as boolean expressions.

Problem 1

Write a function named drawCircle. This function will have a total of two `int` parameters. The function will draw a circle at this x/y position with a diameter of 70px. If the x value is less-than or equal-to the y value, the circle shall be yellow. Otherwise, it shall be blue. You may only use one if-statement (and no else!) For example, the below code will produce the canvas to the left once the function is created.
void setup() {
  size(300, 300); 
}
void draw() {
  drawCircle(100, 150); 
  drawCircle(202, 200);
  drawCircle(250, 75);
  drawCircle(40, 220);
}


Problem 2

Write a function named checkPointsSame. This function will have a total of four `int` parameters. The first two will be the x/y coordinates of a point on the canvas, the the latter two will be the x/y coordinates of another point. If the two coodinates are the exact same, you will draw a green square centered at that position. If they differ, you will draw two red circles, at each location separately. You may only use one if/else statement. For example, the below code will produce the canvas to the left once the function is created.
void setup() {
  size(300, 300); 
}
void draw() {
  checkPointsSame(53, 42, 95, 251);
  checkPointsSame(100, 100, 200, 200); 
  checkPointsSame(150, 200, 150, 200); 
  checkPointsSame(220, 50, 220, 50);
}


Problem 3

write a function named mouseRect. this function will have a total of four `int` parameters. the parameters will be the same as the normal processing rect function. the first two prameters will be the x and y coordinates, and the last two will be the width and height. the rectangle shall be red. however, this will be a "special" rectangles that has interactivity with the mouse. when the mouse is on top of it, it turns green. when the mouse clicks on it, it turns light-blue. you may only use two if-statements. no "else" or "else-if". For example, the below code will produce the canvas to the left once the function is created.
void setup() {
  size(300, 300); 
}
void draw() {
  mouserect(50, 20, 80, 120);
  mouserect(110, 160, 120, 100);
}


Problem 4

Write a function named twoRects. This function will have a total of eight `int` parameters. For reference, the function definition (not filled in with code) would look like so:
void twoRects(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2) {
}
The first four are the x, y, width, and height of one rectangle. The last for are the x, y, width, and height of a second rectangle. This function will draw both rectangles. If the rectangles overlap, they will both be colored blue. If not, one will be yellow and the other green. For example, the below code will produce the canvas to the left once the function is created.
void setup() {
  size(300, 300); 
}
void draw() {
  twoRects(50, 20, 80, 120, 150, 90, 120, 100);
  twoRects(10, 10, 50, 50, 30, 30, 50, 50);
  twoRects(30, 230, 70, 40, 50, 200, 120, 50);
}


If you complete all of the problems with time remaining, you can work on the homework!


Download Solutions