ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 25 Nov 2017 19:52:59 +0100animated line drawinghttps://ask.sagemath.org/question/39755/animated-line-drawing/Is it possible to draw a line in sagemath as like javascript?
A Demo: http://jsfiddle.net/m1erickson/7faRQ/Fri, 24 Nov 2017 01:29:45 +0100https://ask.sagemath.org/question/39755/animated-line-drawing/Answer by eric_g for <p>Is it possible to draw a line in sagemath as like javascript?</p>
<p>A Demo: http://jsfiddle.net/m1erickson/7faRQ/</p>
https://ask.sagemath.org/question/39755/animated-line-drawing/?answer=39759#post-id-39759For sure, see the [animated plots documentation](http://doc.sagemath.org/html/en/reference/plotting/sage/plot/animate.html).
EDIT: here is a possible solution:
def drawlines(lines, ns, **options):
r"""
Input:
- ``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
animation
- ``**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)
anim.show(delay=5)
You can see it at work on [SageMathCell](http://sagecell.sagemath.org/?q=dnpicz).Fri, 24 Nov 2017 08:37:53 +0100https://ask.sagemath.org/question/39755/animated-line-drawing/?answer=39759#post-id-39759Comment by kkmi for <p>For sure, see the <a href="http://doc.sagemath.org/html/en/reference/plotting/sage/plot/animate.html">animated plots documentation</a>.</p>
<p>EDIT: here is a possible solution:</p>
<pre><code>def drawlines(lines, ns, **options):
r"""
Input:
- ``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
animation
- ``**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)
anim.show(delay=5)
</code></pre>
<p>You can see it at work on <a href="http://sagecell.sagemath.org/?q=dnpicz">SageMathCell</a>.</p>
https://ask.sagemath.org/question/39755/animated-line-drawing/?comment=39762#post-id-39762please 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.Fri, 24 Nov 2017 09:44:14 +0100https://ask.sagemath.org/question/39755/animated-line-drawing/?comment=39762#post-id-39762Comment by eric_g for <p>For sure, see the <a href="http://doc.sagemath.org/html/en/reference/plotting/sage/plot/animate.html">animated plots documentation</a>.</p>
<p>EDIT: here is a possible solution:</p>
<pre><code>def drawlines(lines, ns, **options):
r"""
Input:
- ``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
animation
- ``**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)
anim.show(delay=5)
</code></pre>
<p>You can see it at work on <a href="http://sagecell.sagemath.org/?q=dnpicz">SageMathCell</a>.</p>
https://ask.sagemath.org/question/39755/animated-line-drawing/?comment=39763#post-id-39763Well, you may draw the line as a sequence of small segments and then animate it.Fri, 24 Nov 2017 10:19:09 +0100https://ask.sagemath.org/question/39755/animated-line-drawing/?comment=39763#post-id-39763Comment by kkmi for <p>For sure, see the <a href="http://doc.sagemath.org/html/en/reference/plotting/sage/plot/animate.html">animated plots documentation</a>.</p>
<p>EDIT: here is a possible solution:</p>
<pre><code>def drawlines(lines, ns, **options):
r"""
Input:
- ``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
animation
- ``**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)
anim.show(delay=5)
</code></pre>
<p>You can see it at work on <a href="http://sagecell.sagemath.org/?q=dnpicz">SageMathCell</a>.</p>
https://ask.sagemath.org/question/39755/animated-line-drawing/?comment=39788#post-id-39788How 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?Sat, 25 Nov 2017 12:02:47 +0100https://ask.sagemath.org/question/39755/animated-line-drawing/?comment=39788#post-id-39788Comment by eric_g for <p>For sure, see the <a href="http://doc.sagemath.org/html/en/reference/plotting/sage/plot/animate.html">animated plots documentation</a>.</p>
<p>EDIT: here is a possible solution:</p>
<pre><code>def drawlines(lines, ns, **options):
r"""
Input:
- ``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
animation
- ``**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)
anim.show(delay=5)
</code></pre>
<p>You can see it at work on <a href="http://sagecell.sagemath.org/?q=dnpicz">SageMathCell</a>.</p>
https://ask.sagemath.org/question/39755/animated-line-drawing/?comment=39791#post-id-39791I've edited my answer to provide the link to some example.Sat, 25 Nov 2017 19:52:59 +0100https://ask.sagemath.org/question/39755/animated-line-drawing/?comment=39791#post-id-39791