Ask Your Question

animated line drawing

asked 2017-11-23 18:29:45 -0600

kkmi gravatar image

updated 2017-11-23 18:30:53 -0600

Is it possible to draw a line in sagemath as like javascript?

A Demo:

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2017-11-24 01:37:53 -0600

eric_g gravatar image

updated 2017-11-25 14:57:37 -0600

For sure, see the animated plots documentation.

EDIT: here is a possible solution:

def drawlines(lines, ns, **options):
    - ``lines``: a list of lines, each line being defined
      by its endpoints as ``[(x1,y1), (x2,y2)]``
    - ``ns``: the number of division of each line for the 
    - ``**options``: any option to pass to the graphic 
      command ``line``, like ``color`` or ``thickness``
    resu = []
    preceeding = Graphics()
    for li in lines:
        x1, y1 = li[0]
        x2, y2 = li[1]
        dx = float(x2 - x1)/float(ns)
        dy = float(y2 - y1)/float(ns)
        for i in range(ns):
            xx = x1 + (i+1)*dx
            yy = y1 + (i+1)*dy
            resu.append(preceeding + line([(x1,y1), (xx,yy)], **options))
        preceeding = resu[-1]
    return resu

anim = animate(drawlines([[(1,2), (3,4)],
                          [(3,4), (3,0)],
                          [(3,0), (1,2)]], 10, color='red', thickness=2),
               xmin=0, xmax=4, ymin=0, ymax=5)

You can see it at work on SageMathCell.

edit flag offensive delete link more


please tell me the exact example from your url. I can't find the example I want. I mean drawing a line, not moving a drawed line.

kkmi gravatar imagekkmi ( 2017-11-24 02:44:14 -0600 )edit

Well, you may draw the line as a sequence of small segments and then animate it.

eric_g gravatar imageeric_g ( 2017-11-24 03:19:09 -0600 )edit

How can I plot the trace of animated line? If I draw small segments of a line, all segments will be drawed at the same time. Is there 'wait()' like command in sage?

kkmi gravatar imagekkmi ( 2017-11-25 05:02:47 -0600 )edit

I've edited my answer to provide the link to some example.

eric_g gravatar imageeric_g ( 2017-11-25 12:52:59 -0600 )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

1 follower


Asked: 2017-11-23 18:29:45 -0600

Seen: 86 times

Last updated: Nov 25