22
$\begingroup$

How can we define a dashing pattern in a plot such that it reads arbitrary text in Morse code?

For instance

Graphics[{DotDashed, Line[{{0, 0}, {5, 0}}]}]

Mathematica graphics

this could be interpreted as ".-" that is "a" in Morse code.

Proper spacing would be desirable in order to distinguish ".-" (a) from "-." (n).

(Inspired by @rcollyer's comment to my answer to this question.)

$\endgroup$
  • 2
    $\begingroup$ I can't properly express how amused I am that my flippant remark has resulted in this. +1 to both. $\endgroup$ – rcollyer Nov 21 '14 at 23:34
29
$\begingroup$

First define the Morse code (from rosettacode.org with corrections by @evanb)

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

$\endgroup$
  • 1
    $\begingroup$ NB: the original post had the same encodings for 1-4 as 6-9. This is now fixed. $\endgroup$ – evanb Nov 21 '14 at 22:41
  • 3
    $\begingroup$ Or WolframAlpha["Morse code \"Dot/Dash Pattern\"",{{"MorseCodeTranslation", 1}, "Plaintext"}] :) $\endgroup$ – Silvia Nov 22 '14 at 7:21
  • 1
    $\begingroup$ The only thing missing at this point is the corresponding sound with the "di"'s and "da"'s... :D $\endgroup$ – J. M. is away Feb 17 '16 at 17:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.