from time import time

#STD_WIDTH = 250 # the largest allowed width of picture dimension
#STD_HEIGHT = 300

def getCopy(pic):
	"""Return an exact copy of the picture passed as parameter.
	Included since duplicatePicture(pic) is only available in JES 3.1"""
	
	h = getHeight(pic)
	w = getWidth(pic)
	newPic = makeEmptyPicture(w,h)
  
	for x in range(1, w+1):
		for y in range(1, h+1):
			newP = getPixel(newPic,x,y)
			p = getPixel(pic,x,y)
			setColor(newP, getColor(p))
	
	return newPic
	
def scale(pic, width, height):
	"""Scales picture pic to the "width" and "height"
	dimensions and returns the result"""
	
	facX = (1.0 * width) / getWidth(pic)
	facY = (1.0 * height) / getHeight(pic)
	stepX = 1 / facX
	stepY = 1 / facY
	canvas = makeEmptyPicture(width, height)

	# Now, do the actual copying
	sourceX = 1
	for targetX in range(1,width + 1):
		sourceY = 1
		for targetY in range(1,height + 1):
			color = getColor(getPixel(pic, int(sourceX), int(sourceY)))
			setColor(getPixel(canvas, targetX, targetY), color)
			sourceY = sourceY + stepY
		#incrementing column number
		sourceX = sourceX + stepX
	return canvas

def jointShapeCircle(pic1, pic2, r1, r2):
	"""On the pic1 insert part of the pic2 which fits inside the annulus
	formed by two circles centered in the pic1 centre with radii r1 and r2.
	"""
	
	assert (r1 >= r2 >=0), "The first radius must be the outer circle radius"
	w1 = pic1.getWidth()
	h1 = pic1.getHeight()
	w2 = pic2.getWidth()
	h2 = pic2.getHeight()
	assert (h1 == h2 and w1 == w2), "Pictures have different dimensions!"
	
	R = max(h1,w1)
	
	assert (0 <= r1 <= R), "The inner disk must be inside the canvas!"
	
	# centre of the picture
	x0 = w1 / 2
	y0 = h1 / 2
	r1 = int(r1)
	r2 = int(r2)
	if (h1 >= w1):
		xRangeLeft = range(max(1,x0 - r1), max(1,x0 - r2))
		xRangeCentre = range(max(1,x0 - r2),min(x0 + r2,w1 + 1))
		xRangeRight = range(min(x0 + r2,w1 + 1),min(x0 + r1,w1 + 1))
		for x in (xRangeLeft + xRangeRight):
			root = int(sqrt(r1**2 - (x - x0)**2))
			yRange = range(max(1,y0 - root),min(h1 + 1,y0 + root + 1))
			for y in yRange:
				setColor(getPixel(pic1,x,y), getColor(getPixel(pic2,x,y)))
		for x in xRangeCentre:
			root1 = int(sqrt(r1**2 - (x - x0)**2))
			root2 = int(sqrt(r2**2 - (x - x0)**2))
			yRangeUp = range(max(1,y0 - root1), max(1,y0 - root2 + 1))
			yRangeDown = range(min(h1 + 1,y0 + root2),min(h1 + 1,y0 + root1 + 1))
			for y in (yRangeUp + yRangeDown):
				setColor(getPixel(pic1,x,y), getColor(getPixel(pic2,x,y)))
	else:
		yRangeUp = range(max(1,y0 - r1), max(1,y0 - r2))
		yRangeCentre = range(max(1,y0 - r2),min(y0 + r2,h1 + 1))
		yRangeDown = range(min(y0 + r2,h1 + 1),min(y0 + r1,h1 + 1))
		for y in (yRangeUp + yRangeDown):
			root = int(sqrt(r1**2 - (y - y0)**2))
			xRange = range(max(1,x0 - root),min(w1 + 1,x0 + root + 1))
			for x in xRange:
				setColor(getPixel(pic1,x,y), getColor(getPixel(pic2,x,y)))
		for y in xRangeCentre:
			root1 = int(sqrt(r1**2 - (y - y0)**2))
			root2 = int(sqrt(r2**2 - (y - y0)**2))
			yRangeLeft = range(max(1,x0 - root1), max(1,x0 - root2 + 1))
			yRangeRight = range(min(w1 + 1,x0 + root2),min(w1 + 1,x0 + root1 + 1))
			for y in (yRangeLeft + yRangeRight):
				setColor(getPixel(pic1,x,y), getColor(getPixel(pic2,x,y)))
				
def dist(x1, y1, x2, y2):
  return sqrt((x1 - x2)**2 + (y1 - y2)**2)
 

def shapeCircleSlide(pic1, pic2,ns):
	h1 = pic1.getHeight()
	h2 = pic2.getHeight()
	w1 = pic1.getWidth()
	w2 = pic2.getWidth()
	assert (h1 == h2 and w1 == w2), "The pictures must have the same dimensions."

	R = math.sqrt((0.5 * h1)**2 + (0.5 * w1)**2)

	dR = math.sqrt((0.5 * h1)**2 + (0.5 * w1)**2) / ns #len(rads)#numSteps
	copy = getCopy(pic1)
	#print "The values of radius is %d, the number of steps %d, the radius step %d" %(R,numSteps,dR)
	#originally only pic1 present
	copy.show()

	#rads = map(lambda a: int(dR * a), range(ns + 1))

	start = time()
 	
 	#first step: small circle in the centre
	jointShapeCircle(copy,pic2,dR,0)
	copy.repaint()
	
	#next: increment the inner circle and change 
	#pixesl in the annulus
	for i in range(1,ns+1):
		#printNow("Doing step %d" % i)
		jointShapeCircle(copy,pic2,int(dR * i), int(dR*(i-1)))#rads[i-1])
		copy.repaint()

	finish = time()
	print "The transition time is %d seconds" %(finish - start)
