Ejemplo n.º 1
0
def graph(f):
    canvas = Canvas()
    for pointpair in [((-2,0), (2,0)), ((-2,0), (-1.92,.08)), ((-2,0), (-1.92,-.08)), ((2,0), (1.92,.08)), ((2,0), (1.92,-0.08)), ((0,-2), (0,2)), ((0,-2), (-.08,-1.92)), ((0,-2), (.08,-1.92)), ((0,2), (-.08,1.92)), ((0,2), (.08,1.92))]:
        canvas.add(Line(*pointpair))
    if f != None:
        path = Path()
        cubics = recursively_generate_cubics(f, -2, 2)
        for c in cubics:
            t1_3 = (2*c.t0 + c.t1)/3
            t2_3 = (c.t0 + 2*c.t1)/3
            point0 = (c.t0, c.f0)
            control1 = (t1_3, c.c0)
            control2 = (t2_3, c.c1)
            point3 = (c.t1, c.f1)
            path.move_to(point0)
            path.spline_to(control1, control2, point3)
            canvas.add(Circle(control1))
            canvas.add(Circle(control2))
            pixel = 4/500
            canvas.add(Line((c.t0, c.f0-2*pixel), (c.t0, c.f0+2*pixel)))
            canvas.add(Line((c.t1, c.f1-2*pixel), (c.t1, c.f1+2*pixel)))
        canvas.add(path)
    return "\n".join(canvas.output())