Jython(JES)-90度旋转功能 [英] Jython (JES) - 90 degree rotation function
问题描述
我需要编写一个函数spin(pic,x),它将在其中拍摄图片并将其逆时针X倍旋转90度.我在函数中只有顺时针旋转90度:
I need to write a function spin(pic,x) where it will take a picture and rotate it 90 degrees counter clockwise X amount of times. I have just the 90 degree clockwise rotation in a function:
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次的函数.有人知道我该怎么做吗?
.. but I have no idea how I would go about writing a function on rotating it X amount of times. Anyone know how I can do this?
推荐答案
您可以拨打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中不存在) ;):
But this is clearly not the most optimized way because you'll compute X images instead of the one you are interested in. I suggest you try a basic switch...case
approach first (even if this statement doesn't exists in 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
...玩得开心...
Then, may be you'll be able to see a way to merge those cases into a single (but more complicated) double for loop
... Have fun...
这篇关于Jython(JES)-90度旋转功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!