CSC111 replicateHearts.py

From CSclasswiki
Jump to: navigation, search
def copyPicFromTo( fromPic, toPic, offsetX, offsetY ):
  # copy/overlay fromPic at offsets offsetX, offsetY 
  # on top of toPic
  for pixel in getPixels( fromPic ):
    x = getX( pixel )
    y = getY( pixel )
    fromColor = getColor( pixel )
    setColor( getPixel( toPic, x+offsetX, y+offsetY ), fromColor )

def newCopyOf( pict ):
  # given a picture, creates a new copy of it.  Create a blank image
  # and copy all the pixels from pict into the new image.  returns
  # the new image.
  width = getWidth( pict )
  height = getHeight( pict )
  newPict = makeEmptyPicture( width, height )
  copyPicFromTo( pict, newPict, 0, 0 )
  return newPict

def replicateFromTo( fromPic, toPic ):
  # make multiple copies of fromPic on top of toPic.
  fromWidth = getWidth( fromPic )
  toWidth = getWidth( toPic )
  fromHeight = getHeight( fromPic )
  toHeight = getHeight( toPic )
  for offsetX in range( 0, toWidth-fromWidth+1,fromWidth ):
    for offsetY in range( 0, toHeight-fromHeight+1,fromHeight ):
      copyPicFromTo( fromPic, toPic, offsetX, offsetY )

def replicateFromTo2( fromPic, toPic ):
  # Similar to replicateFromTo, but stop copying fromPic on a given row of toPic
  # as soon as the center of fromPic covers a pixel that is not black.
  fromWidth = getWidth( fromPic )
  toWidth = getWidth( toPic )
  fromHeight = getHeight( fromPic )
  toHeight = getHeight( toPic )
  for offsetX in range( 0, toWidth-fromWidth+1,fromWidth ):
    for offsetY in range( 0, toHeight-fromHeight+1,fromHeight ):
      middlePixel = getPixel( toPic, offsetX+fromWidth/2, offsetY+fromHeight/2 )
      middleColor = getColor( middlePixel )
      if distance( middleColor, black ) < 100:  
        copyPicFromTo( fromPic, toPic, offsetX, offsetY )
      else:
        break