CIT020 Index > Functions

Functions

Part 1

Write the following functions:

drawBalloon

This void function draws a balloon. It takes the following parameters:

The first four parameters are float. The function draws an elliptical balloon centered at (xy) in a bounding box of size w by h, in the given color c. The function draws a string at the bottom of the balloon (using a black line()). The string starts at the center of the balloon at the bottom, and extends to the right one-third the width of the balloon and the line’s height is one-third the height of the balloon. The string is outside the balloon’s bounding box.

Creating a Color

In addition to data types such as int and float, Processing has a color data type. Once you create a variable of type color, you can use it anywhere that you would put a color number (for grayscale) or numbers (for RGB). Here is an example that creates and uses color variables.

void setup()
{
  size(200, 200);
  background(255);
  color grayscale = color(192); // 0=black, 255 = white
  color fullColor = color(255, 153, 255); // red, green, blue
  color trans = color(0, 255, 192, 128); // full color with transparency
  fill(grayscale);
  rect(50, 75, 50, 50);
  fill(fullColor);
  ellipse(150, 100, 40, 40);
  drawDiamond(120, 100, 40, trans); // you can use colors as parameters
}

void drawDiamond(int x, int y, int size, color c)
{
  fill(c);
  quad(x, y - size, x + size, y , x, y + size, x - size, y);
}

drawHouse

This void function draws a house with a door and window, given the following parameters, all of which are float:

The function draws a house with a triangular roof taking up the top one-third of the bounding box. The door is drawn 5/8 of the distance from the left. The door is 1/4 the height of the entire house and 3/16 the width of the house. The window is 1/8 the height of the entire house and 1/8 the width of the house. Its upper left corner is 1/8 the distance from the left and 1/8 the distance from the top of the rectangular portion of the house.

Hint: Make a variable named topY that holds the y-coordinate of the rectangular part of the house. This will make your code much easier.

Here is my sketch that I used to figure out the coordinates. It is not drawn to scale. Click the picture to see a larger version.

house showing coordinates

This browser does not have a Java Plug-in.
Get the latest Java Plug-in here.

Putting it Together

Write a program that will generate two houses of any size and shape that you feel appropriate in a 300 by 300 window. The houses will not move. The program will generate a balloon of random size and color; neither the size nor color of the balloon will change. The balloon will move smoothly around the window, bouncing off the corners. You may use random motion or a fixed speed. Use if statements to make sure the body of the balloon does not go outside the window (it is OK if the string leaves the window). Save this sketch with the name city.

Part 2

diagram showing streets and two points Processing already has a dist() function that calculates the straight-line distance between two points. Another way of measuring the distance is the “city block” distance. For example, if you are walking only along the streets, it is 5 blocks from point A to point B (3 blocks right and 2 blocks up). You will write a function to do this calculation.

cityBlockDistance()

This function calculates the “city block” distance between points (x1y1) and (x2y2). The function will take four integers to represent the coordinates of the two points and will return an integer giving the city block distance between those two points.

This browser does not have a Java Plug-in.
Get the latest Java Plug-in here.

Your cityBlockDistance() function must not use the built-in dist() function. Instead, it must calculate the city block distance by returning the absolute value of the difference of the x coordinates plus the absolute value of the difference of the y coordinates.

You will now write a program named blocks that uses this function. In a 100 by 100 window, draw a square that is 20 by 20. It will move at a uniform speed, bouncing off the sides of the window. You will also draw another 20 by 20 square that follows the mouse. Use rectMode(CENTER) for both squares; it makes the code easier.

Calculate the city block distance between the centers of the squares by calling the function that you wrote. If this distance is less than 20, color both squares red. If the distance is less than 50, color both squares yellow. Otherwise, color both squares white. Save this sketch with the name blocks.

When You Finish

Zip up both sketches into a zip file named functions.zip and upload it.