Exemple #1
0
def draw_ulam_spiral(n, side    =20,    x_scale =0,\
                     useTriangle=False, useHex  =False,\
                     addText    =False, addLines=False, addSquares=True,\
                     modulo     =0,     filename=''):

    ulam = Turtle()

    if modulo:
        numbers = range(0, n + 1, modulo)
    else:
        numbers = sieve_of_eratosthenes(n + 1)

    if useTriangle:
        triangle_spiral(ulam, n, side)
    elif useHex:
        hex_spiral(ulam, n, side)
    else:
        rect_spiral(ulam, n, side, x_scale)

    viewBox, size = calculate_viewBox(ulam.coords, side)
    im = svgwrite.drawing.Drawing(size=size, viewBox=viewBox)

    for i, point in enumerate(ulam.coords):
        if addSquares:
            if i + 1 in numbers:
                im.add(im.rect(insert = point,\
                               size   = (side, side),\
                               fill   = '#335588'))
        if addText:
            im.add( im.text(i+1,
                            insert      = text_offset( point, side ),\
                            font_family ="sans-serif",\
                            font_size   =10,\
                            fill        ='black'))
    if addLines:
        for line in ulam.lines:
            im.add(im.line(start  = line[:2],\
                           end    = line[2:],\
                           stroke = 'black'))

    im.saveas('img/' + filename + '.svg')
    return
def anchors(side, pos=[0, 0], _dir=0):
    # 0.381 is a magic constant

    myrtle = Turtle()
    myrtle.set_pos(pos)
    myrtle.set_dir(_dir)
    rev_polygon(myrtle, 5, side)
    myrtle.coords.pop(0)
    myrtle.forward(side * 0.381)
    myrtle.coords.pop()
    myrtle.right(72)
    myrtle.forward(side * 0.381)
    myrtle.dots = myrtle.coords[::]

    return myrtle.coords
            turtle.forward(step)
            turtle.pendown()
            turtle.right(72)

        turtle.set_pos(pos)
        turtle.penup()
        step = 2 * side * (1 + sin(radians(18)))
        turtle.forward(step)
        turtle.pendown()
        turtle.right(72)


if __name__ == '__main__':

    print "Sending turtle Bilbo on an adventure!"
    Bilbo = Turtle()
    """
    print ">>> Bilbo sees a tree fractal.."
    Bilbo.left( 90 )
    clock = time.time()
    tree(Bilbo, 6, 250)
    print "    Time: {}".format(time.time() - clock)
    Bilbo.draw_object('tree')
    Bilbo.restart()

    print ">>> Bilbo found Koch snowflake.."
    clock = time.time()
    koch_snowflake(Bilbo, 4, 450)
    print "    Time: {}".format(time.time() - clock)
    Bilbo.draw_object('koch')
    Bilbo.restart()
Exemple #4
0
def anchors(side, pos=[0, 0], _dir=0):
# 0.381 is a magic constant

    myrtle = Turtle()
    myrtle.set_pos( pos )
    myrtle.set_dir( _dir )
    rev_polygon(myrtle, 5, side )
    myrtle.coords.pop(0)
    myrtle.forward( side * 0.381 )
    myrtle.coords.pop()
    myrtle.right( 72 )
    myrtle.forward( side * 0.381 )
    myrtle.dots = myrtle.coords[::]

    return myrtle.coords
Exemple #5
0
            turtle.pendown()
            turtle.right( 72 )

        turtle.set_pos( pos )
        turtle.penup()
        step = 2 * side * (1 + sin(radians(18))) 
        turtle.forward( step )
        turtle.pendown()
        turtle.right( 72 )


if __name__ == '__main__':
        
    
    print "Sending turtle Bilbo on an adventure!"
    Bilbo = Turtle()

    """
    print ">>> Bilbo sees a tree fractal.."
    Bilbo.left( 90 )
    clock = time.time()
    tree(Bilbo, 6, 250)
    print "    Time: {}".format(time.time() - clock)
    Bilbo.draw_object('tree')
    Bilbo.restart()

    print ">>> Bilbo found Koch snowflake.."
    clock = time.time()
    koch_snowflake(Bilbo, 4, 450)
    print "    Time: {}".format(time.time() - clock)
    Bilbo.draw_object('koch')