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, 20 Apr 2013 11:03:14 +0200Evenly space points along a parametric curve?https://ask.sagemath.org/question/8474/evenly-space-points-along-a-parametric-curve/Are there arc-length parametrization functions hidden somewhere in Sage? I have some 3D parametric curves (smooth) of length `L` along which I would like to put `n` dots at regular intervals. What I've been doing so far is using numerical integration to find the arc length parameter, and then using `find_root` to find the positions of the dots (spaced by arc length `L/n`).
This is pretty slow, and has the further limitation that I need to specify a region on which `find_root` should work. If the parametrization is really uneven, it's tough to develop a good initial estimate for where to look.
So, does anyone have other ideas for doing this? Thanks!
UPDATE: Here are some examples -- they're different fibers in the Hopf fibration, and are given by `r = (rx,ry,rz)`
Example 1:
rx(t) = -0.309*cos(t)*arccos(0.951*cos(-t - 1.57))/(sqrt(-0.904*cos(-t - 1.570)^2 + 1)*pi)
ry(t) = -0.309*sin(t)*arccos(0.951*cos(-t - 1.57))/(sqrt(-0.904*cos(-t - 1.57)^2 + 1)*pi)
rz(t) = 0.951*sin(-t - 1.57)*arccos(0.951*cos(-t - 1.57))/(sqrt(-0.904*cos(-t - 1.57)^2 + 1)*pi)]
Example 2:
rx(t) = -0.707*cos(t)*arccos(0.707*cos(-t - 1.57))/(sqrt(-0.5*cos(-t - 1.57)^2 + 1)*pi)
ry(t) = -0.707*sin(t)*arccos(0.707*cos(-t - 1.57))/(sqrt(-0.5*cos(-t - 1.57)^2 + 1)*pi)
rz(t) = 0.707*sin(-t - 1.57)*arccos(0.707*cos(-t - 1.57))/(sqrt(-0.5*cos(-t - 1.57)^2 + 1)*pi)
Mon, 14 Nov 2011 21:44:56 +0100https://ask.sagemath.org/question/8474/evenly-space-points-along-a-parametric-curve/Comment by jdc for <p>Are there arc-length parametrization functions hidden somewhere in Sage? I have some 3D parametric curves (smooth) of length <code>L</code> along which I would like to put <code>n</code> dots at regular intervals. What I've been doing so far is using numerical integration to find the arc length parameter, and then using <code>find_root</code> to find the positions of the dots (spaced by arc length <code>L/n</code>).</p>
<p>This is pretty slow, and has the further limitation that I need to specify a region on which <code>find_root</code> should work. If the parametrization is really uneven, it's tough to develop a good initial estimate for where to look.</p>
<p>So, does anyone have other ideas for doing this? Thanks!</p>
<p>UPDATE: Here are some examples -- they're different fibers in the Hopf fibration, and are given by <code>r = (rx,ry,rz)</code></p>
<p>Example 1: </p>
<pre><code>rx(t) = -0.309*cos(t)*arccos(0.951*cos(-t - 1.57))/(sqrt(-0.904*cos(-t - 1.570)^2 + 1)*pi)
ry(t) = -0.309*sin(t)*arccos(0.951*cos(-t - 1.57))/(sqrt(-0.904*cos(-t - 1.57)^2 + 1)*pi)
rz(t) = 0.951*sin(-t - 1.57)*arccos(0.951*cos(-t - 1.57))/(sqrt(-0.904*cos(-t - 1.57)^2 + 1)*pi)]
</code></pre>
<p>Example 2:</p>
<pre><code>rx(t) = -0.707*cos(t)*arccos(0.707*cos(-t - 1.57))/(sqrt(-0.5*cos(-t - 1.57)^2 + 1)*pi)
ry(t) = -0.707*sin(t)*arccos(0.707*cos(-t - 1.57))/(sqrt(-0.5*cos(-t - 1.57)^2 + 1)*pi)
rz(t) = 0.707*sin(-t - 1.57)*arccos(0.707*cos(-t - 1.57))/(sqrt(-0.5*cos(-t - 1.57)^2 + 1)*pi)
</code></pre>
https://ask.sagemath.org/question/8474/evenly-space-points-along-a-parametric-curve/?comment=20896#post-id-20896Could you give an example of a typical curve that you're considering?Tue, 15 Nov 2011 15:19:32 +0100https://ask.sagemath.org/question/8474/evenly-space-points-along-a-parametric-curve/?comment=20896#post-id-20896Comment by DSM for <p>Are there arc-length parametrization functions hidden somewhere in Sage? I have some 3D parametric curves (smooth) of length <code>L</code> along which I would like to put <code>n</code> dots at regular intervals. What I've been doing so far is using numerical integration to find the arc length parameter, and then using <code>find_root</code> to find the positions of the dots (spaced by arc length <code>L/n</code>).</p>
<p>This is pretty slow, and has the further limitation that I need to specify a region on which <code>find_root</code> should work. If the parametrization is really uneven, it's tough to develop a good initial estimate for where to look.</p>
<p>So, does anyone have other ideas for doing this? Thanks!</p>
<p>UPDATE: Here are some examples -- they're different fibers in the Hopf fibration, and are given by <code>r = (rx,ry,rz)</code></p>
<p>Example 1: </p>
<pre><code>rx(t) = -0.309*cos(t)*arccos(0.951*cos(-t - 1.57))/(sqrt(-0.904*cos(-t - 1.570)^2 + 1)*pi)
ry(t) = -0.309*sin(t)*arccos(0.951*cos(-t - 1.57))/(sqrt(-0.904*cos(-t - 1.57)^2 + 1)*pi)
rz(t) = 0.951*sin(-t - 1.57)*arccos(0.951*cos(-t - 1.57))/(sqrt(-0.904*cos(-t - 1.57)^2 + 1)*pi)]
</code></pre>
<p>Example 2:</p>
<pre><code>rx(t) = -0.707*cos(t)*arccos(0.707*cos(-t - 1.57))/(sqrt(-0.5*cos(-t - 1.57)^2 + 1)*pi)
ry(t) = -0.707*sin(t)*arccos(0.707*cos(-t - 1.57))/(sqrt(-0.5*cos(-t - 1.57)^2 + 1)*pi)
rz(t) = 0.707*sin(-t - 1.57)*arccos(0.707*cos(-t - 1.57))/(sqrt(-0.5*cos(-t - 1.57)^2 + 1)*pi)
</code></pre>
https://ask.sagemath.org/question/8474/evenly-space-points-along-a-parametric-curve/?comment=20894#post-id-20894Unless a given curve has special properties (say, an ellipse) I don't know that there is a better way in general. Would it work for your needs if we did it this way but faster?Wed, 16 Nov 2011 07:39:13 +0100https://ask.sagemath.org/question/8474/evenly-space-points-along-a-parametric-curve/?comment=20894#post-id-20894Comment by niles for <p>Are there arc-length parametrization functions hidden somewhere in Sage? I have some 3D parametric curves (smooth) of length <code>L</code> along which I would like to put <code>n</code> dots at regular intervals. What I've been doing so far is using numerical integration to find the arc length parameter, and then using <code>find_root</code> to find the positions of the dots (spaced by arc length <code>L/n</code>).</p>
<p>This is pretty slow, and has the further limitation that I need to specify a region on which <code>find_root</code> should work. If the parametrization is really uneven, it's tough to develop a good initial estimate for where to look.</p>
<p>So, does anyone have other ideas for doing this? Thanks!</p>
<p>UPDATE: Here are some examples -- they're different fibers in the Hopf fibration, and are given by <code>r = (rx,ry,rz)</code></p>
<p>Example 1: </p>
<pre><code>rx(t) = -0.309*cos(t)*arccos(0.951*cos(-t - 1.57))/(sqrt(-0.904*cos(-t - 1.570)^2 + 1)*pi)
ry(t) = -0.309*sin(t)*arccos(0.951*cos(-t - 1.57))/(sqrt(-0.904*cos(-t - 1.57)^2 + 1)*pi)
rz(t) = 0.951*sin(-t - 1.57)*arccos(0.951*cos(-t - 1.57))/(sqrt(-0.904*cos(-t - 1.57)^2 + 1)*pi)]
</code></pre>
<p>Example 2:</p>
<pre><code>rx(t) = -0.707*cos(t)*arccos(0.707*cos(-t - 1.57))/(sqrt(-0.5*cos(-t - 1.57)^2 + 1)*pi)
ry(t) = -0.707*sin(t)*arccos(0.707*cos(-t - 1.57))/(sqrt(-0.5*cos(-t - 1.57)^2 + 1)*pi)
rz(t) = 0.707*sin(-t - 1.57)*arccos(0.707*cos(-t - 1.57))/(sqrt(-0.5*cos(-t - 1.57)^2 + 1)*pi)
</code></pre>
https://ask.sagemath.org/question/8474/evenly-space-points-along-a-parametric-curve/?comment=20893#post-id-20893sure, faster would definitely be good. I tried writing some cython functions myself, but couldn't really get speed improvements. Wed, 16 Nov 2011 08:09:51 +0100https://ask.sagemath.org/question/8474/evenly-space-points-along-a-parametric-curve/?comment=20893#post-id-20893Answer by vdelecroix for <p>Are there arc-length parametrization functions hidden somewhere in Sage? I have some 3D parametric curves (smooth) of length <code>L</code> along which I would like to put <code>n</code> dots at regular intervals. What I've been doing so far is using numerical integration to find the arc length parameter, and then using <code>find_root</code> to find the positions of the dots (spaced by arc length <code>L/n</code>).</p>
<p>This is pretty slow, and has the further limitation that I need to specify a region on which <code>find_root</code> should work. If the parametrization is really uneven, it's tough to develop a good initial estimate for where to look.</p>
<p>So, does anyone have other ideas for doing this? Thanks!</p>
<p>UPDATE: Here are some examples -- they're different fibers in the Hopf fibration, and are given by <code>r = (rx,ry,rz)</code></p>
<p>Example 1: </p>
<pre><code>rx(t) = -0.309*cos(t)*arccos(0.951*cos(-t - 1.57))/(sqrt(-0.904*cos(-t - 1.570)^2 + 1)*pi)
ry(t) = -0.309*sin(t)*arccos(0.951*cos(-t - 1.57))/(sqrt(-0.904*cos(-t - 1.57)^2 + 1)*pi)
rz(t) = 0.951*sin(-t - 1.57)*arccos(0.951*cos(-t - 1.57))/(sqrt(-0.904*cos(-t - 1.57)^2 + 1)*pi)]
</code></pre>
<p>Example 2:</p>
<pre><code>rx(t) = -0.707*cos(t)*arccos(0.707*cos(-t - 1.57))/(sqrt(-0.5*cos(-t - 1.57)^2 + 1)*pi)
ry(t) = -0.707*sin(t)*arccos(0.707*cos(-t - 1.57))/(sqrt(-0.5*cos(-t - 1.57)^2 + 1)*pi)
rz(t) = 0.707*sin(-t - 1.57)*arccos(0.707*cos(-t - 1.57))/(sqrt(-0.5*cos(-t - 1.57)^2 + 1)*pi)
</code></pre>
https://ask.sagemath.org/question/8474/evenly-space-points-along-a-parametric-curve/?answer=14811#post-id-14811Hi,
I used some numerical integration (by hand) and without find_roots. It works quite well and quite fast.
x(t) = 2*cos(t) - cos(2*t)
y(t) = 2*sin(t) - sin(2*t)
dx(t) = x.derivative(t)
dy(t) = y.derivative(t)
arc_length = fast_callable(sqrt(dx(t)**2 + dy(t)**2), vars=(t,), domain=RR)
First I make an approximation of the total length of my curve
total_length = 0.0
for s in xsrange(0,2*pi,0.001):
total_length += arc_length(s) * 0.001
And then it is possible to store equally spaced points (here 20)
nb_pts = 20
step = total_length / nb_pts
length = 0.
next_pt = step
L = [(x(0),y(0))]
for s in xsrange(0,2*pi,0.001):
length += arc_length(s) * 0.001
if length >= next_pt:
L.append((x(s),y(s)))
next_pt += step
And
parametric_plot((x(t),y(t)), (t,0,2*pi)) + point2d(L, color='red', pointsize=20)
![image description](/upfiles/13664665214226876.png)Sat, 20 Apr 2013 11:03:14 +0200https://ask.sagemath.org/question/8474/evenly-space-points-along-a-parametric-curve/?answer=14811#post-id-14811