Wolfram Computation Meets Knowledge

New in the Wolfram Language: AnglePath

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.


Join the discussion

!Please enter your comment (at least 5 characters).

!Please enter your name.

!Please enter a valid email address.


  1. 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.

    • 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}]

  2. 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!