hexaflexa.py 文件源码

python
阅读 17 收藏 0 点赞 0 评论 0

项目:hexaflexa 作者: michibo 项目源码 文件源码
def main():
    parser = argparse.ArgumentParser(description='Make a hexaflexagon with a picture printed on each of the six faces.')
    parser.add_argument('pics', type=str, nargs='+',
                        help='Filenames to pictures (only png).')
    parser.add_argument('--output', type=str,
                        help='Output filename (pdf).', default="out.pdf")
    parser.add_argument('--paper', type=str,
                        help='Paper size', default="A4");

    args = parser.parse_args()

    # The units for pdf size is a point=1/72inch
    if (args.paper.upper() == 'A4'):
        WIDTH, HEIGHT = 595, 842
    elif (args.paper.upper() == 'LETTER'):
        WIDTH, HEIGHT = 612, 792
    elif (args.paper.upper() == 'LEGAL'):
        WIDTH, HEIGHT = 612, 1008
    elif (args.paper.upper() == 'TABLOID'):
        WIDTH, HEIGHT = 792, 1224
    else:
        print("Paper type not understood: '"+args.paper+"'")
        sys.exit(1)

    surface = cairo.PDFSurface(args.output, WIDTH, HEIGHT)
    ctx = cairo.Context(surface)

    border = .75 * 72.0 / 2.54   # border = 3/4cm
    a = (HEIGHT - 2*border)/10   # eq. triangle side, we need 10 down the spine
    b = a/2                      # half-side of eq. triangles
    h = sqrt(3)/2 * a            # height of eq.triangles
    n = int(WIDTH / (h*2));      # this is how many will fit in the paper's width

    for i in range(n):
        ctx.move_to( border + i*(2*h), border )

        commonfaces         = list(zip(range(0,3), ["scissor"] * 3))
        hiddenfaces         = list(zip(range(3,6), ["scissor", "scissor", "stone"]))
        transparentfaces    = list(zip(range(0,3), ["paper"]   * 3))

        for pic_fn, (face, ori) in zip(args.pics, commonfaces + hiddenfaces + transparentfaces):
            img = cairo.ImageSurface.create_from_png(pic_fn)

            drawPicture(ctx, a, b, h, face, ori, img)

        drawOutline(ctx, a, b, h)

    surface.show_page()
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号