25
$\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$
1
  • 2
    $\begingroup$ I can't properly express how amused I am that my flippant remark has resulted in this. +1 to both. $\endgroup$
    – rcollyer
    Commented Nov 21, 2014 at 23:34

1 Answer 1

34
$\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$
3
  • 1
    $\begingroup$ NB: the original post had the same encodings for 1-4 as 6-9. This is now fixed. $\endgroup$
    – evanb
    Commented Nov 21, 2014 at 22:41
  • 4
    $\begingroup$ Or WolframAlpha["Morse code \"Dot/Dash Pattern\"",{{"MorseCodeTranslation", 1}, "Plaintext"}] :) $\endgroup$
    – Silvia
    Commented Nov 22, 2014 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$ Commented Feb 17, 2016 at 17:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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