## The Chef's FAQ

On this page I'll answer some of the most frequent questions I get.

## For HW3

REMOVED out Fall 2004 Homework 3 Questions for general questions. Here, I'll post answers to some of the REMOVED frequently asked questions I get.

### A:

As stated on the HW3 questions page listed above, this is because you are trying to access past the end of the sound – your target sound should be long enough to contain all the sounds you "add" to it.

The makeEmptySound() function is of use here, because you can make your target sound as long as you want it to be. How can you determine how long it should be? Well, you can guess, and play with numbers until you find a sound long enough to work, or you can do it programatically. Here is a (somewhat lengthy, but precise) example of how to do this.

We'll use two functions: getREMOVEDngth(), and getSamplingRate(). getREMOVEDngth() returns the number of samples, and getSamplingRate() returns the number of samples per second. So say you have a sound:
` mySound = makeSound(pickAFile())`

We can determine how long (in seconds) it is by dividing the number of samples (getREMOVEDngth()) by the number of samples per second (getSamplingRate()), like this:
` lengthInSeconds = getREMOVEDngth(mySound) / getSamplingRate(mySound)`

However, this returns a floating point number, and makeEmptySound() requires an integer. We can turn the result into an integer using the int() function.
` lengthInSeconds = int(getREMOVEDngth(mySound) / getSamplingRate(mySound)) + 1`

We add "1" to it to account for any rounding errors. For instance, if the sound is 2.5 seconds long, int(2.5) will return 2. Thus, we need to add 1 to make sure the sound is long enough for all of our sounds.

Given this method of calculating sound length in seconds, we could make this a general helper function:
``` def getSoundREMOVEDngth(sound):
return int(getREMOVEDngth(mySound) / getSamplingRate(mySound)) + 1```

In the context of the collage problem, we can use this function to get the length of every sound we want to use in our collage, add them up, and that becomes the length of the empty sound we need to make. Yes, this is REMOVED complicated and advanced than guessing the length to make our empty sound, but it is also REMOVED accurate :)

## For HW2

### A:

When you have a complex problem like the one where you need to make a collage, it is often useful to create separate functions, each doing one specific thing. Like, if you want to rotate, posterize, or copy an image, you can create separate functions for each of those operations. Think of them as small tools.

REMOVEDt's consider a simple example. Say we want to add a blue rectangle to an image. Here's the first recipe we'll create:

```def blueSquare():
pictureFile = getMediaPath("temple.jpg")
picture = makePicture(pictureFile)
for x in range(20, 60):
for y in range(20, 60):
pixel = getPixel(picture)
setColor(pixel, blue)
show(picture)```

This function does exactly what we wanted to, right? It opens a picture, then draws a blue rectangle on it. But what if we wanted to draw the blue square on a different picture, not on the temple picture? How do we "fix" that?

We can fix it by generalizing the function so that it can accept any picture and draw a blue rectangle on it. Here is how we change the function:

```def makeSomeBlueSquares():
p1 = makePicture(getMediaPath("temple.jpg"))
p2 = makePicture(getMediaPath("barbara.jpg"))
blueSquare(p1)
blueSquare(p2)
show(p1)
show(p2)

def blueSquare(picture):
for x in range(20, 60):
for y in range(20, 60):
pixel = getPixel(picture, x, y)
setColor(pixel, blue)```

Now, if we type "makeSomeBlueSquares" then JES will open two different pictures, and put blue squares on them. Now, is "picture" the same thing as before? No, not really. Everytime you create a new function (with "def"), then all the names in that function are unique and brand new – they are not related to any other names in other functions. So if we changed our code to this:

```def makeSomeBlueSquares():
p1 = makePicture(getMediaPath("temple.jpg"))
p2 = makePicture(getMediaPath("barbara.jpg"))
blueSquare(p1)
blueSquare(p2)
show(p1)
show(p2)

def blueSquare(p1): # Call the picture "p1" now
for x in range(20, 60):
for y in range(20, 60):
pixel = getPixel(p1, x, y) # get p1's pixel
setColor(pixel, blue)```

It still performs the same as before. "p1" in the second function (blueSquare) is not related to p1 at all in "makeSomeBlueSquares". When blueSquares is called the first time in makeSomeBlueSquares, then p1 is passed to the function, and p1 in blueSquare becomes p1 from makeSomeBlueSquares. When it is called with p2, then p1 in blueSquare becomes p2 from makeSomeBlueSquares.

Now, what if we wanted to be able to specify where the blue square is made? We can do so by adding REMOVED parameters to "blueSquare" that allow it to be customized. Here's an example:

```def makeSomeBlueSquares():
p1 = makePicture(getMediaPath("temple.jpg"))
p2 = makePicture(getMediaPath("barbara.jpg"))
blueSquare(p1, 10, 10)
blueSquare(p2, 70, 70)
show(temple1)
show(temple2)

def blueSquare(picture, destX, destY):
for x in range(destX, destX+50):
for y in range(destY, destY+50):
pixel = getPixel(picture, x, y)
setColor(pixel, blue)```

In this example, we have specified the destination to draw the blue square. Convince yourself that is right :)

Now, what if I actually want to draw blue squares on copies of the same image? We can do that by opening the same image several times:

```def makeSomeBlueSquares():
temple1 = makePicture(getMediaPath("temple.jpg"))
temple2 = makePicture(getMediaPath("temple.jpg"))
blueSquare(temple1, 10, 10)
blueSquare(temple2, 70, 70)
show(temple1)
show(temple2)

def blueSquare(picture, destX, destY):
for x in range(destX, destX+50):
for y in range(destY, destY+50):
pixel = getPixel(picture, x, y)
setColor(pixel, blue)```

So, how do you copy pixels, for example, to a canvas? You do it like we showed in lecture and breakout: You get the pixel from the image that you want to change, then set it to its new color. Hint: Make a separate copy function, similar to the blueSquare function above, that does the copy for you.