# CSC111 Homework 3 Solution

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

--Thiebaut 15:50, 18 February 2009 (UTC)

# DT's Solution

Solution to the last problem of Homework #3

```# roseAndHeart.py # Solution program for HW #3 # D. Thiebaut # this program takes the picture of a heart and copies it # inside another picture, that of a rose, that is bigger in # dimension. The copy operation takes place in all 4 corners, # and the heart is flipped around every time so that its pointy # side faces the inside of the picture. def copyTo( fromPic, toPic ): # copies all the pixels from the fromPic picture and put # then AT THE SAME LOCATION in the toPic Picture for pixel in getPixels( fromPic ): x = getX( pixel ) y = getY( pixel ) col = getColor( pixel ) toPixel = getPixel( toPic, x, y ) setColor( toPixel, col ) def copyToOffset( fromPic, toPic, offsetX, offsetY ): # copies all the pixels from the fromPic picture and put # then AT THE SAME LOCATION in the toPic Picture for pixel in getPixels( fromPic ): x = getX( pixel ) y = getY( pixel ) col = getColor( pixel ) toPixel = getPixel( toPic, x+offsetX, y+offsetY ) setColor( toPixel, col ) def flipVertical( pict ): # flips the picture received along a horizontal axis height = getHeight( pict ) for x in range( 1, getWidth( pict )+1 ): for y in range( 1, height/2 + 1 ): topPixel = getPixel( pict, x, y ) bottomPixel = getPixel( pict, x, height-y+1 ) topColor = getColor( topPixel ) bottomColor = getColor (bottomPixel ) setColor( topPixel, bottomColor ) setColor( bottomPixel, topColor ) def flipHorizontal( pict ): # flips the picture received along a vertical axis width = getWidth( pict ) for y in range( 1, getHeight( pict )+1 ): for x in range( 1, width/2 + 1 ): leftPixel = getPixel( pict, x, y ) rightPixel = getPixel( pict, width-x+1, y ) leftColor = getColor( leftPixel ) rightColor = getColor( rightPixel ) setColor( leftPixel, rightColor ) setColor( rightPixel, leftColor ) def main(): # sets the media path and get the two pictures heartPic = makePicture( pickAFile() ) rosePic = makePicture( pickAFile() ) # copy the heart picture onto the rose picture copyToOffset( heartPic, rosePic, 10, 10 ) flipVertical( heartPic ) copyToOffset( heartPic, rosePic, 10, getHeight( rosePic )-getHeight( heartPic )-10 ) flipHorizontal( heartPic ) copyToOffset( heartPic, rosePic, getWidth( rosePic )-getWidth( heartPic )-10, getHeight( rosePic )-getHeight( heartPic )-10 ) flipVertical( heartPic ) copyToOffset( heartPic, rosePic, getWidth( rosePic )-getWidth( heartPic )-10, 10 ) # show the result, and save to a file show( rosePic ) writePictureTo( rosePic, "roseAndHearts.jpg" ) ```

# Sarah's Solution

Another good program from the batch of submitted programs.

```#SMorseHW3b.py #Sarah Morse #CSC 111 #This program transfers the little heart picture onto the four # corners of the rose picture, with all the points facing inward. def flip( heart ): #this function flips the heart vertically. height = getHeight( heart ) for x in range( 1, getWidth( heart )+1 ): for y in range( 1, height/2 + 1 ): topPixel = getPixel( heart, x, y ) bottomPixel = getPixel( heart, x, height-y+1 ) topColor = getColor( topPixel ) bottomColor = getColor (bottomPixel ) setColor( topPixel, bottomColor ) setColor( bottomPixel, topColor ) def flop( heart ): #this function flips the heart horizontally. w = getWidth( heart ) h = getHeight( heart) for x in range( 1, w/2+1 ): for y in range( 1, h+1 ): leftPixel = getPixel( heart, x, y ) rightPixel = getPixel( heart, w-x, y ) leftColor = getColor( leftPixel ) rightColor = getColor (rightPixel ) setColor( leftPixel, rightColor ) setColor( rightPixel, leftColor ) def flipFlop(heart): #this function brings the previous two functions together to flip # the heart horizontally and vertically. flip(heart) flop(heart) def leftTop(heart, rose): #places heart in top left corner of rose. w=getWidth(heart) h=getHeight(heart) for x in range (1, w): for y in range(1, h): pixheart=getPixel(heart, x, y) pixrose=getPixel(rose, x, y) setColor(pixrose, getColor(pixheart)) def leftBottom(heart, rose): #places heart in left, bottom corner of picture. flip(heart) #rotates so that point is upward instead of downward. w=getWidth(heart) h=getHeight(heart) hr=getHeight(rose) for x in range (1, w): for y in range(1, h): pixheart=getPixel(heart, x, y) pixrose=getPixel(rose, x, y+hr-h) #must subtract height of heart so that it will fit. Otherwise it would start painting the heart at the very bottom of the picture and you would get an out of bounds error. setColor(pixrose, getColor(pixheart)) def rightTop(heart, rose): #places heart in right top corner of rose picture. flipFlop(heart)#rotates the heart, so point is inward. w=getWidth(heart) h=getHeight(heart) wr=getWidth(rose) for x in range (1, w): for y in range(1, h): pixheart=getPixel(heart, x, y) pixrose=getPixel(rose, x+wr-w, y) #similar to the one before, must subtract width of heart # so it will fit into that top corner. setColor(pixrose, getColor(pixheart)) def rightBottom(heart, rose): #this will place the heart at the bottom right corner of the rose picture. flip(heart) #flips the previous heart upside down so it will also point inward. w=getWidth(heart) h=getHeight(heart) wr=getWidth(rose) hr=getHeight(rose) for x in range (1, w): for y in range(1, h): pixheart=getPixel(heart, x, y) pixrose=getPixel(rose, x+wr-w, y+hr-h) setColor(pixrose, getColor(pixheart)) def main(): #brings all the functions together to make 4, inward pointing hearts in the four corners. leftTop(heart, rose) leftBottom(heart, rose) rightTop(heart, rose) rightBottom(heart, rose) repaint(rose) ```

# Kaitlin's Solution

Kaitlin uses functions that return pictures, although this feature is not used in the program. That's fine for this assignment.

```#Kaitlin Somers #February 15, 2009 #Homework 3 Part b # Using the function main() to copy and rotate hearts # in all corners of the rose picture def heartcornerstl( heart, rose ): #places a heart in the top left corner of the picture for pixel in getPixels( heart ): x = getX( pixel ) y = getY( pixel ) col = getColor( pixel ) toPixel = getPixel( rose, x, y ) setColor( toPixel, col ) return rose def mysteryFunctionside( heart ): #creates a mirror image fliping the picture on its side. width = getWidth( heart ) for y in range( 1, getHeight( heart )+1 ): for x in range( 1, width/2 + 1 ): topPixel = getPixel( heart, width-x+1, y ) bottomPixel = getPixel( heart, x, y ) topColor = getColor( topPixel ) bottomColor = getColor (bottomPixel ) setColor( topPixel, bottomColor ) setColor( bottomPixel, topColor ) return(heart) def mysteryFunction( heart ): #flips the image upside down height = getHeight( heart ) for x in range( 1, getWidth( heart )+1 ): for y in range( 1, height/2 + 1 ): topPixel = getPixel( heart, x, y ) bottomPixel = getPixel( heart, x, height-y+1 ) topColor = getColor( topPixel ) bottomColor = getColor (bottomPixel ) setColor( topPixel, bottomColor ) setColor( bottomPixel, topColor ) return heart def heartcornerstr( heart, rose, offsetX, offsetY ): #places a heart in the top right corner of the picture for pixel in getPixels( heart ): x = getX( pixel ) y = getY( pixel ) col = getColor( pixel ) toPixel = getPixel( rose, x+ offsetX, y+ offsetY ) setColor( toPixel, col ) return(rose) def heartcornersbl(heart, rose, offsetX, offsetY): #places a heart in the bottom left corner of the picture for pixel in getPixels(heart): x= getX(pixel) y= getY(pixel) col= getColor(pixel) toPixel= getPixel(rose, x+offsetX, y+offsetY) setColor(toPixel, col) return(rose) def heartcornersbr( heart, rose, offsetX, offsetY ): #places a heart in the bottom right corner of the picture for pixel in getPixels( heart ): x = getX( pixel ) y = getY( pixel ) col = getColor( pixel ) toPixel = getPixel( rose, x+offsetX, y+offsetY ) setColor( toPixel, col ) return(rose) def main(): #Selects images to be used and then runs programs to place and rotate hearts. file=pickAFile() heart=makePicture(file) pict=pickAFile() rose=makePicture(pict) rosew= getWidth(rose) roseh= getHeight(rose) heartcornerstl(heart, rose) show(rose) mysteryFunctionside( heart ) heartcornerstr(heart, rose, rosew-50, 1) show(rose) mysteryFunction(heart) heartcornersbr(heart, rose, rosew-50, roseh-50) show(rose) mysteryFunctionside( heart ) heartcornersbl(heart, rose, 1, roseh-50) show(rose) ```

# Nicole's Solution

A third example, this time provided by Nicole. Note how the main function is short. This is what we want to aim for. A short main function that is the top of our hierarchy of tasks to be done. This is what we want. Just looking at main we see what the whole program is supposed to do.

```#Nicole Haseltine, Feb 14, 2009 #CSC 111, HW3 Problem 2 #This function copies the image of a heart over the image of a rose in each # of the four corners so that the point of the heart is always facing inward. def leftTop(pict1, pict2): #Copies the heart over the rose image in the top left corner. for pixel in getPixels(pict1): x=getX(pixel) y=getY(pixel) col=getColor(pixel) toPixel=getPixel(pict2,x,y) setColor(toPixel,col) def leftBottom(pict1, pict2): #Copies the heart over the image of the rose in the bottom left corner so the point of the heart is facing inward for pixel in getPixels(pict1): x=getX(pixel) y=getY(pixel) h=getHeight(pict1) h2=getHeight(pict2) w=getWidth(pict1) w2=getWidth(pict2) col=getColor(pixel) toPixel=getPixel(pict2,x, h2-y+1) setColor(toPixel, col) def rightTop(pict1, pict2): #Copies the heart over the image of the rose in the top right corner so the point of the heart is facing inward for pixel in getPixels(pict1): x=getX(pixel) y=getY(pixel) h=getHeight(pict1) h2=getHeight(pict2) w=getWidth(pict1) w2=getWidth(pict2) col=getColor(pixel) toPixel=getPixel(pict2, w2+1-x, y) setColor(toPixel, col) def rightBottom(pict1, pict2): #Copies the heart over the image of the rose in the bottom right corner so the point of the heart is facing inward for pixel in getPixels(pict1): x=getX(pixel) y=getY(pixel) h=getHeight(pict1) h2=getHeight(pict2) w=getWidth(pict1) w2=getWidth(pict2) col=getColor(pixel) toPixel=getPixel(pict2, w2+1-x, h2-y+1) setColor(toPixel, col) def main(pict1, pict2): # Copies pict1 (heart) over pict2 (flower) in each of the four corners # so the point of the heart is always facing inward. leftTop(pict1, pict2) leftBottom(pict1, pict2) rightTop(pict1, pict2) rightBottom(pict1, pict2) show(pict2) ```