Ask Your Question
1

Graphics not plotted in same plot?

asked 2018-04-09 03:08:27 +0100

Luther13 gravatar image

updated 2018-04-09 03:21:10 +0100

I'm trying to make a sierpinsky triangle recursively, and i can generate all the triangles (via 3 line segments), however when i go to show my list, the graphics print separately. I saw on the documentation there's a graphics_array, which seems useful, but there weren't many examples and my tinkering with it was pretty much useless, especially since the "adjoin" feature is not yet implemented.

Also tried passing the graphics object "some_lines" to get filled by the function, but it doesn't seem like it accesses the reference. Code attached, maybe you can see a way I can plot all the lines within one sum of graphics, as was my initial attempt:

def draw_tri(some_lines, endpts):
    some_lines+=line([endpts[0], endpts[1]], color='blue')
    some_lines+=line([endpts[0], endpts[2]], color='blue')
    some_lines+=line([endpts[1], endpts[2]], color='blue')
    return

def get_mid(x,y):
    return (x+y)/2

def recurse_SG(some_lines, n, endpts):
    #graphics_sum = graphics_array([line([endpts[0], endpts[1]], color='blue')],3, 1)
    if(n==1):
        #graphics_sum[i]=draw_tri(endpts)
        draw_tri(some_lines, endpts)
    else:
        recurse_SG(some_lines, n-1, [endpts[0], (get_mid(endpts[0][0], endpts[1][0]), get_mid(endpts[0][1], endpts[1][1])), (get_mid(endpts[0][0], endpts[2][0]), get_mid(endpts[0][1], endpts[2][1]))])
        recurse_SG(some_lines, n-1, [endpts[1], (get_mid(endpts[1][0], endpts[0][0]), get_mid(endpts[1][1], endpts[0][1])), (get_mid(endpts[1][0], endpts[2][0]), get_mid(endpts[1][1], endpts[2][1]))])
        recurse_SG(some_lines, n-1, [endpts[2], (get_mid(endpts[2][0], endpts[0][0]), get_mid(endpts[2][1], endpts[0][1])), (get_mid(endpts[2][0], endpts[1][0]), get_mid(endpts[2][1], endpts[1][1]))])

    show(some_lines, axes=false)
edit retag flag offensive close merge delete

Comments

Welcome to Ask Sage! Thank you for your question!

slelievre gravatar imageslelievre ( 2018-04-09 03:11:41 +0100 )edit

In addition to the functions draw_tri, get_mid, recurse_SG, could you provide an example of how you use them?

slelievre gravatar imageslelievre ( 2018-04-09 03:22:23 +0100 )edit
1

Thanks for editing your question, it is much more readable now!

slelievre gravatar imageslelievre ( 2018-04-09 03:37:03 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted
3

answered 2018-04-09 03:35:54 +0100

slelievre gravatar image

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)
edit flag offensive delete link more

Comments

Thanks! Very insightful. I hope to become proficient in Sage, soon. I'll have to spend time going through lots of documentation, since I didn't even know about polygons! I appreciate the advice, especially the perspective about splitting up tasks. I imagine then that graphic_arrays aren't terribly useful?

Luther13 gravatar imageLuther13 ( 2018-04-09 05:09:45 +0100 )edit
1

The graphics_array function is for displaying a collection of graphic objects in a table form.

For example, maybe you plotted the sierpinsky gasket for various starting triangles and you want to show them side by side...

slelievre gravatar imageslelievre ( 2018-04-09 09:33:23 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2018-04-09 03:08:27 +0100

Seen: 394 times

Last updated: Apr 09 '18