我需要写一个函数spin(pic,x),它会拍照并逆时针旋转90度X次。我在函数中只有90度顺时针旋转:
def rotate(pic):
width = getWidth(pic)
height = getHeight(pic)
new = makeEmptyPicture(height,width)
tarX = 0
for x in range(0,width):
tarY = 0
for y in range(0,height):
p = getPixel(pic,x,y)
color = getColor(p)
setColor(getPixel(new,tarY,width-tarX-1),color)
tarY = tarY + 1
tarX = tarX +1
show(new)
return new
..但我不知道如何在旋转X次数时编写函数。谁知道我怎么做到这一点?
答案 0 :(得分:0)
您可以拨打rotate()
X次:
def spin(pic, x):
new_pic = duplicatePicture(pic)
for i in range(x):
new_pic = rotate(new_pic)
return new_pic
a_file = pickAFile()
a_pic = makePicture(a_file)
show(spin(a_pic, 3))
但这显然不是最优化的方式,因为您将计算X图像而不是您感兴趣的图像。我建议您首先尝试基本的switch...case
方法(即使此语句不存在)在Python中;):
xx = (x % 4) # Just in case you want (x=7) to rotate 3 times...
if (xx == 1):
new = makeEmptyPicture(height,width)
tarX = 0
for x in range(0,width):
tarY = 0
for y in range(0,height):
p = getPixel(pic,x,y)
color = getColor(p)
setColor(getPixel(new,tarY,width-tarX-1),color)
tarY = tarY + 1
tarX = tarX +1
return new
elif (xx == 2):
new = makeEmptyPicture(height,width)
# Do it yourself...
return new
elif (xx == 3):
new = makeEmptyPicture(height,width)
# Do it yourself...
return new
else:
return pic
然后,你可能会看到一种方法将这些案例合并为一个(但更复杂的)double for loop
......玩得开心......