Mathematica is perfect for working with lists and matrices. Essentially making it useful for defining and manipulating geometry through rotations and later on analysing the forces through stiffness methods.

A key task when working with series of co-ordinates is to check that the relative distances between points don’t change so that the structural elements in between remain the same, even though the structure may have changed position and shape. When you’re working with Mathematica and there’s a task that you need to do repeatedly it’s often worth creating a custom function that can be called whenever needed.

A couple of the functions that I’ve written can determine the distance between each co-ordinate in a list for 2D and 3D co-ordinate lists.

First, create some co-ordinates to work with on the examples.

1 2 3 |
x = y = z = Table[i, {i, 10}]; test2D = Partition[Riffle[x, y], 2]; test3D = Transpose[{x, y^3, z^2}]; |

Now that we’ve got some points to work with we can create a function to determine the distance between the points in 2D. On 2D points, Mathematica has an inbuilt function called EuclideanDistance[] which is nice and fast when determining the distance between 2D points.

There are two key parts to the function, the first part maps a list of points across the EuclideanDistance[] function, with the second section essentially iterating the co-ordinates to restructure the list so that the function can be mapped across the list by rearranging it in a (First point, Second point)(Second point, Third point) format…. until the last element in the list.

1 2 3 4 5 |
howFar2D[x_] := EuclideanDistance[#[[1]], #[[2]]] & /@ Table[{x[[i]], x[[i + 1]]}, {i, 1, Length[x] - 1} ] |

1 2 3 4 |
howFar2D[test2D] // AbsoluteTiming {0.000162, {Sqrt[2], Sqrt[2], Sqrt[2], Sqrt[2], Sqrt[2], Sqrt[2], Sqrt[2], Sqrt[2], Sqrt[2]}} |

This is nice and zippy for 2D co-ordinates, but won’t work unfortunately for 3D and needs modifying to make use of the inbuilt function Norm[] .

1 2 3 4 5 |
distanceNorm[x_] := Norm[#[[2]] - #[[1]]] & /@ Table[{x[[i]], x[[i + 1]]}, {i, 1, Length[x] - 1} ] |

1 2 3 4 |
distanceNorm[test2D] // AbsoluteTiming {0.000193, {Sqrt[2], Sqrt[2], Sqrt[2], Sqrt[2], Sqrt[2], Sqrt[2], Sqrt[2], Sqrt[2], Sqrt[2]}} |

1 2 3 4 |
distanceNorm[test3D] // AbsoluteTiming {0.000278, {Sqrt[59], 3 Sqrt[43], Sqrt[1419], Sqrt[3803], Sqrt[8403], 3 Sqrt[1811], Sqrt[28787], Sqrt[47379], Sqrt[73803]}} |

Running a comparison on speed for how 2D points are handled can be compared by zeroing out the Z ordinate.

1 2 3 4 5 |
zerothezordinate = Transpose[{x, y, Array[# - # &, 10]}]; distanceNorm[zerothezordinate] // AbsoluteTiming {0.000212, {Sqrt[2], Sqrt[2], Sqrt[2], Sqrt[2], Sqrt[2], Sqrt[2], Sqrt[2], Sqrt[2], Sqrt[2]}} |

As can be seen, EuclideanDistance[] is faster, but Norm[] is more flexible and can be used on 2D and 3D co-ordinates.