Jython的(JES) - 90度旋转功能(Jython (JES) - 90 degree r

2019-10-18 19:54发布

我需要编写的函数的自旋(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量写一个函数。 任何人都知道我能做到这一点?

Answer 1:

你可以称之为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 ......玩得开心...



文章来源: Jython (JES) - 90 degree rotation function