#from math import sin, cos, pi, radians
#import math

def swirl(pic,inangle, outangle):
  """The angles are meant to be in radians,
  Use inangle pi/4, pi/2 or similar, and outangle 0
  to get decent looking output"""	
  #orig = makePicture(filename)
  height = getHeight(pic)
  width = getWidth(pic)	
  if height != width:
    print "Can't handle nonsquare pictures!"
    return orig
  else:
    size = min(height,width)
    target = makeEmptyPicture(size,size)
    centreX = size / 2
    centreY = size / 2
    radius = size / 2	
    ia = inangle
    oa = outangle
  
    for x in range(1,getWidth(target)+1):
      for y in range(1,getHeight(target)+1):
        pixel = getPixel(target, x, y)
        r = distanceFromCentre(centreX, centreY, x, y)
        if r < radius:
          angle = ia + (oa - ia)*r/radius
          x1 = centreX + int((x - centreX)*cos(angle) + (y - centreY)*sin(angle))
          y1 = centreY + int(-(x - centreX)*sin(angle) + (y - centreY)*cos(angle))
          pixel1 = getPixel(pic,x1,y1)
          colour = getColor(pixel1)
          setColor(pixel, colour)
        else:
          setColor(pixel, getColor(getPixel(pic,x,y)))
					
  return target


def distanceFromCentre(x1, y1, x2, y2):
  return sqrt((x1 - x2)**2 + (y1 - y2)**2)

