坐标的旋转,翻转

来源:互联网 发布:逆战登录网络连接失败 编辑:程序博客网 时间:2024/05/18 03:11

因为一个旋转的变换是(x,y)->(y,-x),所以只需要三次连续旋转就可以获得所有的旋转。而翻转的类型则是(x,y)->(x,-y),翻转之后再旋转则得到另外的图形。如果给定了相应的坐标范围,则要注意旋转之后会超过坐标范围,因此需要利用平移将图形移回定义的坐标系。注意,这也是一个小技巧,不管一个图形的位置如何,我们都可以将它标准化到坐标系的左上角,这样就消除了平移的影响。

下面的代码还加上了去掉重复的旋转图形,有些对称图形的旋转结果是重复的。

def normalize(p):    x,y = p[0][0],p[0][1]    new_p = []    for cell in p:x,y = min(x,cell[0]),min(y,cell[1])    return sort_p(tuple([(i-x,j-y) for (i,j) in p]))def rotate(p):    new_p = []    for cell in p:        new_p.append((cell[1],-cell[0]))    return sort_p(normalize(new_p))def flip(p):    new_p = []    for cell in p:        new_p.append((cell[0],-cell[1]))    return sort_p(normalize(new_p))def sort_p(p):    return tuple(sorted(p,key = lambda cell:(cell[0],cell[1])))def generate(p):    record = set()    for i in range(4):        p = rotate(p)        if p not in record:            record.add(p)            print_format(p)    p = flip(p)    for i in range(4):        p = rotate(p)        if p not in record:            record.add(p)            print_format(p)def print_format(p):    x,y = -1,0    print(p)    for cell in p:        if x!=cell[0]:print();print(' '*(cell[1]),end='');first = 0        print(' '*(cell[1]-y-1)*first+'*',end='')        first = 1        x,y = cell    print('\n','='*30)generate(((0,0),(1,0),(2,0),(2,1),(2,2),(3,1),(5,2)))

这里写图片描述

0 0