Ask Your Question

Revision history [back]

Here are some suggestions with respect to the code in the question.

One suggestion is to draw triangles using polygon instead of line.

This way you don't have to access endpoints[0], endpoints[1], endpoints[2] but you can do all in one go.

sage: pts = [(0,0), (2, 0), (1, 1.732)]
sage: t = polygon(pts, fill=False)
sage: t.show(aspect_ratio=1, axes=False)
Launched png viewer for Graphics object consisting of 1 graphics primitive

Another suggestion would be to separate the computation and the plotting, ie compute the various triangles you will need to plot, storing them in a list, and then just adding plots of these triangle to a graphics object.

You already have a good grasp on how graphics work in Sage. You noticed that f you have several graphics objects, you can just add them with +, as in:

sage: p = plot(x^2)
sage: l = line2d([(-1,-1), (1, 1)], color='green')
sage: c = circle((0,0), 1, color='purple')
sage: p + l + c

Note that if you want an empty graphics object as a starting point, you can get one with Graphics().

So you can do for instance

sage: G = Graphics()
sage: for k in range(10):
....:     G += line2d([(k, 0), (k, 1)], hue=k/10)
sage: G.show(axes=False)

Once you have a function to get all the triangles you need (as a list of triples of points), you could have a different function to plot them:

def plot_gasket(list_of_triangles):
    G = Graphics()
    for t in list_of_triangles:
        G += polygon(t, fill=False)
    return G

and then you can show the result!

sage: G.show(aspect_ratio=1, axes=False)