p1=polygon([(0,0),(1,0),(4,3)])
# draw a polygon specified by vertices
show(p1)
c=circle((1,3), 6) # arguments are center, radius
# CHALLENGE: draw an equilateral triangle
polygon([(0,0), (1,0), (1/2,sqrt(3)/2)])
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])
#0th iteration
eqtri((0,0),1)
# 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
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
sierp_tri((0,0), 1, 8)
# fractal dimension of Sierp tri
N(log(3,2))