Consider the following problem: You want to copy an image in the top-left corner of an empty canvas. This might be part of a collage program or a family photo program that automatically combines For this problem, we need to consider two pictures. We'll call the one being drawn on targetPic and the one being copied pic. In-Class Problem: Below is a skeleton of the solution. Sketch out (don't use the compuer) the code within the nested loop. Hint: You'll need to call getPixel twice, getColor once, and setColor once: file= pickAFile() # here is where we should grab a pixel from pic and draw it on targetPicpic= makePicture(file) targetPic= makeEmptyPicture(300,300) # now draw pic in the top left corner of targetPic col=1 while col<getWidth(pic): row=1 while row<getHeight(pic): row=row+1 col=col+1 Now suppose we want to draw the picture on the canvas, but not beginning at coordinate (1,1) of the target, but at (100,100). How would we fix our code? The solution entails introducing the variables targetCol and targetRow. They will change along with col and row, but they will begin at 100,100. Instructor DEMO Debugging printNow('col:'+str(col)) Error Checking The code above will break if the inner picture is too big. How could we check to make sure this doesn't happen? How could we set things up so it doesn't happen? Instructor DEMO Picture Frame How is picture frame problem different from the samples in this lesson? |