In [6]:
p1=polygon([(0,0),(1,0),(4,3)])
In [7]:
# draw a polygon specified by vertices
show(p1)
In [8]:
c=circle((1,3), 6) # arguments are center, radius
In [ ]:
# CHALLENGE: draw an equilateral triangle 
In [13]:
polygon([(0,0), (1,0), (1/2,sqrt(3)/2)])
Out[13]:
In [15]:
def eqtri(basept, size):
    """Returns equilateral triangle with bottom left vertex at basept,
    and side length equal to size (with bottom edge parallel to x-axis)"""
    (x,y) = basept
    v1 = (x,y) # bottom left vertex
    v2 = (size+x,y) # right vertex
    v3 = (size/2 + x, size*sqrt(3)/2 + y) # top vertex
    return polygon([v1,v2,v3])
    
In [22]:
#0th iteration 
eqtri((0,0),1)
Out[22]:
In [21]:
# CHALLENGE: draw T1 first iteration of sierpinski triangle
t1 = eqtri((0,0), 1/2)
t2 = eqtri((1/2,0),1/2)
t3 = eqtri((1/4, sqrt(3)/4), 1/2)
t1+t2+t3
Out[21]:
In [23]:
def sierp_tri(basept, size, iter):
    """Return sierpinski triangle of base length size, 
    with bottom left point at basept,
    and iter number of iterations."""
    if iter==0:
        return eqtri(basept, size)
    
    else: 
        (x,y) = basept
        t1 = sierp_tri((x,y), size/2, iter-1) # bottom left part 
        t2 = sierp_tri((x+size/2,y), size/2, iter-1) # right part
        t3 = sierp_tri((x+size/4,y+size*sqrt(3)/4), size/2, iter-1) # top part
        return t1+t2+t3
In [30]:
sierp_tri((0,0), 1, 8)
Out[30]:
In [31]:
# fractal dimension of Sierp tri
N(log(3,2))
Out[31]:
1.58496250072116
In [ ]: