Message Boards Message Boards

GROUPS:

Fun with Turmites

Posted 1 month ago
248 Views
|
0 Replies
|
2 Total Likes
|

Just thought I would share a simple function I wrote to plot Turmites. It accepts 2-dimensional rule arrays in the same curly-bracketed format that you'll find in the image captions in the linked Wikipedia entry. The format is structured as rule[state][color] = {new color, turn direction, new state}.

For example, the main image can be generated by runTurmite[{{{1, 2, 0}, {1, 2, 1}}, {{0, 1, 0}, {0, 1, 1}}}, 8342];

and the fractal snowflake can be generated by runTurmite[{ {{1, 8, 1}, {1, 2, 0}}, {{1, 4, 1}, {1, 4, 2}}, {{ }, {0, 4, 0}} }, 300000].

One interesting 2-state 3-color Turmite that I found is described by this ruleset: { {{1, 2, 1}, {2, 8, 1}, {0, 2, 0}}, {{1, 2, 1}, {2, 2, 0}, {1, 8, 0}} } It features a mix of chaotic and patterned behavior, as you can see in this image generated from 100,000 steps:

turmite

Anyway, this was a fun little experiment. Hope someone else finds it useful!

runTurmite[rules_, steps_, plotOpts___] := Module[{
    orientation = 0, (* north *)
    state = 0,
    pos = {0, 0},
    world = <||>,
    color, turn
  },
  Do[
    color = Lookup[world, Key[pos], 0];
    {world[pos], turn, state} = rules[[state + 1, color + 1]];
    orientation = Mod[orientation + Log2[turn], 4];
    pos += Switch[orientation,
      0, { -1,  0 },  (* north *)
      1, {  0,  1 },  (* east *)
      2, {  1,  0 },  (* south *)
      3, {  0, -1 }   (* west *)
    ],
    steps
  ];
  ArrayPlot[world // toSparseArray, plotOpts]
];

(* helper function *)
toSparseArray[world_] := With[{
    offset = 1 - MapThread[Min, Keys[world]]
  },
  SparseArray[Normal[KeyMap[# + offset &, world]]]
];
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract