Skip to main content

First define the Morse code

morsecode = (#1 -> Characters[#2]) & @@@ {
   {"a", ".-"}, {"b", "-..."}, {"c", "-.-."},
   {"d", "-.."}, {"e", "."}, {"f", "..-."},
   {"g", "--."}, {"h", "...."}, {"i", ".."},
   {"j", ".---"}, {"k", "-.-"}, {"l", ".-.."},
   {"m", "--"}, {"n", "-."}, {"o", "---"},
   {"p", ".--."}, {"q", "--.-"}, {"r", ".-."},
   {"s", "..."}, {"t", "-"}, {"u", "..-"},
   {"v", "...-"}, {"w", ".--"}, {"x", "-..-"},
   {"y", "-.--"}, {"z", "--.."}, {"0", "-----"},
   {"1", ".----"}, {"2", "..---"}, {"3", "...--"},
   {"4", "....-"}, {"5", "....."}, {"6", "-...."},
   {"7", "--..."}, {"8", "---.."}, {"9", "----."}
   };

Use AbsoluteDashing and define dots as short dashed, lines as long dashes and include proper spacing between symbols and characters.

morserule =  Append[(#[[1]] -> Append[Riffle[#[[2]], 3], 12]) & /@ (morsecode /. {"." -> 2 , "-" -> 7}), " " -> {0, 17}];

toMorseDash[s_String] :=  Flatten[Characters[ToLowerCase@s] /. morserule]

Now we can test in a simple line.

Graphics[{Thickness[0.02], AbsoluteDashing[toMorseDash[" Morse Code "]], Line[{{0, 0}, {15, 0}}]}]

Mathematica graphics

For plots, use PlotStyle-> AbsoluteDashing[toMorseDash["text"]]

Plot[Sin[t], {t, 0, 4 Pi}, PlotStyle -> {Thick, AbsoluteDashing[toMorseDash[" Sine Wave"]]}]

Mathematica graphics

rhermans
  • 37.4k
  • 4
  • 61
  • 152