Wolfram Blog
José Martín-García

New in the Wolfram Language: AnglePath

May 21, 2015 — José Martín-García, Research Staff Member

A brilliant aspect of the Wolfram Language is that not only you can do virtually anything with it, you can also do whatever you want in many different ways. You can choose the method you prefer, or even better, try several methods to understand your problem from different perspectives.

For example, when drawing a graphic, we usually specify the coordinates of its points or elements. But sometimes it’s simpler to express the graphic as a collection of relative displacements: move a distance r in a direction forming an angle θ with respect to the direction of the segment constructed in the previous step. This is known as turtle graphics in computer graphics, and is basically what the new function AnglePath does. If all steps have the same length, use AnglePath[{θ1,θ2,...}] to specify the angles. If each step has a different length, use AnglePath[{{r1,θ1},{r2,θ2}, ...}] to give the pairs {length, angle}. That’s it. Let’s see some results.

Turn 60 degrees to the left with respect to the previous direction six times. You get a hexagon:

Using AnglePath to create a hexagon

If the angle is 135 degrees and you repeat eight times, then this is the result. Note that 8 * 135° = 1080°, so we go around the center three times:

Repeating 135 degree angle 8 times

Suppose that we again keep turning the same positive angle θ, but we increase the lengths of the steps linearly, in increments dr, from 0 to 1:

Code for creating spirals

Then we get these curves that spiral outward, producing nice outputs:

Spirals created using AnglePath

If we choose the angles randomly, we get random walks on the plane:

Choosing angles randomly to create random walks on a plane

Now let’s try to combine multiple AnglePath lines. Suppose that at each step we choose randomly between two possible angles and that the lengths obey a power law, so they get smaller in each iteration:

Input code for combining multiple AnlgePath lines

The result does not look very interesting yet:

Graphic of combining multiple lines using AnglePath

But if we repeat the experiment 10 times, then we start seeing some structure:

Repeating combining lines ten times

We can construct all different lists of 10 choices of the two angles, using Tuples (there are 2^10=1024 possible lists). Replacing Line with BSplineCurve produces curved lines instead of straight segments. The result is a nice self-similar structure:

Using Tuples and BSplineCurve to create curved lines instead of straight

AnglePath allows us to construct fractal structures easily, with very compact code. In fact, the code fits in a tweet! These are two examples derive from Wolfram Tweet-a-Program:

The Koch curve:

Koch Curve from Wolfram Tweet-a-Program


Curlicues from Wolfram Tweet-a-Program

These curious spirals are approximate Cornu spirals. With larger steps, they develop interesting substructure:

Cornu spirals

This was a quick introduction to how useful and fun the function AnglePath can be. AnglePath is supported in Version 10.1 of the Wolfram Language and Mathematica, and is rolling out soon in all other Wolfram products. Start using it now, and tweet your results through Wolfram TaP!

Download this post as a Computable Document Format (CDF) file.

Leave a Comment


Jesse Friedman

One longtime personal interest of mine is the Collatz conjecture. This Manipulate takes the Collatz function from ExampleData and allows you to adjust the angle and number to evaluate. It then produces an AnglePath Graphics object from the sequence produced by evaluating the Collatz rule with the number:

<< ExampleData/Collatz.m;
AnglePath[Table[{r, a \[Degree]}, {r, Collatz[n]}]]]], {a, 0,
360}, {n, 1, 100, 1}]

You can get some interesting patterns, and it's actually quite a nice visualization of the Collatz sequence. At first glance it might appear to be a random walk-like structure, but upon further examination areas of repetition appear.

Posted by Jesse Friedman    May 21, 2015 at 7:31 pm

    I like this! Try this one:
    Thickness[0.0005 + 0.01 ((max - #)/max)^(4 e)],
    Opacity[0.05 + 0.5 (#/max)^3],
    Table[{r/(1 + r^e), a Pi (0.7 - 2 Mod[r, 2]) }, {r,
    Reverse@Collatz[#]}]]]} & /@
    Reverse[Sort@RandomSample[Range[max], n]]], {{e, 1.2}, 0.01,
    3}, {{a, 0.08}, 0, 1}, {{n, 800}, 1, 2000, 1}, {{max, 200000000}, n,
    200000000}, {seed, 0, 1}, TrackedSymbols :> {n, max, seed, e, a}]

    Posted by Alex    June 1, 2015 at 10:47 am
Steeve Brechmann

The Wolfram Language is just an amazing tool. Every day, I’m amazed of what I can do with this tool! Keep the great work!

Posted by Steeve Brechmann    May 22, 2015 at 6:55 am
Tom Verhoeff

I do hope that they will also add a 3D analogon, where you specify, besides turn angle and travel distance, also the roll angle of the 3D turtle.

See for instance the following demonstration: http://demonstrations.wolfram.com/3DFlyingPipeLayingTurtle/

3D turtle geometry is much more interesting/challenging than the 3D variant. See

Tom Verhoeff.
“3D Turtle Geometry: Artwork, Theory, Program Equivalence and Symmetry”.
Int. J. of Arts and Technology, 3(2/3):288-319 (2010).

Posted by Tom Verhoeff    October 16, 2015 at 6:10 am

Leave a comment in reply to Tom Verhoeff


Or continue as a guest (your comment will be held for moderation):