Travis's answer works for line3d objects consisting of a single segment.

Here is a more general version for any line3d object.

If you haven't already, install more-itertools:

```
sage: %pip install more-itertools
```

Then define this length function:

```
def line3d_euclidean_length(L):
    r"""
    Return the euclidean length of this piecewise linear path.

    INPUT:

    - ``L`` a piecewise linear path, as a ``line3d`` graphics object.
    """
    from more_itertools import pairwise
    V = RDF^3
    return sum((V(q) - V(p)).norm() for p, q in pairwise(L.points))
```

Use it as follows:
```
sage: A = line3d([(1, 0, 1), (1, 1, 1)])
sage: line3d_euclidean_length(A)
sage: 1.0

sage: B = line3d([(0, 0, 0), (1, 0, 0), (1, 1, 1)])
sage: line3d_euclidean_length(B)
2.414213562373095
```

As Travis suggests, one might also be interested in the combinatorial 
length:

```
def line3d_combinatorial_length(L):
    r"""
    Return the combinatorial length of this piecewise linear path.

    That is, the number of segments it consists of.

    INPUT:

    - ``L`` a piecewise linear path, as a ``line3d`` graphics object.
    """
    return Integer(len(L.points) - 1)
```

Usage:

```
sage: line3d_combinatorial_length(A)
sage: line3d_combinatorial_length(B)
```

Following Travis's suggestion, one could add an option
to combine successive segments if they are collinear.

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sage-devel/d1410db8-8dee-4ecc-9c4e-aac2cf653aden%40googlegroups.com.

Reply via email to